How to ignore Map related braces while converting json to Java object using Jackson

nTo Apps :

I'm trying to convert JSON to Java object, but I'm having a hard time building Java equivalent object.

My JSON looks like this

{
    "point1": {
        "x": 1.0,
        "y": 2.0
    },
    "point2": {
        "x": 1.0,
        "y": 2.0
    },
    "point3": {
        "x": 1.0,
        "y": 2.0
    },
    "customobject1": "cust1",
    "customobject2": "cust2"
}

I need to take Map here for points, because there will be n number of points,

public class Test {

    public String getCustomobject1() {
        return customobject1;
    }

    public void setCustomobject1(String customobject1) {
        this.customobject1 = customobject1;
    }

    public String getCustomobject2() {
        return customobject2;
    }

    public void setCustomobject2(String customobject2) {
        this.customobject2 = customobject2;
    }

    Map<String, Point> testing  = new HashMap<>();

    String customobject1;
    String customobject2; 

    public Map<String, Point> getTesting() {
        return testing;
    }

    public void setTesting(Map<String, Point> testing) {
        this.testing = testing;
    }

}

But I'm getting unrecognized property exception, I know there is a extra wrapper ({}) that causes this issue, Can some body suggest me how to ignore this map name while deserializing JSON?

Note: Actual object I'm working is bit complex with similar structure, I'm posting just a prototype here.

Karol Dowbecki :

If you don't know the keys in advance use @JsonAnySetter to map them:

Marker annotation that can be used to define a non-static, two-argument method (first argument name of property, second value to set), to be used as a "fallback" handler for all otherwise unrecognized properties found from JSON content. It is similar to XmlAnyElement in behavior; and can only be used to denote a single property per type.

If used, all otherwise unmapped key-value pairs from JSON Object values are added to the property (of type Map or bean).

public class Test  {
  private Map<String, Point> points = new HashMap<>();

  @JsonAnySetter
  public void setPoints(String name, Point value) {
    points.put(name, value);
  }

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=101393&siteId=1