Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapter not working for Map key #77

Open
jansupol opened this issue Apr 10, 2018 · 2 comments
Open

Adapter not working for Map key #77

jansupol opened this issue Apr 10, 2018 · 2 comments

Comments

@jansupol
Copy link
Owner

I'm trying to use adapters to serialize a Map of two objects. Adapter is working fine for value but not working for the key.
Example :

public class TestJsonB {

    public static void main(String[] args) {

        TestJsonB tB = new TestJsonB();
        TestAdapter test = tB.new TestAdapter();
        Map<Type, Status> mapTest = new HashMap<>();
        mapTest.put(Type.OIDVAL, Status.ACTIVE);
        test.setMap(mapTest);

        Jsonb jsonB = SerializationUtils.getJSONBuilder(false, tB.new TypeAdapter(), tB.new StatusAdapter());
        System.out.println(jsonB.toJson(test));

    }

    class TestAdapter {
        Map<Type, Status> map;

        /**
         * @return the map
         */
        public Map<Type, Status> getMap() {
            return map;
        }

        /**
         * @param map
         *            the map to set
         */
        public void setMap(Map<Type, Status> map) {
            this.map = map;
        }
    }

    enum Type {
        OIDVAL, MSISDN;
    }

    enum Status {
        ACTIVE, INACTIVE;
    }

    class TypeAdapter implements JsonbAdapter<Type, String> {

        @Override
        public String adaptToJson(Type obj) throws Exception {
            return obj.name().toLowerCase();
        }

        @Override
        public Type adaptFromJson(String obj) throws Exception {
            return Type.valueOf(obj);
        }

    }

    class StatusAdapter implements JsonbAdapter<Status, String> {

        @Override
        public String adaptToJson(Status obj) throws Exception {
            return obj.name().toLowerCase();
        }

        @Override
        public Status adaptFromJson(String obj) throws Exception {
            return Status.valueOf(obj);
        }

    }

}

Output is :

{"map":{"OIDVAL":"active"}}
@jansupol
Copy link
Owner Author

@m0mus Commented
Sorry, didn't look what project is it. It looks like a bug in Yasson. I don't see that it's related to the spec. Can you please submit this issue to Yasson issues tracker here: https://github.com/eclipse-ee4j/yasson/issues

@jansupol
Copy link
Owner Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant