Exclude certain fields from serialization based on value in GSON

I use GSON for my serialization purposes, I do not find a way to exclude certain fields from serialization based on the ExclusionStrategy class provided by Gson based on the field value, since it only supports top-level exceptions or field attributes. Field attributes do not include the value of this field . So what should I do?

+7
source share
2 answers

A way to achieve this is to create a custom serializer for the class in question. After Gson creates the default JSON object, remove the property that you want to exclude based on its value.

public class SerializerForMyClass implements JsonSerializer<MyClass> { @Override public JsonElement serialize(MyClass obj, Type type, JsonSerializationContext jsc) { Gson gson = new Gson(); JsonObject jObj = (JsonObject)gson.toJsonTree(obj); if(obj.getMyProperty()==0){ jObj.remove("myProperty"); } return jObj; } } 

And register a new serializer in the Gson object that you use to serialize in the application for this class.

 GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(MyClass.class, new SerializerForMyClass()); Gson gson=gsonBuilder.create(); gson.toJson(myObjectOfTypeMyClass); 
+19
source

Here is a more detailed example of a class that I wrote to remove all false logical lines, as well as all "false" lines. It was folded pretty quickly, but it seems to be working fine. Let me know if there are any errors.

 public class RemoveFalseJsonSerializer implements JsonSerializer<Object> { //~ Methods -------------------------------------------------------------------------------------------------------- /** * serialize * * @param object in value * @param type in value * @param jsonSerializationContext in value * * @return out value */ @Override public JsonElement serialize(Object object, Type type, JsonSerializationContext jsonSerializationContext) { Gson gson = new Gson(); JsonElement jsonElement = gson.toJsonTree(object); trimJson(jsonElement); return jsonElement; } /** * We've finally made it to a primitive of some sort. Should we trim it? * * @param jsonElement in value * * @return out value */ private boolean shouldTrimElement(JsonElement jsonElement) { return jsonElement == null || jsonElement.isJsonNull() || (jsonElement.isJsonPrimitive() && ((jsonElement.getAsJsonPrimitive().isBoolean() && !jsonElement.getAsBoolean()) // trim false || (jsonElement.getAsJsonPrimitive().isString() // also trim the string "false" && "false".equalsIgnoreCase(jsonElement.getAsString())))); } /** * trimJson * * @param jsonElement in value */ private void trimJson(JsonElement jsonElement) { if (jsonElement == null || jsonElement.isJsonNull() || jsonElement.isJsonPrimitive()) { return; } if (jsonElement.isJsonObject()) { List<String> toRemove = new ArrayList<>(); JsonObject asJsonObject = jsonElement.getAsJsonObject(); for (Map.Entry<String, JsonElement> jsonElementEntry : asJsonObject.entrySet()) { if (jsonElementEntry.getValue().isJsonObject() || jsonElementEntry.getValue().isJsonArray()) { trimJson(jsonElementEntry.getValue()); } else if (shouldTrimElement(jsonElementEntry.getValue())) { toRemove.add(jsonElementEntry.getKey()); } } if (CollectionUtils.isNotEmpty(toRemove)) { for (String remove : toRemove) { asJsonObject.remove(remove); } } } else if (jsonElement.isJsonArray()) { JsonArray asJsonArray = jsonElement.getAsJsonArray(); for (JsonElement element : asJsonArray) { trimJson(element); } } } } 
0
source

All Articles