johnzon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Markus Karg <>
Subject User Question: Complex Adapters
Date Tue, 01 Oct 2019 14:07:36 GMT
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,

// 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!

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