Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSONObject transforms to nameValuePair #782

Closed
Kolyall opened this issue Feb 3, 2016 · 5 comments
Closed

JSONObject transforms to nameValuePair #782

Kolyall opened this issue Feb 3, 2016 · 5 comments

Comments

@Kolyall
Copy link

Kolyall commented Feb 3, 2016

Example:

class Example {
 @SerializedName("exampleField")
    public JSONObject exampleField = new JSONObject("{\"name\":\"Nick\"}");
}

Log.e(TAG,gson.toJson(new Example()))

result:

"exampleField": {
  "nameValuePairs": {
   "name": "Nick"
  }
 }

expected Result:

"exampleField": {
   "name": "Nick"
   }
@JakeWharton
Copy link
Contributor

Use com.google.gson.JsonObject not org.json.JSONObject.

@JakeWharton
Copy link
Contributor

Or write a TypeAdapterFactory that can handle org.json* types, but using Gson's own types seems a lot easier.

@l3vick
Copy link

l3vick commented Aug 14, 2018

with JsonObject my result is

"exampleField": {
"members": {
"name": "Nick"
}
}

instead of the expected result.

Can I create a JsonObject without any LinkedTreeMap or similar?

@lingyfh
Copy link

lingyfh commented Sep 7, 2018

GsonBuilder registerTypeAdapter

new GsonBuilder()
.registerTypeAdapter(JSONObject.class, JSONObjectAdapter.sInstance)
.registerTypeAdapter(JSONArray.class, JSONArrayAdapter.sInstance)
    static class JSONObjectAdapter implements JsonSerializer<JSONObject>, JsonDeserializer<JSONObject> {

        public static JSONObjectAdapter sInstance = new JSONObjectAdapter();

        @Override
        public JsonElement serialize(JSONObject src, Type typeOfSrc, JsonSerializationContext context) {
            if (src == null) {
                return null;
            }

            JsonObject jsonObject = new JsonObject();
            Iterator<String> keys = src.keys();
            while (keys.hasNext()) {
                String key = keys.next();
                Object value = src.opt(key);

                JsonElement jsonElement = context.serialize(value, value.getClass());
                jsonObject.add(key, jsonElement);
            }
            return jsonObject;
        }

        @Override
        public JSONObject deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            if (json == null) {
                return null;
            }
            try {
                return new JSONObject(json.toString());
            } catch (JSONException e) {
                e.printStackTrace();
                throw new JsonParseException(e);
            }
        }
    }


    static class JSONArrayAdapter implements JsonSerializer<JSONArray>, JsonDeserializer<JSONArray> {

        public static final JSONArrayAdapter sInstance = new JSONArrayAdapter();

        @Override
        public JsonElement serialize(JSONArray src, Type typeOfSrc, JsonSerializationContext context) {
            if (src == null) {
                return null;
            }
            JsonArray jsonArray = new JsonArray();
            for (int i = 0; i < src.length(); i++) {
                Object object = src.opt(i);
                JsonElement jsonElement = context.serialize(object, object.getClass());
                jsonArray.add(jsonElement);
            }
            return jsonArray;
        }

        @Override
        public JSONArray deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            if (json == null) {
                return null;
            }
            try {
                return new JSONArray(json.toString());
            } catch (JSONException e) {
                e.printStackTrace();
                throw new JsonParseException(e);
            }
        }
    }

@ZimmerZhou
Copy link

GsonBuilder registerTypeAdapter

new GsonBuilder()
.registerTypeAdapter(JSONObject.class, JSONObjectAdapter.sInstance)
.registerTypeAdapter(JSONArray.class, JSONArrayAdapter.sInstance)
    static class JSONObjectAdapter implements JsonSerializer<JSONObject>, JsonDeserializer<JSONObject> {

        public static JSONObjectAdapter sInstance = new JSONObjectAdapter();

        @Override
        public JsonElement serialize(JSONObject src, Type typeOfSrc, JsonSerializationContext context) {
            if (src == null) {
                return null;
            }

            JsonObject jsonObject = new JsonObject();
            Iterator<String> keys = src.keys();
            while (keys.hasNext()) {
                String key = keys.next();
                Object value = src.opt(key);

                JsonElement jsonElement = context.serialize(value, value.getClass());
                jsonObject.add(key, jsonElement);
            }
            return jsonObject;
        }

        @Override
        public JSONObject deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            if (json == null) {
                return null;
            }
            try {
                return new JSONObject(json.toString());
            } catch (JSONException e) {
                e.printStackTrace();
                throw new JsonParseException(e);
            }
        }
    }


    static class JSONArrayAdapter implements JsonSerializer<JSONArray>, JsonDeserializer<JSONArray> {

        public static final JSONArrayAdapter sInstance = new JSONArrayAdapter();

        @Override
        public JsonElement serialize(JSONArray src, Type typeOfSrc, JsonSerializationContext context) {
            if (src == null) {
                return null;
            }
            JsonArray jsonArray = new JsonArray();
            for (int i = 0; i < src.length(); i++) {
                Object object = src.opt(i);
                JsonElement jsonElement = context.serialize(object, object.getClass());
                jsonArray.add(jsonElement);
            }
            return jsonArray;
        }

        @Override
        public JSONArray deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            if (json == null) {
                return null;
            }
            try {
                return new JSONArray(json.toString());
            } catch (JSONException e) {
                e.printStackTrace();
                throw new JsonParseException(e);
            }
        }
    }

NB

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants