Gson serialize null for specific class or field

I have a solution similar to the one of Aleksey but that can be applied to one or more fields in any class (example in Kotlin): Create a new annotation for fields that should be serialized as null: @Retention(AnnotationRetention.RUNTIME) @Target(AnnotationTarget.FIELD) annotation class SerializeNull Create a TypeAdapterFactory that checks if a class has fields annotated with … Read more

Jsonpath with Jackson or Gson

The Jayway JsonPath library has support for reading values using a JSON path. For example: String json = “…”; Map<String, Object> book = JsonPath.read(json, “$.store.book[0]”); System.out.println(book); // prints {category=reference, author=Nigel Rees, title=Sayings of the Century, price=8.95} Double price = JsonPath.read(json, “$.store.bicycle.price”); System.out.println(price); // prints 19.95 You can also map JSON objects directly to classes, like … Read more

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

Parse a nested JSON using gson

If you only need the “PostalCode”, you could use JsonParser instead of having a bunch of classes: JsonParser jsonParser = new JsonParser(); JsonObject address = jsonParser.parse(json) .getAsJsonObject().get(“Response”) .getAsJsonObject().getAsJsonArray(“View”).get(0) .getAsJsonObject().getAsJsonArray(“Result”).get(0) .getAsJsonObject().get(“Location”) .getAsJsonObject().getAsJsonObject(“Address”); String postalCode = address.get(“PostalCode”).getAsString(); or for all results: JsonArray results = jsonParser.parse(json) .getAsJsonObject().get(“Response”) .getAsJsonObject().getAsJsonArray(“View”).get(0) .getAsJsonObject().getAsJsonArray(“Result”); for (JsonElement result : results) { JsonObject address = … Read more

Best approach to parse huge (extra large) JSON file

You don’t need to switch to Jackson. Gson 2.1 introduced a new TypeAdapter interface that permits mixed tree and streaming serialization and deserialization. The API is efficient and flexible. See Gson’s Streaming doc for an example of combining tree and binding modes. This is strictly better than mixed streaming and tree modes; with binding you … Read more

Gson NoClassDefFoundError after ADT and SDK Tools update to v17

I faced a similar problem today. This fixed it for me: Remove all Android Library projects and external jars from the build path. Create a folder named ‘libs’ in your project. Place all external .jars in that folder, the ADT should now place them under ‘Android Dependencies’. Re-import all your previous Android Library projects the … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)