[QUESTION] WriteNulls + Filter
- Dominant language
- Java
- Stars
- 4.4k
- Forks
- 613
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 6
Description
IHello from [graphql-kotlin](https://github.com/ExpediaGroup/graphql-kotlin/tree/master)!.
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
```kotlin
@JsonInclude(JsonInclude.Include.NON_NULL)
data class GraphQLResponse(
val data: Map? = null,
val errors: List? = null,
val extensions: Map? = 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
```kotlin
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`, and null values are included.
Currently i was able to do this using a combination of a `PropertyPreFilter` and a `JsonWriter` feature
```kotlin
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 ?
Contributor guide
Research direction
Start with the JSON.toJSONString call and the PropertyPreFilter and JsonWriter.Feature.WriteNulls APIs shown in the issue. Compare the requested GraphQLResponse output with the current filter-based result, then determine whether an existing non-filter configuration supports selective top-level null omission while retaining nulls inside data. Done means the behavior is documented or supported and the example serializes as shown.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, kotlin
- Domain
- backend
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100