I am using GSON for my serialization purposes, I am not finding a way of excluding certain fields from serialization based on ExclusionStrategy class provided by Gson based
This is how I use a type adapter to avoid serializing boolean values that are false. It avoids creating an additional Gson instance and does not rely on specific field names.
class MyClassTypeAdapter: JsonSerializer{
override fun serialize(myObject: MyClass, type: Type, context: JsonSerializationContext): JsonElement {
val jsonElement = context.serialize(myObject)
jsonElement.asJsonObject.entrySet().removeAll { it.value is JsonPrimitive && (it.value as JsonPrimitive).isBoolean && !it.value.asBoolean }
return jsonElement
}
}