Hello,
I have been using Jackson for a long time now and I have submitted
patches to it, some of which were blunders, I must admit.
For what it's worth, I am the developper of json-schema-validator
(
https://github.com/fge/json-schema-validator) which uses jackson at
its core; and since this API is all about JSON and not POJOs, I am a
heavy user of JsonNode.
What bothers me at the moment is two things:
* JsonNode is not immutable;
* short of a JsonParser, you cannot really walk through it...
What I needed was a "tree walker", so I created one:
@Beta
public interface JsonTreeVisitor<T>
{
JsonTreeVisitResult visitNode(final JsonPointer ptr, final JsonNode node,
final ValidationMessage message)
throws JsonSchemaException;
JsonTreeVisitResult preVisitArray(final JsonPointer ptr,
final JsonNode node, final Collection<JsonPointer> children,
final ValidationMessage message)
throws JsonSchemaException;
JsonTreeVisitResult postVisitArray(final JsonPointer ptr,
final ValidationMessage message);
JsonTreeVisitResult preVisitObject(final JsonPointer ptr,
final JsonNode node, final Collection<JsonPointer> children,
final ValidationMessage message)
throws JsonSchemaException;
JsonTreeVisitResult postVisitObject(final JsonPointer ptr,
final ValidationMessage message)
throws JsonSchemaException;
}
OK, forget about ValidationMessage and JsonSchemaException, this part
is specific; the two important parts here are:
* JsonPointer: this is my own implementation of, well, JSON Pointer,
which adapts to all tree nodes but which Tatu declinded for reasons I
don't understand ;)
* Collection<JsonPointer>: this is a Collection into which the visitor
can push paths it _wants_ to be visited afterwards and no others.
But I'll be using something like this in the next version of
json-schema-validator; right now though, as annotated, it's @Beta.
Note also that it requires that the FULL TREE be available, that is,
the full JSON. With JSON Schema, you cannot reliably decide on a
success or failure before the full tree is available... As such, this
is not meant to be applied to a JsonParser.
But it may be applied in some way to Jackson; in a "waiting manner",
visitors can collect what nodes they want to visit behind the node
they are currently on (the prt argument, always present) and be only
"woken up" when needed.
An idea throw in the wild...
One last note about JsonPointer: unlike the implementation in 2.3.x,
it is independent of the TreeNode implementation! You only have to
write a TokenResolver implementation to get it to work with _any_
TreeNode implementation -- and it is overly tested.
Have fun,
--
Francis Galiegue,
fgal...@gmail.com
JSON Schema in Java:
http://json-schema-validator.herokuapp.com