Home  >  Forum  >  Core Java
Post New Query

No ObjectCodec defined for the generator, can only serialize simple wrapper type



Asked on February 27, 2015
I am new to Jackson API. While using Jackson JsonGenerator, I am getting below error. I am using code as below. 

JsonGenerator jsonGen = jsonFactory.createJsonGenerator(fos, JsonEncoding.UTF8);
Person person = new Person(10, "ABCD", address);
jsonGen.writeObject(person);

But getting error.

Exception in thread "main" java.lang.IllegalStateException: No ObjectCodec defined for the generator, can only serialize simple wrapper types (type passed com.concretepage.Person)
at org.codehaus.jackson.impl.JsonGeneratorBase._writeSimpleObject(JsonGeneratorBase.java:556)
at org.codehaus.jackson.impl.JsonGeneratorBase.writeObject(JsonGeneratorBase.java:317)



Replied on February 28, 2015
You need to set ObjectCodec in JsonGenerator.  ObjectMapper is an ObjectCodec because it  is extended by ObjectMapper. Add the line of code as 

jsonGen.setCodec(new ObjectMapper());
jsonGen.writeObject(person);



Replied on March 01, 2015
Class ObjectCodec java.lang.Object extended by org.codehaus.jackson.ObjectCodec Direct Known Subclasses: ObjectMapper public abstract class ObjectCodec extends Object Abstract class that defines the interface that JsonParser and JsonGenerator use to serialize and deserialize regular Java objects (POJOs aka Beans). The standard implementation of this class is ObjectMapper.

Write Answer







©2019 concretepage.com | Privacy Policy | Contact Us