Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map the Mixins for the nested JSON response

I am using Jackson APIs for Mapping my JSON response into a java object. For example,

for the response { name :'karthikeyan',age:'24',gender:'Male'}

@JsonProperty("name")
public String _name;
@JsonProperty("age")
public int _age;
@JsonProperty("gender")
public String _gender;

is the Mix-in and it works fine.(internally we will be mapping this pojo and Mix-in).Now how can i represent the following response in a Mix-in?

{
name :'karthikeyan',
age:'24',
gender:'Male',
interest:
      {
        books:'xxx',
        music:'yyy',
        movie:'zzz'
      }
}

i have tried with the following, but no luck.

@JsonProperty("name")
public String _name;
@JsonProperty("age")
public int _age;
@JsonProperty("gender")
public String _gender;

@JsonProperty("interest")
public InterestPojo interestPojo;  //created same format mix-in and pojo for interest params as well.

but unable to map them exactly, give your comments and thoughts on how to do it ?

like image 800
Karthikeyan Avatar asked Feb 13 '14 10:02

Karthikeyan


People also ask

How do I map nested values with Jackson?

Mapping With Annotations To map the nested brandName property, we first need to unpack the nested brand object to a Map and extract the name property. To map ownerName, we unpack the nested owner object to a Map and extract its name property.

What is JSON mixin?

4 minute read. Jackson mixins is a mechanism to add Jackson annotations to classes without modifying the actual class. It was created for those cases where we can't modify a class such as when working with third-party classes.

How do you access the JSON fields arrays and nested objects of JsonNode in Java?

We can access a field, array or nested object using the get() method of JsonNode class. We can return a valid string representation using the asText() method and convert the value of the node to a Java int using the asInt() method of JsonNode class.


1 Answers

I tried the following:

    ObjectMapper mapper = new ObjectMapper();
    System.out.println(mapper.writeValueAsString(new Something("Name", 12, "male", new Nested("books", "Music", "Movie"))));

public class Something {

    @JsonProperty("name")
    public String name;
    @JsonProperty("age")
    public int age;
    @JsonProperty("gender")
    public String gender;
    @JsonProperty("interest")
    public Nested nested;
    //Constructor
}

public class Nested {

    @JsonProperty("books")
    public String books;
    @JsonProperty("music")
    public String music;
    @JsonProperty("movie")
    public String movie;

    //Constructor
}

and the Output is:

{
"name":"Name",
"age":12,
"gender":"male",
"interest":
    {
        "books":"books",
        "music":"Music",
        "movie":"Movie"
    }
}

So everything is working as expected. I already checked if theres a difference if you provide some setters and getters and setting the visibility of the fields on private but that doas not make a difference.

Maybe you want to show us your InterestPojo or your output/stacktrace?

EDIT: Okay i think i got it ;)

I tried the following:

public void start() throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.getSerializationConfig().addMixInAnnotations(Something.class, Nested.class);
    mapper.getDeserializationConfig().addMixInAnnotations(Something.class, Nested.class);
    System.out.println(mapper.writeValueAsString(new Something("Name", 12, "male", new NestedImpl("name", null))));
}
public final class Something {
    private final String name;
    private int age;
    private String gender;
    // thats your interest thing
    public Nested nested;

    public Something(String name, int age, String gender, Nested nested) {
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.nested = nested;
    }
    String getName() {
        return name;
    }
    Nested getNested() {
        return nested;
    }
}
public abstract class Nested {
    @JsonProperty("name-ext")
    abstract String getName();
    @JsonProperty("interest-ext")
    abstract Nested getNested();
}
public class NestedImpl extends Nested {
    private String name;
    private Nested nested;
    private NestedImpl(String name, Nested nested) {
        this.name = name;
        this.nested = nested;
    }
    @Override
    String getName() {
        return name;
    }
    @Override
    Nested getNested() {
        return nested;
    }
}

Output:

{
    "age":12,
    "gender":"male",
    "name-ext":"Name",
    "interest-ext":
    {
        "name-ext":"name",
        "interest-ext":null
    }
}

Thats not exactly your structure, but I think thats what you want. Am I right?

EDIT2: I Tested the following structure with JSON->Object and Object->JSON.

ObjectMapper mapper = new ObjectMapper();
mapper.getSerializationConfig().addMixInAnnotations(Something.class, Mixin.class);
mapper.getSerializationConfig().addMixInAnnotations(Nested.class, NestedMixin.class);
mapper.getDeserializationConfig().addMixInAnnotations(Something.class, Mixin.class);
mapper.getDeserializationConfig().addMixInAnnotations(Nested.class, NestedMixin.class);

Nested nested = new Nested();
nested.setName("Nested");
nested.setNumber(12);

Something some = new Something();
some.setName("Something");
some.setAge(24);
some.setGender("Male");
some.setNested(nested);

String json = mapper.writeValueAsString(some);
System.out.println(json);
Something some2 = mapper.readValue(json, Something.class);
System.out.println("Object: " + some2);

public abstract class Mixin {

    @JsonProperty("name")
    private String _name;
    @JsonProperty("age")
    private int _age;
    @JsonProperty("gender")
    private String _gender;
    @JsonProperty("interest")
    private Nested nested;
}

public class Something {
    private String _name;
    private int _age;
    private String _gender;
    private Nested nested;

    // You have to provide Setters and Getters!!
}

public abstract class NestedMixin {

    @JsonProperty("nameNested")
    private String name;
    @JsonProperty("numberNested")
    private int number;
}

public class Nested {
    private String name;
    private int number;

    // You have to provide Setters and Getters!!
}

Output: {"age":24,"gender":"Male","name":"Something","interest":{"nameNested":"Nested","numberNested":12}}

Object: Something{name=Something, age=24, gender=Male, nested=Nested{name=Nested, number=12}}

Note: It seems that jackson got problems with inner classes. So if you test that examples in an extra project create extra class-files ;)

EDIT3: If you are using a Module, try the following:

public class JacksonMixinModule extends SimpleModule {
    public JacksonMixinModule() {
        super("JacksonMixinModule", new Version(0, 1, 0, "SNAPSHOT"));
    }
    @Override
    public void setupModule(SetupContext context) {
        super.setupModule(context);
        context.setMixInAnnotations(Something.class, Mixin.class);
        context.setMixInAnnotations(Nested.class, NestedMixin.class);
    }
}

...

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JacksonMixinModule());
like image 105
Yser Avatar answered Nov 26 '22 06:11

Yser