How does Gson handles object's fields?
Category: com.google.gson, viewed: 6K time(s).
In this example you'll see how the Gson library handles the object fields. For object fields to be serialized into JSON string it doesn't need to use any annotations, it can even read private fields. If you have a null value field it will not be serialized into JSON string. To exclude a field from serialization you can mark the field with the transient keyword.
In the snippet below we create a People object. This object has a null value field named age, this will not included in the serialization because we didn't assign any value to it. The secret field is also not serialized because we mark it with transient keyword.
If your run the code above you'll see the following line printed:
json = {"name":"John","address":"350 Banana St.","dateOfBirth":"Nov 11, 1980 8:47:04 AM"}
Here is the People class.
More examples on com.google.gson
|
|