johnzon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Romain Manni-Bucau <rmannibu...@gmail.com>
Subject Re: User Question: Complex Adapters
Date Tue, 01 Oct 2019 14:24:06 GMT
Hi Markus,

Map does not match Map.Entry so it does not work ;)

Semantically you miss a level of "container" (a map is a kind of
List<Map.Entry> even if structurally it does not match).
Also note that Map.Entry is modelized per its signatures as a {key:...,
value: ....} and not as {key:value} as in a map.

I guess a Deserializer can be closer than an adapter of what you are trying
to do.

Romain Manni-Bucau
@rmannibucau <https://twitter.com/rmannibucau> |  Blog
<https://rmannibucau.metawerx.net/> | Old Blog
<http://rmannibucau.wordpress.com> | Github <https://github.com/rmannibucau> |
LinkedIn <https://www.linkedin.com/in/rmannibucau> | Book
<https://www.packtpub.com/application-development/java-ee-8-high-performance>


Le mar. 1 oct. 2019 à 16:17, Markus Karg <karg@quipsy.de> a écrit :

> I’d be very very glad if you could answer a short user question I’m
> completely stuck with. 😊
>
> Johnzon is able to parse JSON object into Java Map while respecting custom
> adapters:
>
> // Works well: Uses custom Adapter<UUID, String>
> Map<UUID, Foo> map = jsonb.fromJson("{ \"" + uuid + "\": { \"name\":
> \"Lala\" } }", new JohnzonParameterizedType(Map.class, UUID.class,
> Foo.class));
>
> Now let’s assume we do not want to get a complete Map but just a single
> Map.Entry, as we know for sure the JSON string only contains exactly just
> one single key-value-pair:
>
> JsonbConfig jsonbConfig = new JsonbConfig().withAdapters(new
> UuidAdapter(), new MapEntryAdapter<UUID, Foo>());
>
> // Fails with "Can't map Entry<UUID, Foo>"
> Map.Entry<UUID, Foo> entry = jsonb.fromJson("{ \"" + uuid + "\": {
> \"name\": \"Lala\" } }", new JohnzonParameterizedType(Map.Entry.class,
> UUID.class, Foo.class));
>
> public class MapEntryAdapter<K, V> implements JsonbAdapter<Map.Entry<K,
> V>, Map<K, V>> {
>     @Override public Map<K, V> adaptToJson(Entry<K, V> obj) throws
> Exception { … }
>     @Override public Entry<K, V> adaptFromJson(Map<K, V> obj) throws
> Exception { … }
> }
>
> Strange but true, this does not work but complains it cannot map
> Entry<UUID, Foo> -- it seems it cannot „see“ the correctly registered
> adapter (not even if I replace <K, V> by <UUID, String> manually in the
> code).
>
> So the question is: Where is the fault? Did I try something (what?) which
> is forbidden in JSON-B? Is this a bug in Johnzon?
>
> Actually I assume it is my personal fault (as so often), but I just cannot
> see where… 😉
>
> Thanks a lot for your kind help!
> -Markus
>
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message