-1

I have a java object as given below. How to serialize/deserialize with Jackson json?

public class Employee {

     private String name;
     List<Employee> friends;
}

The JSON:

{"friends":[{"name":"abc"}],[{{"name":"pqr"}}]}

My Implementation class:

public class EmployeeImpl implements Employee, Serializable { 

    private String name;
    private List<Employee> friends;

    public String getName() { return name; }

    public void setName(String name) { this.name = name; }

    public List<Employee> getFriends() { return friends; }

    public void setFriends(List<Employee> friends) { this.friends = friends; } 
}

Test Class:

public class Test { 

    public static void main(String[] args) throws Exception {

        String json = "{\"name\":\"gangi\", \"friends\":[{\"name\":\"abc\"},{\"name\":\"pqr\"}]}";
        Employee employee = deserializeJson(json, new TypeReference<EmployeeImpl>(){});
    }

    public static <T> T deserializeJson(String jsonData, TypeReference<T> typeRef) throws Exception {

        ObjectMapper mapper = new ObjectMapper();
        return mapper.<T>readValue(jsonData, typeRef);
    }
}

Exception stacktrace...

Exception in thread "main" org.codehaus.jackson.map.JsonMappingException: 
Can not construct instance of Employee, 
problem: abstract types can only be instantiated with additional type information at 
[Source: java.io.StringReader@68da4b71; line: 1, column: 29] 
(through reference chain: EmployeeImpl["friends"]) at 
org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163‌​)
Programmer Bruce
  • 64,977
  • 7
  • 99
  • 97
  • 1
    Please format code and do some research. Google is your friend. – Cole Tobin Jul 22 '12 at 16:22
  • If json isn't really your strong suit, I'd recommend building your objects, serializing them and inspecting the json. That way you know what you should provide for deserialization. Also you might want to show what you've attempted, that didn't work. Showing effort is good, and it allows people to explain what you're doing wrong. – Gaute Løken Jul 22 '12 at 16:31
  • A question, why do you have a base class Employee? You are not utilizing the declarations of base class also, you are redeclaring the same same fields in the sub-class. – devang Jul 23 '12 at 00:30

2 Answers2

3

Add following annotation to your Employee interface

`@JsonDeserialize(as=EmployeeImpl.class)`
devang
  • 5,376
  • 7
  • 34
  • 49
  • Thank you very much!! That works, but the problem is that referencing implementation class name in the interface makes me worry... In fact my interface would live outside of my project. Thanks for your quick help! – Prabhakara Gangi Jul 23 '12 at 00:04
0

Your json format is incorrect. See [http://www.json.org/] on how to define array/lists in json. Example for json building [http://java.dzone.com/tips/json-processing-using-jackson]

devang
  • 5,376
  • 7
  • 34
  • 49
  • String json = "{\"name\":\"gangi\", \"friends\":[{\"name\":\"abc\"},{\"name\":\"pqr\"}]}"; – Prabhakara Gangi Jul 22 '12 at 16:44
  • I would like to know the approach basically? – Prabhakara Gangi Jul 22 '12 at 16:45
  • My Implementation class: public class EmployeeImpl implements Employee, Serializable { private String name; private List friends; public String getName() { return name; } public void setName(String name) { this.name = name; } public List getFriends() { return friends; } public void setFriends(List friends) { this.friends = friends; } } – Prabhakara Gangi Jul 22 '12 at 17:14
  • Test Class: public class Test { public static void main(String[] args) throws Exception { String json = "{\"name\":\"gangi\", \"friends\":[{\"name\":\"abc\"},{\"name\":\"pqr\"}]}"; Employee employee = deserializeJson(json, new TypeReference(){}); } public static T deserializeJson(String jsonData, TypeReference typeRef) throws Exception { ObjectMapper mapper = new ObjectMapper(); return mapper.readValue(jsonData, typeRef); } } – Prabhakara Gangi Jul 22 '12 at 17:16
  • Exception stacktrace.... Exception in thread "main" org.codehaus.jackson.map.JsonMappingException: Can not construct instance of Employee, problem: abstract types can only be instantiated with additional type information at [Source: java.io.StringReader@68da4b71; line: 1, column: 29] (through reference chain: EmployeeImpl["friends"]) at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163) – Prabhakara Gangi Jul 22 '12 at 17:21
  • In your EmployeeImpl POJO, change List to List . – devang Jul 22 '12 at 17:31
  • If you want to use abstract classes see [here](http://stackoverflow.com/questions/5489532/jackson-json-library-how-to-instantiate-a-class-that-contains-abstract-fields) – devang Jul 22 '12 at 17:35