-
Notifications
You must be signed in to change notification settings - Fork 3
Jackson3 Changes JsonNode
As of Jackson 2.x, only some of DeserializationFeatures (and few if any SerializationFeatures) affect handling of JsonNode. This is due to most of them being POJO-centric, as JsonNode is meant to be faithful representation of JSON that was read, or is to be written out, with few changes.
But users do have legitimate need/desire to make SOME changes. It's just that these changes are not necessarily aligned with changes to POJO handling.
With that, I suggest addition of...
This would be enumeration like DeserializationFeature with choices like:
- READ_NULL_PROPERTIES (default: true) -- are
nullvalued properties represented asNullNodes in resultingJsonObjector skipped? - WRITE_NULL_PROPERTIES (default: true) -- are
nullvalued fields inObjectNodewritten out as JSON or skipped? - READ_NULL_ELEMENTS (default: true) -- are
nullelements in JSON arrays represented asNullNodes in resultingJsonArrayor skipped? - WRITE_NULL_ELEMENTS (default: true) -- are
nullelements inArrayNodewritten out as JSON or skipped? - CONVERT_POJOS_FULLY (default: ?) -- when converting values (mapper.convertValue(), treeToValue()), are opaque values contained in
POJONode:- Serialized explicitly by matching serializer (true) -- which will essentially transform it into non-opaque value (like Map, List etc)
- Written out as "writePOJO", which in case of
TokenBufferwill retain opaque value exactly as is
One thing to note is that while there could be NodeReadFeature/NodeWriteFeature, I think set of values we need is small enough, and things closely related so that it makes more sense to have just one set. It is also bit easier to implement.
As per
https://github.com/FasterXML/jackson-databind/issues/2176
there is finally a globally shared "default" ObjectMapper available, and this allows some things that were not formerly available:
-
JsonNode.toString()can use real, non-duplicated, full serialization functionality!- before this change, I was planning to actually remove ability altogether since output of 2.x is NOT guaranteed to be valid JSON, and much of functionality from mapper-originated serialization (
ObjectMapper.writeValueAsString()is simply not available -- and cost of instantiating mapper (or heaven help,ThreadLocal...) is too expensive for such usage - but now
toString()can just returnJsonMapper.shared().writeValueAsString() - also see section about exception reporting: we will be able to allow full error reporting, too
- before this change, I was planning to actually remove ability altogether since output of 2.x is NOT guaranteed to be valid JSON, and much of functionality from mapper-originated serialization (
With 3.0, plan is to change JsonProcessingException to be unchecked:
This is relevant for JsonNode, too, since we can start throwing formal Jackson exceptions, without having to declare them separately for all (or just some) accessors.
This is relevant for a few entries here:
-
JsonNode.toString()can pass any exceptions for real serializationn as-is, with no additional wrapping
However: I think it also makes sense to introduce at least one new Jackson exception type, like:
ValueCoercionException
which can then be used by methods that attempt coercion (like JsonNode.asIntValue()), but fail due to incompatible types (or perhaps parsing error, from String to number).
This exception type should retain and expose information like:
- Source token type (
JsonToken.VALUE_STRING) - Target shape (as token, like
JsonToken.VALUE_NUMBER_INT) - If available, target Java type (
java.lang.Integer#typeforint, for example) - We do not necessarily have information on path, but possibly exception catch/rethrow could re-create this
- For
ContainerNode(JsonObject,JsonArray):-
removeNullValues()(or: separate for object ("removeNullProperties()") vs array ("removeNullElements")?) -
clear()(remove all entries)
-
isEmpty()
It should be possible to stream through both values (array elements, values of properties), and in case of JsonObject, entries (name and value). Not sure what is the best Java 8 pattern to follow, perhaps take consumers?
Maybe allow a way to "map"? Might become problematic for JsonObject, if and when modifying while traversing: maybe should just create a new JsonObject / JsonArray if and when making any change(s)?
Will definitely need help defining good Java 8 API additions here