2

I have arrays like these.

{
    ImplArray1:[
        {
            ...
        }
        , ...
    ],
    ImplArray2:[
        {
            ...
        }
        , ...
    ]
}

which both Impl1 and Impl2 implement same interface.

How can I deserialize both arrays into two List< Interface>?

Edited : My god. The question doesn't make sense at all. Really sorry for being bad at life.

What I mean is, an array ImplArray1 contains Impl1, and array ImplArray2 contains Impl2. Both Impl1 and Impl2 implement same interface. How can I deserialize both into List< Interface1>?

2
  • you mean Impl1 and Impl2 both implement same Interface? Commented Jul 7, 2015 at 9:54
  • maybe this answer helps, serializing/deserializing stackoverflow.com/a/31022354/928952 Commented Jul 7, 2015 at 9:55

1 Answer 1

2

The only way I managed to pull something that is close to the requirement, is if you have a super class that is extended by both implementations. The super class may implement the common interface

ObjectMapper om = new ObjectMapper();
TypeFactory tf = om.getTypeFactory();

// map key type
JavaType stringType = tf.constructType(String.class);
// map value type
JavaType listOfSuper = tf.constructParametrizedType(List.class, List.class, Super.class);
// map type
JavaType mapType = tf.constructMapType(Map.class, stringType, listOfSuper);
// finally, do the parsing
Reader fr = new FileReader("C://Temp/xx.json");
Map<String, List<Interface>> map = (Map<String, List<Interface>>)om.readValue(fr, mapType);

static interface Interface  
{
}

static class Super implements Interface 
{
    public String name = "";
    public int age = 0;
}

static class Impl1 extends Super 
{
}

static class Impl2 extends Super
{
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.