johnzon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Romain Manni-Bucau (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (JOHNZON-213) Make @JsonbTypeAdapter works with accessors
Date Fri, 14 Jun 2019 10:23:00 GMT

    [ https://issues.apache.org/jira/browse/JOHNZON-213?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16863921#comment-16863921
] 

Romain Manni-Bucau commented on JOHNZON-213:
--------------------------------------------

Hi [~jinahya], didn't you forget to set the adapter on the setter? This test passes for me:

{code}

    @Test
    public void jira_JOHNZON213() throws Exception {
        try (final Jsonb jsonb = JsonbBuilder.create()) {
            final Child child = new Child();
            child.options = new SomeOptions("ok");
            final String json = jsonb.toJson(child);
            assertEquals("{\"options\":\"ok\"}", json);
            final Child deserialized = jsonb.fromJson(json, Child.class);
            assertNotNull(deserialized.options);
            assertEquals("ok", deserialized.options.value);
        }
    }

    public static class Base {
    }

    public static class SomeOptions extends Base {
        private final String value;

        public SomeOptions(final String value) {
            this.value = value;
        }

        @Override
        public String toString() {
            return value;
        }
    }

    public static class Parent<T extends Base> {

        protected T options;
    }

    public static class Child extends Parent<SomeOptions> {

        @JsonbTypeAdapter(SomeOptionsTypeAdapter.class)
        public SomeOptions getOptions() {
            return options;
        }

        @JsonbTypeAdapter(SomeOptionsTypeAdapter.class)
        public void setOptions(final SomeOptions options) {
            this.options = options;
        }
    }

    public static class SomeOptionsTypeAdapter implements JsonbAdapter<SomeOptions, String>
{

        @Override
        public String adaptToJson(final SomeOptions obj) {
            return obj.toString();
        }

        @Override
        public SomeOptions adaptFromJson(final String obj) {
            return new SomeOptions(obj);
        }
    }
{code}

> Make @JsonbTypeAdapter works with accessors
> -------------------------------------------
>
>                 Key: JOHNZON-213
>                 URL: https://issues.apache.org/jira/browse/JOHNZON-213
>             Project: Johnzon
>          Issue Type: Bug
>          Components: JSON-B
>    Affects Versions: 1.1.12
>            Reporter: Jin Kwon
>            Priority: Major
>
> For some reasons, I need to annotate an overridden method with an {{@JsonbTypeAdapter}}.
> {code:java}
> class Parent<T extends ...> {
>     T options;
> }
> class Child extends Parent<SomeOptions> {
>     @JsonbTypeAdapter(value = SomeOptionsTypeAdapter.class)
>     public SomeOptions getOptions() {
>         return super.getOptions();
>     } 
> }
> {code}
> Now Johnzon complains.
> {code}
> javax.json.bind.JsonbException: Missing a Converter for type class ...Params$Options
to convert the JSON String '{"..."}' . Please register a custom converter for it.
> Caused by: org.apache.johnzon.mapper.MapperException: Missing a Converter for type class
...Params$Options to convert the JSON String '{"..."}' . Please register a custom converter
for it.
> {code}



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Mime
View raw message