Are you using Gson for Serializing Json String from Java Bean/Model class? If yes then It may be useful for you.
Gson ignore null/empty value while serialize Object to Json String. Like
Gson gson = new Gson();
String jsonData = gson.toJson(obj);
In above code if obj has any empty/null String variable then Gson didn't write that variable to Json String.
But some time we need that parameters in Json. GsonBuilder has serialize() method to serialize Null/Empty variable to Json String.
Here is example code for serialize object with empty/null variable to Json String.
Object obj = new Object();
GsonBuilder gb = new GsonBuilder();
gb.serializeNulls();
Gson gson = gb.create();
String jsonData = gson.toJson(obj);
Happy Coding :D
0 Comment(s)