Why does GSON use fields and not getters/setters?

Generally speaking when you serialize/deserialize an object, you are doing so to end up with an exact copy of the state of the object; As such, you generally want to circumvent the encapsulation normally desired in an OO design. If you do not circumvent the encapsulation, it may not be possible to end up with … Read more

class A declares multiple JSON fields

Since they are private fields there should not be any problem while creating json string I don’t think this statement is true, GSON looks up at the object’s private fields when serializing, meaning all private fields of superclass are included, and when you have fields with same name it throws an error. If there’s any … Read more

How to handle Dynamic JSON in Retrofit?

Late to the party, but you can use a converter. RestAdapter restAdapter = new RestAdapter.Builder() .setEndpoint(“https://graph.facebook.com”) .setConverter(new DynamicJsonConverter()) // set your static class as converter here .build(); api = restAdapter.create(FacebookApi.class); Then you use a static class which implements retrofit’s Converter: static class DynamicJsonConverter implements Converter { @Override public Object fromBody(TypedInput typedInput, Type type) throws ConversionException … Read more

com.google.gson.internal.LinkedTreeMap cannot be cast to my class [duplicate]

In my opinion, due to type erasure, the parser can’t fetch the real type T at runtime. One workaround would be to provide the class type as parameter to the method. Something like this works, there are certainly other possible workarounds but I find this one very clear and concise. public static <T> List<T> stringToArray(String … Read more

How to Parse JSON Array with Gson

You can parse the JSONArray directly, don’t need to wrap your Post class with PostEntity one more time and don’t need new JSONObject().toString() either: Gson gson = new Gson(); String jsonOutput = “Your JSON String”; Type listType = new TypeToken<List<Post>>(){}.getType(); List<Post> posts = gson.fromJson(jsonOutput, listType);

Retrofit GSON serialize Date from json string into java.util.date

Gson gson = new GsonBuilder() .setDateFormat(“yyyy-MM-dd’T’HH:mm:ss”) .create(); RestAdapter restAdapter = new RestAdapter.Builder() .setEndpoint(API_BASE_URL) .setConverter(new GsonConverter.create(gson)) .build(); Or the Kotlin equivalent: val gson = GsonBuilder().setDateFormat(“yyyy-MM-dd’T’HH:mm:ss”).create() RestAdapter restAdapter = Retrofit.Builder() .baseUrl(API_BASE_URL) .addConverterFactory(GsonConverterFactory.create(gson)) .build() .create(T::class.java) You can set your customized Gson parser to retrofit. More here: Retrofit Website Look at Ondreju’s response to see how to implement this … Read more

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