On Sun, Aug 20, 2017 at 1:48 PM, Vladimir <[email protected]> wrote:
>
> Thank you for the response once again.
>
> As you suggested I am using "findNonContextualValueDeserializer" to locate
> deserializer for a given type I expect.
>
> JsonNode eventNode = jsonTree.get("event");
>
> ctxt
>
> .findNonContextualValueDeserializer(ctxt.constructType(Event.class))
>
> .deserialize(eventNode.traverse(parser.getCodec()));
>
>
> In my case event is an abstract class, I wont know exactly what concrete
> implementation its going to be unless I look at the eventNode and extract
> @class property which contains the fully qualified java class name.
>
> My question is:
> Will I have to parse out the type property specifying concrete class first?
> Then construct a JavaType from it with, and then look for the custom
> deserializer using the findNonContextualValueDeserializer(javaType).
I am not sure I understand.
If you read the whole JSON sub-tree into `JsonNode`, you can extract
out class name,
construct `JavaType` from it (via TypeFactory, or if it's simple class
name, Class.forName()),
and pass that type? There is no need to bind it into other types in between.
But if and when you need to extract more than one typed value, you can
traverse `JsonNode`
tree and take further sub-trees. There is no need to try to make the
whole thing map anything unless doing
that simplifies handling.
>
> Ultimately I am trying to achieve the same dispatching behavior in
> deserializers as Jackson has in the custom serializers. In the serializer
> example below all I have to do is gen.WriteObjectField("fieldName",
> someObject), and Jackson locates the custom serializers I have declared
> somewhere else for the someObject.
> @Override
> public void serialize(EventMessage value, JsonGenerator gen,
> SerializerProvider provider) throws IOException {
>
> gen.writeStartObject();
> gen.writeObjectField("event", value.getPayLoad());
> gen.writeObjectField("headers", value.getHeaders());
> gen.writeEndObject();
> }
>
> @Override
> public void serialize(EventMessage value, JsonGenerator gen,
> SerializerProvider provider) throws IOException {
>
> gen.writeStartObject();
> gen.writeObjectField("event", value.getPayLoad());
> gen.writeObjectField("headers", value.getHeaders());
> gen.writeEndObject();
> }
Read and write sides are not symmetrical in behavior unfortunately,
but I think that is a natural property of things.
Deserialization is more difficult than serialization.
-+ Tatu +-
>
>
>
>
>
>
>
>
> On Friday, August 18, 2017 at 4:32:56 PM UTC-4, Vladimir wrote:
>>
>>
>> I implement the ResolvableDeserializer within my custom deserializer,
>> which gives me the reference to the default JsonDeserializer<?>.
>> Id like to invoke the default deserializer on the "Jail" JsonNode to
>> return to me a Jail object
>>
>> public class BadStudentDeserializer extends StdDeserializer<BadStudent>
>> implements ResolvableDeserializer{
>> private static final long serialVersionUID = 1L;
>> private JsonDeserializer<?> defaultDeserializer;
>>
>>
>> public BadStudentDeserializer(JsonDeserializer<?> defaultDeserializer) {
>> super(BadStudent.class);
>> this.defaultDeserializer = defaultDeserializer;
>> }
>>
>> @Override
>> public BadStudent deserialize(JsonParser p, DeserializationContext ctxt)
>> throws IOException, JsonProcessingException {
>> JsonNode jsonTree = p.getCodec().readTree(p);
>>
>> String id = jsonTree.get("id").asText();
>> String weapon = jsonTree.get("weapon").asText();
>> JsonNode jailNode = jsonTree.get("jail");
>> JsonParser jailNodeParser = jailNode.traverse();
>> //advance it as per documentation
>> jailNodeParser.nextToken();
>> //this doesnt work
>> Jail jail = (Jail)defaultDeserializer.
>> deserialize(jailNodeParser, ctxt);
>> return new BadStudent(id, weapon, jail);
>> }
>>
>>
>> @Override
>> public void resolve(DeserializationContext ctxt) throws
>> JsonMappingException {
>> ((ResolvableDeserializer) defaultDeserializer).resolve(ctxt);
>>
>> }
>>
>>
>>
>>
>> How do I provide the defaultDeserializer a clue as to what class TYPE a
>> tree node is, so that it can pick up the right deserializer or that
>> treeNode?
>>
>> Hope I am able to explain myself clearly.
>>
>>
>>
> --
> You received this message because you are subscribed to the Google Groups
> "jackson-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/d/optout.