gson invoking standard deserialization in custom deserializer

You can do that by implementing custom TypeAdapterFactory for your object (say CustomClass.class) to be deserialized as below. public class CustomTypeAdapterFactory implements TypeAdapterFactory { public final TypeAdapter create(Gson gson, TypeToken type) { return new TypeAdapter() { @Override public void write(JsonWriter out, Object value) throws IOException { JsonElement tree = delegate.toJsonTree(value); //add code for writing object …

Read more

Parse JSON file using GSON

Imo, the best way to parse your JSON response with GSON would be creating classes that “match” your response and then use Gson.fromJson() method. For example: class Response { Map<String, App> descriptor; // standard getters & setters… } class App { String name; int age; String[] messages; // standard getters & setters… } Then just …

Read more

JSON string from Gson: remove double quotes

It’s not documented properly, but JsonElement#toString() gets you a string that represents the JSON element and would be appropriate for re-creating the JSON serialization. What you want is JsonElement#getAsString(). This will throw an error if you’re not looking at a string, but if you are, you’ll get the string value. Here’s a test program to …

Read more

GSON – Custom serializer in specific case

You can write a custom serializer something like this: public class StudentAdapter implements JsonSerializer<Student> { @Override public JsonElement serialize(Student src, Type typeOfSrc, JsonSerializationContext context) { JsonObject obj = new JsonObject(); obj.addProperty(“name”, src.name); obj.addProperty(“school”, src.school.id); return obj; } }

How to compare JSON documents and return the differences with Jackson or Gson?

Reading the JSON documents as Maps and comparing them You could read both JSON documents as Map<K, V>. See the below examples for Jackson and Gson: ObjectMapper mapper = new ObjectMapper(); TypeReference<HashMap<String, Object>> type = new TypeReference<HashMap<String, Object>>() {}; Map<String, Object> leftMap = mapper.readValue(leftJson, type); Map<String, Object> rightMap = mapper.readValue(rightJson, type); Gson gson = new …

Read more

Gson – Automatic quote (“) escaping?

Maybe I’m not understanding your question, but I was able to get GSON to handle Strings with quotes without any settings or changes. import com.google.gson.Gson; public class GSONTest { public String value; public static void main(String[] args) { Gson g = new Gson(); GSONTest gt = new GSONTest(); gt.value = “This is a \”test\” of …

Read more

Serialize java object with GSON

In order to get the result you desire, you need to write the serializer like this: public static class PersonSerializer implements JsonSerializer<Person> { public JsonElement serialize(final Person person, final Type type, final JsonSerializationContext context) { JsonObject result = new JsonObject(); result.add(“id”, new JsonPrimitive(person.getId())); result.add(“name”, new JsonPrimitive(person.getName())); Person parent = person.getParent(); if (parent != null) { …

Read more