Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serialize an object with no data in Jackson

Is it possible to serialize an object with no fields in Jackson using only annotations? When I attempt to serialize such an object with no annotations I get:

Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class [redacted].SubjectObjectFeatureExtractor and no properties discovered to create BeanSerializer 

I have examined the list of Jackson annotations without seeing a way to annotate the class as having no serializable data. I tried putting @JsonCreator on the empty constructor (not expecting it to work, since it's a deserialization annotation), and I got the same error. There are no accessors or fields to put @JsonProperty on. Any ideas?

Update: The reason for this is that I have a list of objects which represent transformations which can be applied to a certain type of data. Some of these transformations are defined by parameters which needs to be serialized, but some of these are parameter-less (the data-less objects in question). I'd like to be able to serialize and deserialize a sequence of these transformations. Also, I'm using DefaultTyping.NON_FINAL so that the class name will be serialized.

Update: An example class would be

class ExtractSomeFeature implements FeatureExtractor<SomeOtherType> {
    public void extractFeature(SomeOtherType obj, WeightedFeatureList output) {
          // do stuff
    }
 }

I don't particularly care how the JSON for this looks like, as long as I can deserialize List<FeatureExtractor>s properly. My impression is that using default typing, the expected JSON would be something like:

['com.mycompany.foo.ExtractSomeFeature', {}]

Other sub-classes of FeatureExtractor would have real parameters, so they would presumably look something like:

[`com.mycompany.foo.SomeParameterizedFeature', {some actual JSON stuff in here}]

I think I could use @JsonValue on some toJSONString() method to return {}, but if possible I'd like to hide such hackery from end-users who will be creating FeatureExtractor sub-classes.

like image 879
Ryan Gabbard Avatar asked Jan 21 '14 00:01

Ryan Gabbard


People also ask

How does Jackson serialize null?

Serialize Null Fields Fields/PropertiesWith its default settings, Jackson serializes null-valued public fields. In other words, resulting JSON will include null fields. Here, the name field which is null is in the resulting JSON string.

Does Jackson require serializable?

Note that Jackson does not use java. io. Serializable for anything: there is no real value for adding that. It gets ignored.

How do I ignore null values in Jackson?

In order to ignore null fields at the class level, we use the @JsonInclude annotation with include. NON_NULL. Let's take an example to understand how we can use @JsonInclude annotation to ignore the null fields at the class level.


1 Answers

You have to configure your object mapper to support this case.

ObjectMapper objectMapper = ...
  objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);

The documentation of this feature can be found here : 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). If enabled (default), an exception is thrown to indicate these as non-serializable types; if disabled, they are serialized as empty Objects, i.e. without any properties.

like image 94
wmlynarski Avatar answered Sep 29 '22 22:09

wmlynarski