On Wed, Sep 12, 2018 at 10:03 PM jeff walker <[email protected]> wrote:
>
> Say I have bean classes defined thus:
>
> public class Top {
>   public List<Bottom> bottoms;
> }
>
> public class Bottom {
>   public String foo;
> }
>
> And serialized form:
>
> {
>   "bottoms" = [ {"foo" = "1"}, {"foo" = "2"} ]
> }
>
>
> The ObjectMapper will figure out that it needs to construct Bottom objects to 
> place into the bottoms property. But, how? I'm browsing the ObjectReader 
> code, looking at CollectionDeserializer, but I'm getting lost in the details 
> of the implementation. How does it ultimately figure out the value 
> deserializer?

Value deserializer is either constructed and passed to
CollectionDeserializer by DeserializerFactory if (and only if) defined
by annotation on Collection type.
But if not, it is fetched during `createContextual()`, based on fully
resolved `JavaType` in effect: `SerializerProvider` (context object)
is called to fetch value deserializer, and that is used to deserialize
from JSON Array (or with some settings, from single value).

Whoever is calling CollectionDeserializer will then use value in some
way: for other Maps, Collections, it gets put() or add()ed, but for
POJOs,
BeanDeserializer will call matching `SettableBeanProperty` that was
matched based on property name. Or to be precise,
`SettableBeanProperty` is found based on name and contains value
deserializer to use, calls it, and invokes Reflection-based entity
(Field, Method etc).
There is some complexity for special case of constructor parameters,
which must be buffered and passed as an array.

> Some background that probably isn't required, but in case someone is 
> interested. I'm trying to write a module that will, if a property name isn't 
> found, it will look for a method to call, passing it the deserialized array 
> or object. Also, if it's an array and the method takes a singleton object, 
> call the method multiple times. If the method takes a collection, pass it 
> directly, much like a normal setter.
>
> I'm trying to keep the JSON as clean as possible, but I may end up doing 
> something like
> {"@class": "<fully qualified name>"}
>
> if I need to. I'm also trying to keep the bean classes totally agnostic to 
> this process, providing only the method needed.
>
> I would mostly use this for Hibernate entity classes that need an "add" 
> method to keep one-to-many type relationships in sync. But, I can imagine 
> some more general uses of this as well.
>
> So:
> public class Top {
>   public List<Bottom> bottoms;
>
>   public void addBottom(Bottom bottom) {
>     bottoms.add(bottom);
>     bottom.top = this;
>   }
> }
>
> public class Bottom {
>   public String fool
>   public Top top;
> }
>
>
>
>
> and JSON something like:
>
> { "@addBottom" : [ {"foo" = "1"}, {"foo" = "2"} ] }
>
>
> I understand the pitfalls and language limitations involved, so If I can't 
> achieve this level transparency, I'll punt and add more metadata to JSON.
>
> Also, if anyone has done this before/better, I'd be interested, but I haven't 
> found anything like this in my searching.

Good luck! It sounds quite challenging, considering the way things
work but I have usually been proven wrong when I say something is
impractical

-+ Tatu +-

-- 
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.

Reply via email to