[QUESTION] WriteNulls + Filter
IHello from graphql-kotlin!. I am currently exploring the usage of fastjson2 as replacement for jackson, our benchmarks are showing impressive results when comparing jackson with fastjson2.
Currently I am facing an issue where i need to partially write and not write null values,
consider this data class and its @JsonInclude annotation
@JsonInclude(JsonInclude.Include.NON_NULL)
data class GraphQLResponse(
val data: Map<String, Any?>? = null,
val errors: List<GraphQLServerError>? = null,
val extensions: Map<String, Any?>? = null,
)
the errors field needs to be included if and only if its a NON NULL value, however, if the data map contains null values those need to be serialized,
in order words, this object
val toSerialize = GraphQLResponse(
data = mapOf(
"foo" to mapOf(
"bar" to 1
"bar2" to null
),
"other" to "something",
"baa" to null
),
errors = null
extensions = null
)
Should be serialized as:
{
"data": {
"foo": {
"bar": 1,
"bar2": null
},
"other": "something",
"baa": null
}
}
errors and extensions fields not included in top level fields, data field contains the JSON representation of its type Map<String, Any?>, and null values are included.
Currently i was able to do this using a combination of a PropertyPreFilter and a JsonWriter feature
val filter = PropertyPreFilter { _, source, name ->
when {
source is GraphQLResponse && name == "errors" -> source.errors != null
else -> true
}
}
JSON.toJSONString(toSerialize, filter, JSONWriter.Feature.WriteNulls)
is there a way to to this in a different way without filters ?