Google-gson is a java library from Google for encoding and decoding JSON text.
Get the latest binaries from
http://code.google.com/p/google-gson/.
Versioning allows to serialize only a certain properties
based on version. We can annotate the required properties with @Since annotation.
NamedClass namedCls = new NamedClass();
namedCls.setName("BTC");
namedCls.setName1("BTC1");
namedCls.setName2("BTC2");
namedCls.setName3("BTC3");
namedCls.setName4("BTC4");
Gson gson = new GsonBuilder().setPrettyPrinting().create();
System.out.println("Non versioned");
System.out.println(gson.toJson(namedCls));
// Field or type marked with a version higher than this value are
// ignored during serialization or de-serialization.
gson = new GsonBuilder().setVersion(1.0).setPrettyPrinting().create();
System.out.println("Fields versioned less or equal to 1.0");
System.out.println(gson.toJson(namedCls));
gson = new GsonBuilder().setVersion(2.0).setPrettyPrinting().create();
System.out.println("Fields versioned less or equal to 2.0");
System.out.println(gson.toJson(namedCls));
gson = new GsonBuilder().setVersion(3.0).setPrettyPrinting().create();
System.out.println("Fields versioned less or equal to 3.0");
System.out.println(gson.toJson(namedCls));
}
}
It gives the following output,
Student.json
Non versioned
{
"name": "BTC",
"name1": "BTC1",
"name2": "BTC2",
"name3": "BTC3",
"name4": "BTC4"
}
Fields versioned less or equal to 1.0
{
"name": "BTC",
"name1": "BTC1"
}
Fields versioned less or equal to 2.0
{
"name": "BTC",
"name1": "BTC1",
"name2": "BTC2"
}
Fields versioned less or equal to 3.0
{
"name": "BTC",
"name1": "BTC1",
"name2": "BTC2",
"name3": "BTC3"
}