Open DeLaGuardo opened 3 years ago
@DeLaGuardo How would you expect the following object to be encoded? null
is not a valid keyword in JSON AFAIK.
Right, it is not valid but Cheshire encodes it as an empty string by default. I think throwing an exception is a good default behaviour but would be great to have some option to specify what to do with nil
keys as an option for write-value
function. That is possible right now but it requires custom NullKeySerializer implementation so the overall setup become a bit bloated.
Example:
class NullKeySerializer extends StdSerializer<Object> {
public NullKeySerializer() {
this(null);
}
public NullKeySerializer(Class<Object> t) {
super(t);
}
@Override
public void serialize(Object nullKey, JsonGenerator jsonGenerator, SerializerProvider unused)
throws IOException, JsonProcessingException {
jsonGenerator.writeFieldName("");
}
}
(.setNullKeySerializer
(.getSerializerProvider j/default-object-mapper)
(NullKeySerializer.))
I'm getting an exception trying to write to string a map with nil as a key.
I tried to set :encode-key-fn option but with the same result. Looks like my function is not executed at all.