I use google-gson to serialize a Java map into a JSON string. It provides a builder handles null values:
Gson gson = new GsonBuilder().serializeNulls().create();
The problem is that the result is the string null, as in:
gson.toJson(categoriesMap));
{"111111111":null}
And the required result is:
{"111111111":""}
I can do a String-replace for null and "", but this is ugly and prone to errors. Is there a native gson support for adding a custom String instead of null?
解决方案
If this were your application type, you could register a type adapter. But Gson doesn't allow you to change the serialized form of strings or primitive types.
Your best bet is to change your Java objects. Or to loosen the restriction around nulls.