Monday, 6 January 2020

Jackson: DeSerialize private fields


By default, ObjectMapper instance access the properties that are public fields or have public getters/setters. Let’s confirm the same with an example.

Employee.java
package com.sample.app.model;

public class Employee {
 private String firstName;
 private String lastName;

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("Employee [firstName=");
  builder.append(firstName);
  builder.append(", lastName=");
  builder.append(lastName);
  builder.append("]");
  return builder.toString();
 }

}


App.java
package com.sample.app;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sample.app.model.Employee;

public class App {

 public static void main(String args[]) throws JsonParseException, JsonMappingException, IOException {
  String json = "{\"firstName\":\"Krishna\",\"lastName\":\"Ponnam\"}";
  
  ObjectMapper mapper = new ObjectMapper();
  Employee emp = mapper.readValue(json, Employee.class);
  
  System.out.println(emp);
  
 }

}


When you ran App.java, you will get UnrecognizedPropertyException.
Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "firstName" (class com.sample.app.model.Employee), not marked as ignorable (0 known properties: ])
 at [Source: {"firstName":"Krishna","lastName":"Ponnam"}; line: 1, column: 15] (through reference chain: com.sample.app.model.Employee["firstName"])
 at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:51)
 at com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:839)
 at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1045)
 at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1352)
 at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1330)
 at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:264)
 at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:125)
 at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3736)
 at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2726)
 at com.sample.app.App.main(App.java:16)

How can ObjectMapper access private properties?
Set field visibility of mapper to ANY.
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);


App.java
package com.sample.app;

import java.io.IOException;

import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sample.app.model.Employee;

public class App {

        public static void main(String args[]) throws JsonParseException, JsonMappingException, IOException {
                String json = "{\"firstName\":\"Krishna\",\"lastName\":\"Ponnam\"}";

                ObjectMapper mapper = new ObjectMapper();
                mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
                Employee emp = mapper.readValue(json, Employee.class);

                System.out.println(emp);

        }

}

Run App.java, you will get below message in console.
Employee [firstName=Krishna, lastName=Ponnam]


Previous                                                    Next                                                    Home

No comments:

Post a Comment