0

i tried to visit every SO thread related to this but i didn't succeed to understand source of my problem,

i'm working with Maps of <String,Objects> as demonstrated below, i used TypeToken as shown here in Gson ReadMe to capture Type for Runtime, i've prepared two test method for demonstrating my problem, i think two tests have the same semantics but doesn't produce the same results ,

test2 will generate some LinkedTreeMap and result in NPE, while test1 succeed

// my Model

public static class DataType implements Distinguishable, Serializable {
        private final String id;
        private final int i;
        public DataType(String id, int i){
            this.id = id;
            this.i = i;
        }
        @Override
        public String getID() {
            return id;
        }

        public String getId() {
            return id;
        }

        public int getI() {
            return i;
        }
    }


// test 1:

@Test
    public void shouldSerAndDesAMap(){

        // manual
        Type type = new TypeToken<Map<String,DataType>>(){}.getType();


        Map<String,DataType> map = new HashMap<String,DataType>(){{
            put("s1",new DataType("s1",1));
            put("s2",new DataType("s2",2));
        }};

        String s = g.toJson(map,type);
        Map<String,DataType> m = g.fromJson(s,type);

        System.out.println(m.get("s1").getI());
    }


// test 2 (and its utils):

    Gson g = new GsonBuilder().setPrettyPrinting().create();

    private <T extends Distinguishable> String serialize(Map<String,T> map){
        Type type = new TypeToken<Map<String,T>>(){}.getType(); // generating TypeToken 
        return g.toJson(map,type);
    }

    private <T extends  Distinguishable> Map<String,T> deserialize(String s){
        Type type = new TypeToken<Map<String,T>>(){}.getType();
        return g.fromJson(s,type);
    }

    @Test
    public void shouldSerAndDesByGenerics(){
        Map<String,DataType> map = new HashMap<String,DataType>(){{
            put("s1",new DataType("s1",1));
            put("s2",new DataType("s2",2));
        }};

        String s = serialize(map);
        System.out.println(s);
        Map<String,DataType> m = deserialize(s);

        System.out.println(m.get("s1").getI());
    }

if i'm still facing Java type Erasure how can i overcome this? i need to generate a generic class with TypeToken as its field while i cant ask user to inject it in,

nort3x
  • 11
  • 1
  • 2

1 Answers1

0

a friend pointed me to this

private <T extends Distinguishable> String serialize(Map<String,T> map, Class<T> clazz){
        Type type = TypeToken.getParametrized(Map.class,String.class,clazz) // generating TypeToken 
        return g.toJson(map,type);
    }

this way i can achieve what i need,

nort3x
  • 11
  • 1
  • 2