Polymorphism with gson

This is a bit late but I had to do exactly the same thing today. So, based on my research and when using gson-2.0 you really don’t want to use the registerTypeHierarchyAdapter method, but rather the more mundane registerTypeAdapter. And you certainly don’t need to do instanceofs or write adapters for the derived classes: just … Read more

Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $

This is a well-known issue and based on this answer you could add setLenient: Gson gson = new GsonBuilder() .setLenient() .create(); Retrofit retrofit = new Retrofit.Builder() .baseUrl(BASE_URL) .client(client) .addConverterFactory(GsonConverterFactory.create(gson)) .build(); Now, if you add this to your retrofit, it gives you another error: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 … Read more

GSON – Date format

It seems that you need to define formats for both date and time part or use String-based formatting. For example: Gson gson = new GsonBuilder() .setDateFormat(“EEE, dd MMM yyyy HH:mm:ss zzz”).create(); or using java.text.DateFormat Gson gson = new GsonBuilder() .setDateFormat(DateFormat.FULL, DateFormat.FULL).create(); or do it with serializers: I believe that formatters cannot produce timestamps, but this … Read more

Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2

You state in the comments that the returned JSON is this: { “dstOffset” : 3600, “rawOffset” : 36000, “status” : “OK”, “timeZoneId” : “Australia/Hobart”, “timeZoneName” : “Australian Eastern Daylight Time” } You’re telling Gson that you have an array of Post objects: List<Post> postsList = Arrays.asList(gson.fromJson(reader, Post[].class)); You don’t. The JSON represents exactly one Post … Read more

How to parse json parsing Using GSON in android

you could try reading the gson value like this: try { AssetManager assetManager = getAssets(); InputStream ims = assetManager.open(“file.txt”); Gson gson = new Gson(); Reader reader = new InputStreamReader(ims); GsonParse gsonObj = gson.fromJson(reader, GsonParse.class); }catch(IOException e) { e.printStackTrace(); } Assuming that you are just receiving this one block and not a list. And also this … Read more

Gson serialize a list of polymorphic objects

There’s a simple solution: Gson’s RuntimeTypeAdapterFactory (from com.google.code.gson:gson-extras:$gsonVersion). You don’t have to write any serializer, this class does all work for you. Try this with your code: ObixBaseObj lobbyObj = new ObixBaseObj(); lobbyObj.setIs(“obix:Lobby”); ObixOp batchOp = new ObixOp(); batchOp.setName(“batch”); batchOp.setIn(“obix:BatchIn”); batchOp.setOut(“obix:BatchOut”); lobbyObj.addChild(batchOp); RuntimeTypeAdapterFactory<ObixBaseObj> adapter = RuntimeTypeAdapterFactory .of(ObixBaseObj.class) .registerSubtype(ObixBaseObj.class) .registerSubtype(ObixOp.class); Gson gson2=new GsonBuilder().setPrettyPrinting().registerTypeAdapterFactory(adapter).create(); Gson gson = … 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); Hope that helps.

Get nested JSON object with GSON using retrofit

You would write a custom deserializer that returns the embedded object. Let’s say your JSON is: { “status”:”OK”, “reason”:”some reason”, “content” : { “foo”: 123, “bar”: “some value” } } You’d then have a Content POJO: class Content { public int foo; public String bar; } Then you write a deserializer: class MyDeserializer implements JsonDeserializer<Content> … 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