Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serializing with Jackson (JSON) - getting "No serializer found"?

Tags:

java

json

jackson

I get the an exception when trying to serialize a very simple object using Jackson. The error:

org.codehaus.jackson.map.JsonMappingException: No serializer found for class MyPackage.TestA and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) )

Below is the simple class and code to serialize.

Can anyone tell my why I get this error?

public class TestA {     String SomeString = "asd"; }  TestA testA = new TestA(); ObjectMapper om = new ObjectMapper(); try {     String testAString = om.writeValueAsString(testA); // error here!      TestA newTestA = om.readValue(testAString, TestA.class); } catch (JsonGenerationException e) {     // TODO Auto-generated catch block     e.printStackTrace(); } catch (JsonMappingException e) {     // TODO Auto-generated catch block     e.printStackTrace(); } catch (IOException e) {     // TODO Auto-generated catch block     e.printStackTrace(); } 
like image 646
Ted Avatar asked Dec 03 '11 11:12

Ted


People also ask

Does Jackson need serializable?

It requires the class to have a default no-args constructor to instantiate Java Object from JSON string. If no-args constructor is not provided, it will throw JsonMappingException . Unlike Gson, Jackson will require getters for all private fields, otherwise serialization and deserialization won't work.

What is Jackson object serialization?

Jackson is a solid and mature JSON serialization/deserialization library for Java. The ObjectMapper API provides a straightforward way to parse and generate JSON response objects with a lot of flexibility.

What is Fail_on_empty_beans?

FAIL_ON_EMPTY_BEANS. public static final SerializationFeature FAIL_ON_EMPTY_BEANS. Feature that determines what happens when no accessors are found for a type (and there are no annotations to indicate it is meant to be serialized).

What is JSON mapping exception?

public class JsonMappingException extends JsonProcessingException. Checked exception used to signal fatal problems with mapping of content, distinct from low-level I/O problems (signaled using simple IOException s) or data encoding/decoding problems (signaled with JsonParseException , JsonGenerationException ).


1 Answers

As already described, the default configuration of an ObjectMapper instance is to only access properties that are public fields or have public getters/setters. An alternative to changing the class definition to make a field public or to provide a public getter/setter is to specify (to the underlying VisibilityChecker) a different property visibility rule. Jackson 1.9 provides the ObjectMapper.setVisibility() convenience method for doing so. For the example in the original question, I'd likely configure this as

myObjectMapper.setVisibility(JsonMethod.FIELD, Visibility.ANY); 

For Jackson >2.0:

myObjectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY); 

For more information and details on related configuration options, I recommend reviewing the JavaDocs on ObjectMapper.setVisibility().

like image 138
Programmer Bruce Avatar answered Sep 28 '22 09:09

Programmer Bruce