Gson: @Expose vs @SerializedName

Even if it’s late I wanted to answer this question. To explain it we must know what is serialization and deserialization. serialization is converting object into json string and deserialization is converting json string into object. Let’s say we’ve User class with no annotations. public class User{ private String userName; private Integer userAge; public User(String … Read more

Gson custom seralizer for one variable (of many) in an object using TypeAdapter

This is a great question because it isolates something that should be easy but actually requires a lot of code. To start off, write an abstract TypeAdapterFactory that gives you hooks to modify the outgoing data. This example uses a new API in Gson 2.2 called getDelegateAdapter() that allows you to look up the adapter … Read more

gson.toJson() throws StackOverflowError

That problem is that you have a circular reference. In the BomModule class you are referencing to: private Collection<BomModule> parentModules; private Collection<BomModule> subModules; That self reference to BomModule, obviously, not liked by GSON at all. A workaround is just set the modules to null to avoid the recursive looping. This way I can avoid the … Read more

How to convert a String to JsonObject using gson library

You can convert it to a JavaBean if you want using: Gson gson = new GsonBuilder().setPrettyPrinting().create(); gson.fromJson(jsonString, JavaBean.class) To use JsonObject, which is more flexible, use the following: String json = “{\”Success\”:true,\”Message\”:\”Invalid access token.\”}”; JsonParser jsonParser = new JsonParser(); JsonObject jo = (JsonObject)jsonParser.parse(json); Assert.assertNotNull(jo); Assert.assertTrue(jo.get(“Success”).getAsString()); Which is equivalent to the following: JsonElement jelem = gson.fromJson(json, … Read more

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

Using GSON to parse a JSON array

Problem is caused by comma at the end of (in your case each) JSON object placed in the array: { “number”: “…”, “title”: “..” , //<- see that comma? } If you remove them your data will become [ { “number”: “3”, “title”: “hello_world” }, { “number”: “2”, “title”: “hello_world” } ] and Wrapper[] data … Read more

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

Gson library in Android Studio

Add following dependency to build.gradle: implementation ‘com.google.code.gson:gson:2.8.7’ Or download the JAR file from Maven by clicking a release and finding the .jar file. Replace 2.8.7 with the latest version from Maven. Visit the GitHub repo for documentation and more.

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