Jackson JSON Serialization without field name - java

I have a JAVA POJO which has many fields. One of the fields is Map<String, Object> for which I am using the Custom JsonSerializer as it can have many type of Objects. All I want to know is how can I avoid the Serialization of the fieldname only for this Map<String,Object> field. For all other fields in POJO, I would like to have the field name but only for this, I want to remove it.
As of now when use Jackson searlizer then I get the following output:
{
"isA" : "Human",
"name" : "Batman",
"age" : "2008",
"others" : {
"key1" : "value1",
"key2" : {
"key3" : "value3"
},
"key5" : {
"key4" : "One",
"key4" : "Two"
}
}
}
I want to get the following output: (All I want to do is remove the Map<String,Object> field name but keep its children.)
{
"isA" : "Human",
"name" : "Batman",
"age" : "2008",
"key1" : "value1",
"key2" : {
"key3" : "value3"
},
"key5" : {
"key4" : "One",
"key4" : "Two"
}
}
Following is my Human.class POJO which is used by ObjectMapper:
#JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, visible = true, property = "isA")
#JsonInclude(JsonInclude.Include.NON_NULL)
#JsonIgnoreProperties(ignoreUnknown = true)
#Getter
#Setter
#NoArgsConstructor
#ToString
class Human {
private String isA;
private String name;
private String age;
#JsonSerialize(using = MyCustomSearlize.class)
private Map<String, Object> others = new HashMap<>();
}
Following is my Custom searlizer which is used by MAP during searlization:
class MyCustomSearlize extends JsonSerializer<Map<String, Object>> {
private static final ObjectMapper mapper = new ObjectMapper();
#Override
public void serialize(Map<String, Object> value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeStartObject();
recusiveSerializer(value, gen, serializers);
gen.writeEndObject();
}
public void recusiveSerializer(Map<String, Object> value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
for (Map.Entry<String, Object> extension : value.entrySet()) {
if (extension.getValue() instanceof Map) {
//If instance is MAP then call the recursive method
gen.writeFieldName(extension.getKey());
gen.writeStartObject();
recusiveSerializer((Map) extension.getValue(), gen, serializers);
gen.writeEndObject();
} else if (extension.getValue() instanceof String) {
//If instance is String directly add it to the JSON
gen.writeStringField(extension.getKey(), (String) extension.getValue());
} else if (extension.getValue() instanceof ArrayList) {
//If instance if ArrayList then loop over it and add it to the JSON after calling recursive method
for (Object dupItems : (ArrayList<Object>) extension.getValue()) {
if (dupItems instanceof Map) {
gen.writeFieldName(extension.getKey());
gen.writeStartObject();
recusiveSerializer((Map) dupItems, gen, serializers);
gen.writeEndObject();
} else {
gen.writeStringField(extension.getKey(), (String) dupItems);
}
}
}
}
}
}
Following is my Main class:
public class Main {
public static void main(String[] args) throws JsonProcessingException {
Human person = new Human();
person.setName("Batman");
person.setAge("2008");
Map<String, Object> others = new HashMap<>();
others.put("key1", "value1");
Map<String, Object> complex = new HashMap<>();
complex.put("key3", "value3");
others.put("key2", complex);
Map<String, Object> complex2 = new HashMap<>();
List<String> dup = new ArrayList<>();
dup.add("One");
dup.add("Two");
complex2.put("key4", dup);
others.put("key5", complex2);
person.setOthers(others);
final ObjectMapper objectMapper = new ObjectMapper();
SimpleModule simpleModule = new SimpleModule();
objectMapper.registerModule(simpleModule);
final String jsonEvent = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(person);
System.out.println(jsonEvent);
}
}
Following things I tried:
I tried to add #JsonValue on the Map but this will remove all my other values (name,age, isA, etc.)
I tried the #JsonAnyGetter this works for Map and String but does not work for ArrayList as I want. I am handling the ArrayList bit differently in my application as a part of my requirement.
Is there a way to may it work with #JsonSerialize and #JsonAnyGetter because I am unable to use both together.
Can someone please help in solving this issue? Please guide me to appropriate documentation or workaround thanks a lot.

From the wiki page it sounds like the #JsonUnwrapped annotation should do what you want.
#JsonUnwrapped: property annotation used to define that value should be "unwrapped" when serialized (and wrapped again when deserializing), resulting in flattening of data structure, compared to POJO structure.
The Javadoc for the class also has an example that looks appropriate.

As mentioned in another answer #JsonUnwrapped might work but I used the following approach to get it working. Posting here as it can be helpful to someone in the future:
I added the #JsonAnyGetter and #JsonSearlize on the Getter method of Map and got it to work.
#JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, visible = true, property = "isA")
#JsonInclude(JsonInclude.Include.NON_NULL)
#JsonIgnoreProperties(ignoreUnknown = true)
#Getter
#Setter
#NoArgsConstructor
#ToString
class Human {
private String isA;
private String name;
private String age;
#JsonIgnore
private Map<String, Object> others = new HashMap<>();
#JsonAnyGetter
#JsonSerialize(using = MyCustomSearlize.class)
public Map<String,Object> getOther(){
return others;
}
}
In the MyCustomSearlize class code I removed the start and end object
#Override
public void serialize(Map<String, Object> value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
recusiveSerializer(value, gen, serializers);
}

Related

Serialize nested objects with jackson

I want to have an output like
{ Orga1: [ dep1, dep2], Orga2: [dep88, dep99], ...}
but somehow I fail to get it properly done.
I have the following structure:
#JsonSerialize(using = OrganisationSerializer.class)
public class Organisation {
String name;
private HashMap<String, Department> lstDepartments = new HashMap<>();
public List<Department> getList() {
return lstDepartments.values().stream().collect(Collectors.toList());
}
}
with the nested class
#JsonSerialize(using = DepartmentSerializer.class)
public class Department {
String name;
HashMap<String, Role4Filter> lstRole = new HashMap<>();
public List<Role4Filter> getList() {
return lstRole.values().stream().collect(Collectors.toList());
}
...
}
The major problem is that the HashMap needs to be transferred into a List which needs to be serialized. But somehow I fail to convert into a JSON properly.
My approach with
public class OrganisationSerializer extends JsonSerializer<Organisation> {
#Override
public void serialize(Organisation value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeStartObject();
gen.writeArrayFieldStart(value.name);
for (final Department item : value.getList()) {
gen.writeObject(item);
}
gen.writeEndArray();
gen.writeEndObject();
}
fails with the exception that
com.fasterxml.jackson.databind.JsonMappingException: org..Department cannot be cast to org..Organisation
Any ideas? Or is there some other annotation possible (beside the serializer?)

ObjectMapper - How to convert a Map to POJO

I am new to java and trying to learn about objectmapper. I am using it to convert a map to a pojo. The keys in the map are string and all the values are string values except one which I want to convert to a Map.
Please go through the below example code for more clearer picture.
POJO Class:
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import javax.annotation.Nonnull;
import java.util.Map;
public class POJOClass {
private String string1;
private Map<String, String> map1;
#JsonCreator
public POJOClass(#Nonnull #JsonProperty(value="String1", required = true) String string1,
#Nonnull #JsonProperty(value = "Map1", required = true) Map<String, String> map1) {
this.string1 = string1;
this.map1 = map1;
}
#Nonnull
public String getString1() {
return string1;
}
#Nonnull
public Map<String, String> getMap1() {
return map1;
}
}
Test Code:
#Test
public void testPOJOClass() {
Map<String, String> map = new HashMap<>();
map.put("String1", "string");
map.put("Map1", "{\"key1\" : \"value1\", \"key2\":\"value2\", \"key3\" : null }");
ObjectMapper mapper = new ObjectMapper();
POJOClass pojoClass = mapper.convertValue(map, POJOClass.class);
}
Exception:
java.lang.IllegalArgumentException: Can not instantiate value of type [map type; class java.util.LinkedHashMap, [simple type, class java.lang.String] -> [simple type, class java.lang.String]] from String value ('{"key1" : "value1", "key2":"value2", "key3" : null }'); no single-String constructor/factory method
at [Source: N/A; line: -1, column: -1]
at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:3286)
at com.fasterxml.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:3212)
Variant Options Tried:
I know that I can keep map1 field also as a String and later on convert it into map using another instance of object mapper, but I want to avoid it. Is there any way to directly convert the string in the test code to the mentioned Pojo directly.
I even tried changing the type of map1 from Map to Map but even that didn't work.
public class CustomerDeserializer extends JsonDeserializer<Map<String, String>> {
#Override
public Map<String, String> deserialize(final JsonParser p, final DeserializationContext ctxt) throws IOException, JsonProcessingException {
final ObjectMapper mapper = (ObjectMapper) p.getCodec();
return mapper.readValue(p.getText(), new TypeReference<Map<String, String>>() {
});
}
}
You can write a customer deserializer, annotate JsonDeserialize on map1 parameter in your constructor
#JsonDeserialize(using = CustomerDeserializer.class)
#JsonProperty(value = "Map1", required = true) Map<String, String> map1) {

Jackson catch unrecognized field in a map

I'm using Jackson in a java Rest Api to handle request params.
My Bean class :
public class ZoneModifBeanParam extends ModifBeanParam<Zone> {
#FormParam("type")
private String type;
#FormParam("geometry")
private Geometry geometry;
#FormParam("name")
private String name;
...
My API interface :
#POST
#Consumes("application/json")
#Produces("application/json; subtype=geojson")
#ApiOperation(value = "Create a zone", notes = "To create a zone")
public Response createZone(ZoneModifBeanParam zoneParam) {
...
This Works fine but I need to receive other params that aren't specified by my Bean in a Map.
Example :
{
"geometry": {...},
"name": "A circle name",
"type": "4",
"hello": true
}
By receiving this I need to store in a Map (named unrecognizedFields and declared in my bean) the couple ("hello", true).
Is there any annotation or object allowing this?
Just use #JsonAnySetter. That's what it's made for. Here is a test case
public class JacksonTest {
public static class Bean {
private String name;
public String getName() { return this.name; }
public void setName(String name) { this.name = name; }
private Map<String, Object> unrecognizedFields = new HashMap<>();
#JsonAnyGetter
public Map<String, Object> getUnrecognizedFields() {
return this.unrecognizedFields;
}
#JsonAnySetter
public void setUnrecognizedFields(String key, Object value) {
this.unrecognizedFields.put(key, value);
}
}
private final String json
= "{\"name\":\"paul\",\"age\":600,\"nickname\":\"peeskillet\"}";
private final ObjectMapper mapper = new ObjectMapper();
#Test
public void testDeserialization() throws Exception {
final Bean bean = mapper.readValue(json, Bean.class);
final Map<String, Object> unrecognizedFields = bean.getUnrecognizedFields();
assertEquals("paul", bean.getName());
assertEquals(600, unrecognizedFields.get("age"));
assertEquals("peeskillet", unrecognizedFields.get("nickname"));
}
}
The #JsonAnyGetter is used on the serialization side. When you serialize the bean, you will not see the unrecognizedFields in the JSON. Instead all the properties in the map will be serialized as top level properties in the JSON.
You may be able to ignore the unrecognized fields safely by configuring the ObjectMapper, however to specifically put them as key-value pairs of a Map field, you'll need your own de-serializer.
Here's a (heavily simplified) example:
Given your POJO...
#JsonDeserialize(using=MyDeserializer.class)
class Foo {
// no encapsulation for simplicity
public String name;
public int value;
public Map<Object, Object> unrecognized;
}
... and your custom de-serializer...
class MyDeserializer extends JsonDeserializer<Foo> {
#Override
public Foo deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
// new return object
Foo foo = new Foo();
// setting unrecognized container
Map<Object, Object> unrecognized = new HashMap<>();
foo.unrecognized = unrecognized;
// initializing parsing from root node
JsonNode node = p.getCodec().readTree(p);
// iterating node fields
Iterator<Entry<String, JsonNode>> it = node.fields();
while (it.hasNext()) {
Entry<String, JsonNode> child = it.next();
// assigning known fields
switch (child.getKey()) {
case "name": {
foo.name = child.getValue().asText();
break;
}
case "value": {
foo.value = child.getValue().asInt();
break;
}
// assigning unknown fields to map
default: {
foo.unrecognized.put(child.getKey(), child.getValue());
}
}
}
return foo;
}
}
Then, somewhere...
ObjectMapper om = new ObjectMapper();
Foo foo = om.readValue("{\"name\":\"foo\",\"value\":42,\"blah\":true}", Foo.class);
System.out.println(foo.unrecognized);
Output
{blah=true}

json jackson unusual map serialization

Assume, I have the following structure:
public class SomeClass {
private String id;
#JsonProperty("key-value")
private Map<String, Object> keyValue;}
Obviously, it will be serialized to
{
"id" : "id1",
"key-value" :
{"key1" : "value1",
"key2 : "value2"}
}
Is it possible to represent it like this?
{
"id" : "id1",
"key1" : "value1",
"key2 : "value2"
}
Thanks in advance!
It is quite possible with the help of Jackson's custom serializer:
add the #JsonSerialize annoation to your POJO:
(also added necessary ctor and getters)
#JsonSerialize(using = SomeClassSerializer.class)
public static class SomeClass {
private String id;
#JsonProperty("key-value")
private Map<String, Object> keyValue;
public SomeClass(String id, Map<String, Object> keyValue) {
this.id = id;
this.keyValue = keyValue;
}
public String getId() { return id; }
public Map<String, Object> getKeyValue() { return keyValue; }
}
the custom serializer looks like this:
:
public class SomeClassSerializer extends JsonSerializer<SomeClass>
{
#Override
public void serialize(SomeClass sc, JsonGenerator gen, SerializerProvider serializers)
throws IOException, JsonProcessingException
{
gen.writeStartObject();
// write id propertry
gen.writeStringField("id", sc.getId());
// loop on keyValue entries, write each as property
for (Map.Entry<String, Object> keyValueEntry : sc.getKeyValue().entrySet()) {
gen.writeObjectField(keyValueEntry.getKey(), keyValueEntry.getValue());
}
gen.writeEndObject();
}
}
calling Jackson's mapper is done in the usual manner:
:
public static void main(String[] args)
{
Map<String, Object> keyValue = new HashMap<>();
keyValue.put("key1", "value1");
keyValue.put("key2", "value2");
keyValue.put("key3", new Integer(10));
SomeClass sc = new SomeClass("id1", keyValue);
try {
new ObjectMapper().writeValue(System.out, sc);
} catch (IOException e) {
e.printStackTrace();
}
}
output:
{"id":"id1","key1":"value1","key2":"value2","key3":10}

Custom JSON Deserialization with Jackson

I'm using the Flickr API. When calling the flickr.test.login method, the default JSON result is:
{
"user": {
"id": "21207597#N07",
"username": {
"_content": "jamalfanaian"
}
},
"stat": "ok"
}
I'd like to parse this response into a Java object:
public class FlickrAccount {
private String id;
private String username;
// ... getter & setter ...
}
The JSON properties should be mapped like this:
"user" -> "id" ==> FlickrAccount.id
"user" -> "username" -> "_content" ==> FlickrAccount.username
Unfortunately, I'm not able to find a nice, elegant way to do this using Annotations. My approach so far is, to read the JSON String into a Map<String, Object> and get the values from there.
Map<String, Object> value = new ObjectMapper().readValue(response.getStream(),
new TypeReference<HashMap<String, Object>>() {
});
#SuppressWarnings( "unchecked" )
Map<String, Object> user = (Map<String, Object>) value.get("user");
String id = (String) user.get("id");
#SuppressWarnings( "unchecked" )
String username = (String) ((Map<String, Object>) user.get("username")).get("_content");
FlickrAccount account = new FlickrAccount();
account.setId(id);
account.setUsername(username);
But I think, this is the most non-elegant way, ever. Is there any simple way, either using Annotations or a custom Deserializer?
This would be very obvious for me, but of course it doesn't work:
public class FlickrAccount {
#JsonProperty( "user.id" ) private String id;
#JsonProperty( "user.username._content" ) private String username;
// ... getter and setter ...
}
You can write custom deserializer for this class. It could look like this:
class FlickrAccountJsonDeserializer extends JsonDeserializer<FlickrAccount> {
#Override
public FlickrAccount deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
Root root = jp.readValueAs(Root.class);
FlickrAccount account = new FlickrAccount();
if (root != null && root.user != null) {
account.setId(root.user.id);
if (root.user.username != null) {
account.setUsername(root.user.username.content);
}
}
return account;
}
private static class Root {
public User user;
public String stat;
}
private static class User {
public String id;
public UserName username;
}
private static class UserName {
#JsonProperty("_content")
public String content;
}
}
After that, you have to define a deserializer for your class. You can do this as follows:
#JsonDeserialize(using = FlickrAccountJsonDeserializer.class)
class FlickrAccount {
...
}
Since I don't want to implement a custom class (Username) just to map the username, I went with a little bit more elegant, but still quite ugly approach:
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(in);
JsonNode user = node.get("user");
FlickrAccount account = new FlickrAccount();
account.setId(user.get("id").asText());
account.setUsername(user.get("username").get("_content").asText());
It's still not as elegant as I hoped, but at least I got rid of all the ugly casting.
Another advantage of this solution is, that my domain class (FlickrAccount) is not polluted with any Jackson annotations.
Based on #MichaƂ Ziober's answer, I decided to use the - in my opinion - most straight forward solution. Using a #JsonDeserialize annotation with a custom deserializer:
#JsonDeserialize( using = FlickrAccountDeserializer.class )
public class FlickrAccount {
...
}
But the deserializer does not use any internal classes, just the JsonNode as above:
class FlickrAccountDeserializer extends JsonDeserializer<FlickrAccount> {
#Override
public FlickrAccount deserialize(JsonParser jp, DeserializationContext ctxt) throws
IOException, JsonProcessingException {
FlickrAccount account = new FlickrAccount();
JsonNode node = jp.readValueAsTree();
JsonNode user = node.get("user");
account.setId(user.get("id").asText());
account.setUsername(user.get("username").get("_content").asText());
return account;
}
}
You can also use SimpleModule.
SimpleModule module = new SimpleModule();
module.setDeserializerModifier(new BeanDeserializerModifier() {
#Override public JsonDeserializer<?> modifyDeserializer(
DeserializationConfig config, BeanDescription beanDesc, JsonDeserializer<?> deserializer) {
if (beanDesc.getBeanClass() == YourClass.class) {
return new YourClassDeserializer(deserializer);
}
return deserializer;
}});
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(module);
objectMapper.readValue(json, classType);
I made it this way:
public class FlickrAccount {
private String id;
#JsonDeserialize(converter = ContentConverter.class)
private String username;
private static class ContentConverter extends StdConverter<Map<String, String>, String> {
#Override
public String convert(Map<String, String> content) {
return content.get("_content"));
}
}
}
You have to make Username a class within FlickrAccount and give it a _content field

Categories