On Fri, Jul 15, 2016 at 5:06 AM, <[email protected]> wrote: > OK. > I'm naively just calling findRootValueDeserializer() when I need to > deserialize an object, which appears to work.I don't know where I'd > construct a BeanProperty from to call the findContextual method. >
Right, BeanProperty will only be available via createContextual(), which is called when constructed (de)serializer is being associated with actual property it'll be used for, to allow for per-property configuration overrides. So either it's from that method, or an instance is constructed from createContextual() and holds a reference to be used later on. But if you do not have it, yes, just use findRootValueDeserializer() > > Not sure I understand the purpose of ContextualDeserializer in this > context. It doesn't help me create deserializers that I might want to use > does it? > The point is that locating and configuring deserializers can be somewhat expensive, so instead of adding that overhead for every time json is serialized, it is done just once when the chain of deserializers is constructed. Well that and ability to check for per-property annotations like `@JsonFormat`; otherwise they will have no effect. > > Is there a good guide somewhere on how to do this? > I am not aware of anything. I have had the tentative plan to write a longer "How to write perfect (de)serializer" series for years now, but haven't found time & motivation to do that. Maybe one day. There are some articles on writing custom serializers/deserializers, and if anyone can recommend good ones I'll add links to from `jackson-docs` github repo. -+ Tatu +- > > Thanks. > > On Thursday, 14 July 2016 21:01:56 UTC+1, Tatu Saloranta wrote: >> >> Yes, for delegation you would retrieve a JsonDeserializer via >> DeserializationContext. >> >> However, for performance reasons this is most often done by implementing >> ContextualDeserializer, and handling lookups from method >> `createContextual()` which is called once after construction. This method >> can access annotations, fetch deserializers to delegate, and then construct >> a new instance with settings (it should NOT modify instance itself, as it >> may be shared among multiple properties). >> >> Method to call depends on situation; most likely it is one of: >> >> * findContextualValueDeserializer(): handles contextualization (as name >> implies), requires `BeanProperty` (which is passed to `createContextual()` >> * findRootValueDeserializer(): called when this is the initial root >> deserializer; also wraps TypeDeserializer for polymorphic types >> >> -+ Tatu +- >> >> >> On Thu, Jul 14, 2016 at 8:39 AM, <[email protected]> wrote: >> >>> So I'm writing a custom deserializer, extending StdDeserializer<T>. >>> >>> I want the JSON structure to be slightly different to "normal", hence >>> needing my own serializer and deserializer. >>> >>> Within the serializer I just call writeObjectField when I want to >>> serialize the properties of my object, but I can't work out what the >>> reverse of that is. >>> >>> So within my deserializer I want to be able to deserialize a value of >>> given Class. So what I *think* I want to do is acquire a JsonDeserializer >>> for a given Class from the DeserializationContext I have access to, but I >>> can't work out how to do that. >>> >>> I've looked through some of the code, but I can't find anything that >>> points me in the right direction. >>> >>> Any help would be appreciated. >>> Thanks. >>> >>> >>> -- >>> 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. > -- 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.
