Gson: How to exclude specific fields from Serialization without annotations

Any fields you don’t want serialized in general you should use the “transient” modifier, and this also applies to json serializers (at least it does to a few that I have used, including gson). If you don’t want name to show up in the serialized json give it a transient keyword, eg: private transient String … Read more

Deserialize a List object with Gson?

Method to deserialize generic collection: import java.lang.reflect.Type; import com.google.gson.reflect.TypeToken; … Type listType = new TypeToken<ArrayList<YourClass>>(){}.getType(); List<YourClass> yourClassList = new Gson().fromJson(jsonArray, listType); Since several people in the comments have mentioned it, here’s an explanation of how the TypeToken class is being used. The construction new TypeToken<…>() {}.getType() captures a compile-time type (between the < and >) … Read more

Representing null in JSON

Let’s evaluate the parsing of each: http://jsfiddle.net/brandonscript/Y2dGv/ var json1 = ‘{}’; var json2 = ‘{“myCount”: null}’; var json3 = ‘{“myCount”: 0}’; var json4 = ‘{“myString”: “”}’; var json5 = ‘{“myString”: “null”}’; var json6 = ‘{“myArray”: []}’; console.log(JSON.parse(json1)); // {} console.log(JSON.parse(json2)); // {myCount: null} console.log(JSON.parse(json3)); // {myCount: 0} console.log(JSON.parse(json4)); // {myString: “”} console.log(JSON.parse(json5)); // {myString: “null”} … Read more

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