Why I'm Not Able to Unwrap and Serialize a Java Map Using the Jackson Java Library

Why I'm not able to unwrap and serialize a Java map using the Jackson Java library?

@JsonUnwrapped doesn't work for maps, only for proper POJOs with getters and setters. For maps, You should use @JsonAnyGetter and @JsonAnySetter (available in jackson version >= 1.6).

In your case, try this:

@JsonAnySetter 
public void add(String key, String value) {
map.put(key, value);
}

@JsonAnyGetter
public Map<String,String> getMap() {
return map;
}

That way, you can also directly add properties to the map, like add('abc','xyz') will add a new key abc to the map with value xyz.

How to unwrap and serialize java map in java using jackson?

You could create a getter for dataMap and serialize the dataMap instead of the entire Foo instance.

mapper.writeValueAsString(myFOOObject.getDataMap());

Another method is using the @JsonUnwrapped annotation. This annotation is available in Jackson 1.9+.

http://jackson.codehaus.org/1.9.9/javadoc/org/codehaus/jackson/annotate/JsonUnwrapped.html

The downside of using this annotation is the inability to use maps as stated in the answer to your other question

Jackson HashMap, ignore map name when writing to String

Why I'm not able to unwrap and serialize a Java map using the Jackson Java library?

Quoting relevant Answers here

@JsonUnwrapped doesn't work for maps, only for proper POJOs with getters and setters. For maps, You should use @JsonAnyGetter and @JsonAnySetter (available in jackson version >= 1.6).

In your case, try this:

@JsonAnySetter 
public void add(String key, String value) {
map.put(key, value);
}

@JsonAnyGetter
public Map<String,String> getMap() {
return map;
}

That way, you can also directly add properties to the map, like add('abc','xyz') will add a new key abc to the map with value xyz.

There is currently an an open issue at the Jackson project to allow support for @JsonUnwrapped on Maps. It is not tagged as being in the upcoming 2.10 or 3.x versions of Jackson, so it does not look like it's on the near-term feature roadmap.

Until this feature is supported, the workaround about using @JsonAnySetter/@JsonAnyGetter appears to be the way to go.

Serialize Map as properties of parent object

I've used the @JsonAnyGetter annotation to achieve this. There is also a matching @JsonAnySetter annotation that does the opposite: stuff unrecognized properties in a map.

  @JsonAnyGetter
public Map<String, Object> getMap() {
return map;
}

Here's a helpful guide: https://www.baeldung.com/jackson-annotations

Jackson JSONGenerator adds newline,backlash etc and does not display the given content in formatted order

After trying for few more things I tried generator.writeRaw(event); and that worked.

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;

import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

public class Main {
public static void main(String[] args) throws IOException {
List<String> stringEvents = new ArrayList<>();
stringEvents.add("{\n" +
" isA : \"Customer\",\n" +
" name : \"Rise Against\",\n" +
" age : \"2000\",\n" +
" google:sub : \"MyValue\",\n" +
" google:sub : \"MyValue\"\n" +
"}");

JsonFactory factory = new JsonFactory();
StringWriter jsonObjectWriter = new StringWriter();
JsonGenerator generator = factory.createGenerator(jsonObjectWriter);
generator.writeStartObject();
generator.writeStringField("schema", "1.0");
generator.writeFieldName("eventList");
generator.writeStartArray();
stringEvents.forEach(event->{
try {
generator.writeRaw(event);
} catch (IOException e) {
e.printStackTrace();
}
});
generator.writeEndArray();
generator.writeEndObject();
generator.close();
System.out.println(jsonObjectWriter.toString());
}
}


Related Topics



Leave a reply



Submit