J.Mengelle
J.Mengelle

Reputation: 341

Jackson : map nested object

Using jackson, i wonder if it's possible du map json to Java with nested Object that are not like the json structure.

Here an exemple of what i want to do.

Json :

{
  a = "someValue",
  b = "someValue",
  c = "someValue"
}

Java :

public class AnObject {
  @JsonProperty("a")
  private String value;

  //Nested object
  private SomeObject;
}

public class SomeObject {
  @JsonProperty("b")
  private String value1;

  @JsonProperry("c")
  private String value2;
}

Is it possible ?

Upvotes: 6

Views: 9014

Answers (3)

imk
imk

Reputation: 384

Using ObjectMapper you can convert JSON string to Object. Use JsonUnwrapped in your AnObject class over someObject field.

@JsonUnwrapped
private SomeObject someObject;

then read JSON string and convert it to AnObject.

ObjectMapper mapper = new ObjectMapper();
try {
   AnObject anObject1 = mapper.readValue(jsonString, AnObject.class);   
} catch (IOException e) {
    e.printStackTrace();
}

Upvotes: 1

Felk
Felk

Reputation: 8224

Use the JsonUnwrapped annotation:

@JsonUnwrapped
private final SomeObject someObject;

which unwrappes all of SomeObject's fields into the parent, resulting in the following when serializing:

{"a":"foo","b":"bar","c":"baz"}

Upvotes: 9

Gaetano Piazzolla
Gaetano Piazzolla

Reputation: 1547

First of all, this is a JSON object.

It's an object literal.

Second of all, that is not a valid formatted object literal. The correct one is this:

{ "a" : "someValue", "b": "someValue", "c": "someValue"}

Next, as sayd in comments, you have to define your own deserializer.

Main:

public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {

    String json = "{\"a\" : \"someValue\",\"b\" : \"someValue\",\"c\" : \"someValue\"}";

    final ObjectMapper om =
        new ObjectMapper();//
    om.registerSubtypes(AnObject.class);
    SimpleModule module = new SimpleModule();
    module.addDeserializer(AnObject.class, new CustomDeserializer2());
    om.registerModule(module);

    AnObject ob = om.readValue(json, AnObject.class);
    System.out.println(ob.getValue());
    System.out.println(ob.getObject().getValue1());
    System.out.println(ob.getObject().getValue2());
}

Deserializer:

public class CustomDeserializer2 extends StdDeserializer<AnObject> {

private static final long serialVersionUID = -3483096770025118080L;

public CustomDeserializer2() {
    this(null);
}

public CustomDeserializer2(Class<?> vc) {
    super(vc);
}

@Override
public AnObject deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    JsonNode interNode = jp.getCodec().readTree(jp);

    AnObject ob = new AnObject();

    if (interNode.get("a") != null) {
        ob.setValue(interNode.get("a").toString());
    }

    SomeObject obj = new SomeObject();

    if (interNode.get("b") != null) {
        obj.setValue1(interNode.get("b").toString());
    }
    if (interNode.get("c") != null) {
        obj.setValue2(interNode.get("c").toString());
    }

    ob.setObject(obj);
    return ob;
}

Model: Pay attention to @JsonProperty on A field

public class AnObject {

@JsonProperty("a")
private String value;

private SomeObject object;

public String getValue() {
    return value;
}

public void setValue(String value) {
    this.value = value;
}

public SomeObject getObject() {
    return object;
}

public void setObject(SomeObject arg) {
    object = arg;
}



public class SomeObject {

private String value1;
private String value2;
public String getValue1() {
    return value1;
}
public void setValue1(String value1) {
    this.value1 = value1;
}
public String getValue2() {
    return value2;
}
public void setValue2(String value2) {
    this.value2 = value2;
}

Bye

Upvotes: 0

Related Questions