gson: Treat null as empty String

The above answer works okay for serialisation, but on deserialisation, if there is a field with null value, Gson will skip it and won’t enter the deserialize method of the type adapter, therefore you need to register a TypeAdapterFactory and return type adapter in it. Gson gson = GsonBuilder().registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory()).create(); public static class NullStringToEmptyAdapterFactory<T> implements … Read more

Karaf / Maven – Unable to resolve: missing requirement osgi.wiring.package

I believe you have two options here. If you have Import-Package: com.google.gson;version=”[2.3,3)” in your MANIFEST.MF, this means that you want some package to be imported from a deployed bundle, not from an embedded jar. In this case, you should first deploy gson-2.3.1.jar bundle (copy this file to the deploy folder), and then deploy your bundle. … Read more

GSON parsing without a lot of classes

You don’t need to define any new classes, you can simply use the JSON objects that come with the Gson library. Heres a simple example: JsonParser parser = new JsonParser(); JsonObject rootObj = parser.parse(json).getAsJsonObject(); JsonObject locObj = rootObj.getAsJsonObject(“result”) .getAsJsonObject(“geometry”).getAsJsonObject(“location”); String status = rootObj.get(“status”).getAsString(); String lat = locObj.get(“lat”).getAsString(); String lng = locObj.get(“lng”).getAsString(); System.out.printf(“Status: %s, Latitude: %s, … 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

Is there a standard implementation for a GSON Joda Time serialiser?

I’ve decided to roll my own open source one – you can find it here: https://github.com/gkopff/gson-jodatime-serialisers Here’s the Maven details (check central for the latest version): <dependency> <groupId>com.fatboyindustrial.gson-jodatime-serialisers</groupId> <artifactId>gson-jodatime-serialisers</artifactId> <version>1.6.0</version> </dependency> And here’s a quick example of how you drive it: Gson gson = Converters.registerDateTime(new GsonBuilder()).create(); SomeContainerObject original = new SomeContainerObject(new DateTime()); String json = … Read more

How to serialize a class with an interface?

Here is a generic solution that works for all cases where only interface is known statically. Create serialiser/deserialiser: final class InterfaceAdapter<T> implements JsonSerializer<T>, JsonDeserializer<T> { public JsonElement serialize(T object, Type interfaceType, JsonSerializationContext context) { final JsonObject wrapper = new JsonObject(); wrapper.addProperty(“type”, object.getClass().getName()); wrapper.add(“data”, context.serialize(object)); return wrapper; } public T deserialize(JsonElement elem, Type interfaceType, JsonDeserializationContext context) … Read more

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 array response using Retrofit & Gson

Please try to use this one @FormUrlEncoded @POST(“api/sponsors”) Call<List<SponsorsResult>> getStatesAndDistrict( @Field(“xyz”) String field1 ); Call <List<SponsorsResult>> call = service.getSponsorsValue(); call.enqueue(new Callback<List<SponsorsResult>>() { @Override public void onResponse(Call<List<SponsorsResult>> call, Response<List<SponsorsResult>> response) { List<SponsorsResult> rs = response.body(); } @Override public void onFailure(Call<List<SponsorsResult>> call, Throwable t) { } }); class SponsorsResult { @SerializedName(“sponsors”) private List<SponsorsValue> sponsors; public List<SponsorsValue> getSponsors() … 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; } }

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