jansupol / jsonbapi

0 stars 0 forks source link

Adapter not working for Map key #77

Open jansupol opened 6 years ago

jansupol commented 6 years ago

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 commented 6 years ago
jansupol commented 6 years ago

@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