gson
Object Serialization to JSON (using Gson). How to set field names in UpperCamelCase?
Taken from the docs: Gson supports some pre-defined field naming policies to convert the standard Java field names (i.e. camel cased names starting with lower case — “sampleFieldNameInJava”) to a Json field name (i.e. sample_field_name_in_java or SampleFieldNameInJava). See the FieldNamingPolicy class for information on the pre-defined naming policies. It also has an annotation based strategy … Read more
How to parse JSON string containing “NaN” in Node.js
Have a node.js app that is receiving JSON data strings that contain the literal NaN, like Then your NodeJS app isn’t receiving JSON, it’s receiving text that’s vaguely JSON-like. NaN is not a valid JSON token. Three options: 1. Get the source to correctly produce JSON This is obviously the preferred course. The data is … Read more
Parsing nested JSON data using GSON
You just need to create a Java class structure that represents the data in your JSON. In order to do that, I suggest you to copy your JSON into this online JSON Viewer and you’ll see the structure of your JSON much clearer… Basically you need these classes (pseudo-code): class Response Data data class Data … Read more
ProGuard and Gson on Android (ClassCastException)
For the latest version of the recommended proguard configuraiton file, please see the gson supplied android proguard example at: https://github.com/google/gson/blob/master/examples/android-proguard-example/proguard.cfg
How to handle deserializing with polymorphism?
In the Gson project code base is the RuntimeTypeAdapter, which reportedly works well for polymorphic serialization and deserialization. I don’t think I’ve yet tried to use it. See http://code.google.com/p/google-gson/issues/detail?id=231 for more info. Note, it hasn’t yet been included in any Gson releases. If use of it doesn’t fit your needs, then custom deserialization processing is … Read more
How do I convert a LinkedTreeMap to gson JsonObject
You’d typically have to serialize the object to JSON, then parse that JSON back into a JsonObject. Fortunately, Gson provides a toJsonTree method that kind of skips the parsing. LinkedTreeMap<?,?> yourMap = …; JsonObject jsonObject = gson.toJsonTree(yourMap).getAsJsonObject(); Note that, if you can, just deserialize the JSON directly to a JsonObject with gson.fromJson(theJson, JsonObject.class);
Retrofit2.0 gets MalformedJsonException while the json seems correct?
Finally I solved my problem which is not related to the json lenient mode, something wrong with my POST response (there some other non json output before the json data). Here is the response from JakeWharton regarding how to set Gson lenient mode: make sure that you haveļ¼compile ‘com.google.code.gson:gson:2.6.1’ Gson gson = new GsonBuilder() .setLenient() … Read more
Gson turn an array of data objects into json – Android
Here’s a comprehensive example on how to use Gson with a list of objects. This should demonstrate exactly how to convert to/from Json, how to reference lists, etc. Test.java: import com.google.gson.Gson; import java.util.List; import java.util.ArrayList; import com.google.gson.reflect.TypeToken; import java.lang.reflect.Type; public class Test { public static void main (String[] args) { // Initialize a list of … Read more
Using GSON with proguard enabled
Variable names will be obfuscated with proguard, leaving you with something like private String a; Instead of private String descripcionCategoria; You can add proguard rules so some classes don’t get obfuscated. I got away with it using these: -keepattributes Signature # POJOs used with GSON # The variable names are JSON key values and should … Read more