Deserialize multiple json fields into single java property - java

I want to convert a json into Java class by having custom deserializer.
I'm able to serialize ACC_NUM, NAME and any other fields from json but not sure what can be done to convert MOBILE_NUMBER_1,MOBILE_NUMBER_2 like fields into single JSONArray(See AccountInfo class). There can be many more fields like this and count also is not fixed. Example there can be ADDRESS_1, ADDRESS_2 till ADDRESS_20 and so on and all this fields should go in JSONArray of ADDRESS after deserilization.
I have a Map of Map which holds info like this:
{
"accountInfo": {
"ACC_NUM": "1234567890",
"NAME": "John Cena",
"MOBILE_NUMBER_1": "12376534",
"MOBILE_NUMBER_2": "12376534",
"MOBILE_NUMBER_3": "12376534",
"MOBILE_NUMBER_4": "12376534"
},
"someOther": {
//similer to above
}
}
This info I want to convert to the following class CommonInfo:
public class CommonInfo {
private AccountInfo accountInfo;
//other properties...
}
#JsonIgnoreProperties(ignoreUnknown = true)
public class AccountInfo {
#JsonProperty("ACC_NUM")
private FieldValue<BigInteger> accountNum;
#JsonProperty("NAME")
private FieldValue<String> name;
#JsonProperty("MOBILE_NUMBER")
private FieldValue<JSONArray> mobileNumber;
}
//FieldValue class
public interface FieldValue<T> {
T getInitialValue();
void setInitialValue(T initialValue);
T getValue();
void setValue(T value);
}
#JsonInclude(JsonInclude.Include.ALWAYS)
public class FieldValueImpl<T> implements FieldValue<T> {
protected T initialValue;
protected T value;
//getters, setters, cons..
}
My service code takes json/Map and tries to convert it to CommonInfo class
#Service
public class MyService {
private final ObjectMapper jsonMapper = new ObjectMapper();
#PostConstruct
protected void init() {
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(FieldValue.class, new FieldValueSerializer());
simpleModule.addDeserializer(FieldValue.class, new FieldValueDeserializer());
jsonMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
jsonMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
jsonMapper.registerModule(simpleModule);
}
public CommonInfo setPojoResult(Map<String, LinkedHashMap<String, String>> contentAsMap) {
return jsonMapper.convertValue(contentAsMap, CommonInfo.class);
}
}
Serializer and Deserializer looks like this:
public class FieldValueDeserializer extends JsonDeserializer<FieldValue<?>> implements ContextualDeserializer {
private JavaType valueType;
public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property)
throws JsonMappingException {
var deserializer = new FieldValueDeserializer();
if (property == null) {
deserializer.valueType = ctxt.getContextualType().containedType(0);
} else {
var wrapperType = property.getType();
var valueType = wrapperType.containedType(0);
deserializer.valueType = valueType;
}
return deserializer;
}
#Override
public FieldValue<?> deserialize(JsonParser parser, DeserializationContext context) throws IOException {
FieldValueDeserializer deserializer = new FieldValueDeserializer();
deserializer.getKnownPropertyNames();
FieldValue<?> fieldValueImpl = new FieldValueImpl<>();
if (valueType.toString().contains("java.time.LocalDate")) {
JsonNode node = parser.getCodec().readTree(parser);
FieldValue<LocalDate> f1 = new FieldValueImpl<>();
f1.setValue(DateUtils.convertJulianToLocalDate(node.textValue()));
return f1;
} else {
fieldValueImpl.setValue(context.readValue(parser, valueType));
}
return fieldValueImpl;
}
}
//--
public class FieldValueSerializer extends StdSerializer<FieldValue> {
public FieldValueSerializer() {
this(null);
}
public FieldValueSerializer(Class<FieldValue> vc) {
super(vc);
}
#Override
public void serialize(FieldValue value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
jgen.writeString(String.valueOf(value.getCurValue()));
}
}

Related

How to conditionally serialize a field

I want to conditionally serialize a field. I figured out how to conditionally ignore a field with
public class NologIntrospector extends JacksonAnnotationIntrospector {
#Override
public boolean hasIgnoreMarker(AnnotatedMember m) {
boolean noLogOnClass = m.getDeclaringClass().getAnnotation(NoLog.class) != null;
return m.hasAnnotation(NoLog.class) || super.hasIgnoreMarker(m) || noLogOnClass;
}
}
But what I really want to do is to redact the field. So if I have
#Getter
#Setter
#NoArgsConstructor
public class MyObject1 {
public String field1 = "field1";
#NoLog
public String field2 = "field2";
public static void main(String[] args) throws JsonProcessingException {
final ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setAnnotationIntrospector(new NologIntrospector());
final MyObject1 myObject1 = new MyObject1();
System.out.println(objectMapper.writeValueAsString(myObject1));
}
}
I get
{"field1":"field1"}
Field2 is correctly ignored. But what I really want is
{"field1":"field1", "field2": "<***redacted***>"}
I have another annotation, #MaskSensitiveData
#Target({ElementType.FIELD, ElementType.PARAMETER})
#Retention(RetentionPolicy.RUNTIME)
#JacksonAnnotationsInside
#JsonSerialize(using = MaskSensitiveDataSerializer.class)
public #interface MaskSensitiveData {
}
public class MaskSensitiveDataSerializer extends StdSerializer<Object> {
protected MaskSensitiveDataSerializer() {
this(null);
}
public MaskSensitiveDataSerializer(Class<Object> t) {
super(t);
}
#Override
public void serialize(Object value, JsonGenerator gen, SerializerProvider provider) throws IOException {
if (!(value instanceof String)) {
throw new RuntimeException("MaskSensitiveData annotation is only valid for string");
}
gen.writeString("<*** redacted ***>");
}
}
So what I want to do is combine them. So if a field has the special annotation and I am using my introspector, only then, do I want to redact the field. Otherwise, the field should be serialized normally.
Instead of overriding method hasIgnoreMarker in NologIntrospector. You need to override findSerializer.
This way when your introspector is enabled the custom serializer will be used else it will use the original serializer.
public class NologIntrospector extends JacksonAnnotationIntrospector {
#Override
public Object findSerializer(Annotated ann){
if (ann.hasAnnotation(NoLog.class)) {
return MaskSensitiveDataSerializer.class;
}
return super.findSerializer(ann);
}
}

Java Jackson: why do I get JsonMappingException although I used #JsonDeserialize

I need some help with Jackson library in Java. I have the following class:
#JsonIgnoreProperties(ignoreUnknown = true)
public class MarketHistoryData {
private String countryCode;
#JsonDeserialize(keyUsing = BpTimeDeserializer.class)
private Map<BpTime, Double> hourToExpectedEngagePaidListings;
#JsonDeserialize(keyUsing = BpTimeDeserializer.class)
private Map<BpTime, Double> hourMinuteToExpectedEngagePaidListings;
public MarketHistoryData() {}
...
// getters and setters
}
I understood that Jackson has difficulties with deserializing a map which its keys are an object. Therefore, I added the annotations:
#JsonDeserialize(keyUsing = BpTimeDeserializer.class)
The class BpTimeDeserializer is:
public class BpTimeDeserializer extends KeyDeserializer {
private ObjectMapper mapper = new ObjectMapper();
#Override
public BpTime deserializeKey(String key, DeserializationContext ctxt) throws IOException {
return mapper.readValue(key, BpTime.class);
}
}
Still, I get an error during the deserialization process:
*****.UncheckedExecutionException:
com.fasterxml.jackson.databind.JsonMappingException: Unrecognized
token 'com': was expecting (JSON String, Number, Array, Object or
token 'null', 'true' or 'false') at [Source:
(String)"****BpTime#5922062e[hour=1,minute=0]";
line: 1, column: 1] (through reference chain:
***PacerCycleOutput["campaignIdToFactorComputationData"]->java.util.LinkedHashMap["1011620661"]->****FactorComputationData["selectedMarketHistoryDataForCampaign"]->***MarketHistoryData["hourToExpectedEngagePaidListings"])
Do you know what I can do to overcome this error?
I finally found a way how to resolve this error.
#JsonIgnoreProperties(ignoreUnknown = true)
public class MarketHistoryData {
private String countryCode;
#JsonDeserialize(keyUsing = BpTimeDeserializer.class)
#JsonSerialize(keyUsing = BpTimeSerializer.class)
private Map<BpTime, Double> hourToExpectedEngagePaidListings;
#JsonDeserialize(keyUsing = BpTimeDeserializer.class)
#JsonSerialize(keyUsing = BpTimeSerializer.class)
private Map<BpTime, Double> hourMinuteToExpectedEngagePaidListings;
...
}
public class BpTimeDeserializer extends KeyDeserializer {
private ObjectMapper mapper = new ObjectMapper();
#Override
public BpTime deserializeKey(String key, DeserializationContext ctxt) throws IOException {
String[] keySplit = key.split(" ");
BpTime bpTime = new BpTime();
if (!keySplit[0].equals("-1")) {
bpTime.setHour(Integer.parseInt(keySplit[0]));
}
if (!keySplit[1].equals("-1")) {
bpTime.setMinute(Integer.parseInt(keySplit[1]));
}
return bpTime;
}
}
public class BpTimeSerializer extends StdSerializer<BpTime> {
public BpTimeSerializer() {
this(null);
}
public BpTimeSerializer(Class<BpTime> t) {
super(t);
}
#Override
public void serialize(BpTime value, JsonGenerator generator, SerializerProvider arg2) throws IOException {
generator.writeFieldName(String.format("%s %s", ofNullable(value.getHour()).orElse(-1),
ofNullable(value.getMinute()).orElse(-1)));
}
}

Jackson json property (string) to instance

Assuming I have the following JSON:
{
"property": "123:1234"
}
How do I use Jackson annotations to ensure that the string value of "property" is de-serialized to a self-defined class rather than a String object?
I went through their documentation and I was unable to find this particular feature.
Thanks in advance.
You could create custom deserializer for your field. Assuming you want to map it to SomeClass object :
public class SomeClass {
#JsonDeserialize(using = CustomPropertyDeserializer.class)
private Properties property;
public Properties getProperty() {
return property;
}
public void setProperty(Properties property) {
this.property = property;
}
}
You annotate your field that you want to deserialize customly with #JsonDeserialize annotation passing custom deserializer.
Your deserializer could look like this :
public class CustomPropertyDeserializer extends StdDeserializer<Properties> {
public CustomPropertyDeserializer() {
super(Properties.class);
}
#Override
public Properties deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
String valueAsString = p.getValueAsString();
String[] split = valueAsString.split(":");
return new Properties(split[0], split[1]);
}
}
And custom property class :
public class Properties {
private String first;
private String second;
public Properties(String first, String second) {
this.first = first;
this.second = second;
}
public String getFirst() {
return first;
}
public void setFirst(String first) {
this.first = first;
}
public String getSecond() {
return second;
}
public void setSecond(String second) {
this.second = second;
}
}
For testing it :
public static void main(String[] args) throws IOException {
String s = Files.lines(Paths.get("src/main/resources/data.json")).collect(Collectors.joining());
ObjectMapper objectMapper = new ObjectMapper();
SomeClass someClass = objectMapper.readValue(s, SomeClass.class);
System.out.println(someClass.getProperty().getFirst());
System.out.println(someClass.getProperty().getSecond());
}
The output is then :
123
1234
So all the custom logic how to map your String to some class that you define could be placed in deserialize method of your custom deserializer.
First thing first define your class that needs to be used:
#JsonInclude(JsonInclude.Include.NON_NULL)
public class JsonTest{
#JsonProperty("property")
private String property;
//define your getters and setters for the field
Then you can use the ObjectMapper class from jackson:
public static <T> T extractObjectFromJson(String jsonText, Class<T> type) {
try {
return new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).reader().forType(type)
.readValue(jsonText);
} catch (Exception e) {
//Manage your exception here
}
return null;
}
So you can just call the method extractobjectFromJson(//Your JSON String, JsonTest.class) to get your JSON deserialized.

jackson: mapping some fields of json to inner fields of class

I want to map some fields of json to inner fields of a class. e.g
{
values:[{
"name":"Abc",
"age":18,
"street":"test",
"postalcoad":"1231412"
},
{
"name":"ccvb",
"age":20,
"street":"test2",
"postalcoad":"123"
}
]}
Following i my java class
#JsonIgnoreProperties(ignoreUnknown = true)
public class Customer{
#JsonProperty("name")
private string name;
#JsonProperty("age")
private int age;
private Address address;
}
#JsonIgnoreProperties(ignoreUnknown = true)
public class Address{
#JsonProperty("street")
private string street;
#JsonProperty("postalcode")
private string postalcode;
}
ObjectMapper mapper = new ObjectMapper();
Customer[] c = mapper.readValue(mapper.readTree(json).get("values").toString(), Customer[].class);
It returns me Customer object without Address. Any idea how can i create Address object from this json.
One of the options is to use #JsonCreator annotation:
#JsonCreator
public Customer(
#JsonProperty("name") String name,
#JsonProperty("age") int age,
#JsonProperty("street") String street,
#JsonProperty("postalcode") String postalcode
) {
this.name = name;
this.age = age;
this.address = new Address();
this.address.street = street;
this.address.postalcode = postalcode;
}
Second option is create custom deserializer and bind your class with deserializer using #JsonDeserialize annotation
#JsonDeserialize(using = CustomerDeserializer.class)
public static class Customer{
....
}
public class CustomerDeserializer extends StdDeserializer<Customer> {
public CustomerDeserializer() {
super(Customer.class);
}
#Override
public Customer deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
Customer customer = new Customer();
JsonNode treeNode = p.readValueAsTree();
if (treeNode == null) {
return null;
}
customer.setName(treeNode.get("name").asText());
customer.setAge(treeNode.get("age").asInt());
Address address = new Address();
address.setStreet(treeNode.get("street").asText());
address.setPostalcode(treeNode.get("postalcode").asText());
customer.setAddress(address);
return customer;
}
}
As third option, you can use #JsonAnySetter with some kind of post construct processing:
public interface PostConstruct {
void postConstruct();
}
public class Customer implements PostConstruct {
//mapping
private Map<String, Object> additionalFields = new HashMap<>();
#JsonAnySetter
public void setAdditionalValue(String key, Object value) {
additionalFields.put(key, value);
}
#Override
public void postConstruct() {
address = new Address();
address.setStreet(String.valueOf(additionalFields.get("street")));
address.setPostalcode(String.valueOf(additionalFields.get("postalcode")));
}
}
public static class PostConstructDeserializer extends DelegatingDeserializer {
private final JsonDeserializer<?> deserializer;
public PostConstructDeserializer(JsonDeserializer<?> deserializer) {
super(deserializer);
this.deserializer = deserializer;
}
#Override
protected JsonDeserializer<?> newDelegatingInstance(JsonDeserializer<?> newDelegatee) {
return deserializer;
}
#Override
public Object deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
Object result = _delegatee.deserialize(jp, ctxt);
if (result instanceof PostConstruct) {
((PostConstruct) result).postConstruct();
}
return result;
}
}
//using of post construct deserializer
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.setDeserializerModifier(new BeanDeserializerModifier() {
#Override
public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config,
BeanDescription beanDesc,
final JsonDeserializer<?> deserializer) {
return new PostConstructDeserializer(deserializer);
}
});
mapper.registerModule(module);
I would create a custom deserializer and inside of it call the default deserializer for Customer and then call the default deseriazlier for Address. Then you add the address to the customer object. This way they both look at the same json but you get two different objects out and you can connect them the way you want.
To call a standard deserializer from a custom deseriazlier see this answer: How do I call the default deserializer from a custom deserializer in Jackson.

Custom serializer for serializing a List<User> in List<String>

I've a Model object Group
public class Group {
String title;
List<User> members;
String createdBy;
}
I'm using Jackson to serialize this Object. Instead of serializing the whole User object in list "members" I want to serializer just the user.getTitle() field.
Basically I want a HashMap to be something like
{
"title" : "sometitle"
"members" : [user1.getTitle(), user2.getTitle()]
}
I've written a custom serializer for this
public class GroupSerializer extends JsonSerializer<Circle> {
#Override
public void serialize(Group value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
if(value != null) {
gen.writeStartObject();
gen.writeStringField("title", value.getTitle());
gen.writeStringField("createdBy", value.getCreatedBy());
gen.writeFieldName("members");
gen.writeStartArray();
for(User user : value.getMembers()) {
gen.writeString(user.getEmail());
}
gen.writeEndArray();
gen.writeEndObject()
}
}
}
But it's not working. How do I serialize only a field of List instead of whole User Object?
I suggest that you look into Jackson's Converter interface, which seems more suited to the task than creating a custom serializer.
One approach it to create a Converter instance and add it to the ObjectMapper, so that it will be used for the serialization of all User instances.
public class UserConverter extends StdConverter<User, String> {
#Override
public String convert(User user) {
return user.getTitle();
}
}
Register it on your ObjectMapper like this:
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(User.class, new StdDelegatingSerializer(new UserConverter()));
ObjectMapper om = new ObjectMapper().registerModule(simpleModule);
Another approach, in case you don't want to convert all User instances to String, is to annotate selected properties with a converter like this:
public class Group {
String title;
#JsonSerialize(converter = ListUserConverter.class)
List<User> members;
String createdBy;
}
And have a corresponding converter that looks something like this:
public class ListUserConverter extends StdConverter<List<User>, List<String>> {
#Override
public List<String> convert(List<User> users) {
return users.stream().map(User::getTitle).collect(Collectors.toList());
}
}
Try like below :
Group:
#JsonIgnoreProperties(ignoreUnknown=true)
public class Group {
#JsonSerialize(using= TitleSerializer.class)
List<User> members;
//getters and setters
}
User:
public class User {
private String title;
//getters and setters
}
Custom Serializer :
public class TitleSerializer extends StdSerializer<List<User>> {
private static List<User> users=new ArrayList<User>();
protected TitleSerializer(Class<List<User>> t) {
super(t);
// TODO Auto-generated constructor stub
}
#SuppressWarnings("unchecked")
public TitleSerializer(){
this((Class<List<User>>) users.getClass());
}
#Override
public void serialize(List<User> users, JsonGenerator paramJsonGenerator,
SerializerProvider paramSerializerProvider) throws IOException {
paramJsonGenerator.writeStartObject();
List<String> titles=new ArrayList<String>(users.size());
for(User user: users){
titles.add(user.getTitle());
}
paramJsonGenerator.writeObjectField("members", titles);
paramJsonGenerator.writeEndObject();
}
}
Test :
Group group=new Group(Arrays.asList(new User("a"),new User("b"),new User("c")));
ObjectMapper mapper = new ObjectMapper();
String serialized = mapper.writeValueAsString(group);
System.out.println("output "+serialized);
Output:
{"members":["a","b","c"]}

Categories