JsonConverter.java 829 Bytes
package net.plil.clubinfo.etunicorn.utils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParser;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by badetitou on 05/02/2017.
 */

public class JsonConverter {

    private static Gson gson;

    public static Gson getConverter(){
        if (gson != null)
            return gson;

        gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        return gson;
    }

    public static JSONObject convertToJSONObject(Object selectedItem) {
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(getConverter().toJson(selectedItem));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonObject;
    }
}