jackson IOException: Can not deserialize Class com.mycompany.models.Person$Address (of type non-static member class) as a Bean

Viewed 9783

I have a class that looks like this:

public class Person {
    public class Address {
        private String line1;
        private String line2;
        private String zipCode;
        private String state;

        // standard public getters and setters for the class here
   }

private String name;
private String address;

// standard public getters and setters for the class here

}

and here’s how I am using jackson with it.

public class JsonTest {
    public static <T> Object fromJson(String jsonAsString, Class<T> pojoClass)
throws JsonMappingException, JsonParseException, IOException {
        return m.readValue(jsonAsString, pojoClass);
    }

    public static String toJson(Object pojo, boolean prettyPrint)
throws JsonMappingException, JsonGenerationException, IOException {
        StringWriter sw = new StringWriter();
        JsonGenerator jg = jf.createJsonGenerator(sw);
        if (prettyPrint) {
            jg.useDefaultPrettyPrinter();
        }
        m.writeValue(jg, pojo);
        return sw.toString();
    }

    public static void main(String[] args) {
         Person p = new Person();
         String json = this.toJson(p, true); // converts ‘p’ to JSON just fine
         Person personFromJson = this.fromJson(json, Person.class); // throws exception!!!
    }
}

the 3rd line of the main method (where I try to convert json to Person object), throws this exception:

IOException: Can not deserialize Class com.mycompany.models.Person$Address (of type non-static member class) as a Bean

what am I doing wrong?

2 Answers
Related