Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make the Jackson fail on duplicate property in a JSON

I use Jackson to deserialize a JSON to an immutable custom Java object. Here is the class:

final class DataPoint {
  private final int count;
  private final int lower;
  private final int median;
  private final int upper;

  @JsonCreator
  DataPoint(
      @JsonProperty("count") int count,
      @JsonProperty("lower") int lower,
      @JsonProperty("median") int median,
      @JsonProperty("upper") int upper) {
    if (count <= 0) {
      throw new IllegalArgumentException("...");
    }
    this.count = count;
    this.lower = lower;
    this.median = median;
    this.upper = upper;
  }

  // getters...
}

Here is the JSON I deserialize:

{
  "count": 3,
  "lower": 2,
  "median": 3,
  "upper": 4
}

It works fine. Now I break the JSON, i.e. douplicate the lower property:

{
  "count": 4,
  "lower": 2,
  "lower": 3,
  "median": 4,
  "upper": 5
}

Now I get count == 4, and lower == 3. Instead, I would like the Jackson to fail deserilizing, since there is a duplicate property in the JSON (lower).

Here is the deserializing code:

String json = "..."; // the second JSON
ObjectMapper mapper = new ObjectMapper().enable(
    DeserializationFeature.FAIL_ON_MISSING_CREATOR_PROPERTIES,
    DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES,
    DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY)
    .disable(DeserializationFeature.ACCEPT_FLOAT_AS_INT);
DataPoint data = mapper.readValue(json, DataPoint.class);

Folks, can I make the Jackson to fail when desierializing a JSON with duplicate property keys?

Thank you a lot, guys!

like image 254
danissimo Avatar asked May 05 '17 11:05

danissimo


People also ask

How do I ignore Jsonproperty?

To ignore individual properties, use the [JsonIgnore] attribute. You can specify conditional exclusion by setting the [JsonIgnore] attribute's Condition property. The JsonIgnoreCondition enum provides the following options: Always - The property is always ignored.

How does Jackson parse JSON?

databind. ObjectMapper ) is the simplest way to parse JSON with Jackson. The Jackson ObjectMapper can parse JSON from a string, stream or file, and create a Java object or object graph representing the parsed JSON. Parsing JSON into Java objects is also referred to as to deserialize Java objects from JSON.

How does Jackson read nested JSON?

A JsonNode is Jackson's tree model for JSON and it can read JSON into a JsonNode instance and write a JsonNode out to JSON. To read JSON into a JsonNode with Jackson by creating ObjectMapper instance and call the readValue() method. We can access a field, array or nested object using the get() method of JsonNode class.

Is JSON A Jackson?

Jackson allows you to read JSON into a tree model: Java objects that represent JSON objects, arrays and values. These objects are called things like JsonNode or JsonArray and are provided by Jackson.


1 Answers

You can enable STRICT_DUPLICATE_DETECTION to make the parser throw an Exception if there's a duplicate property, e.g.:

String s = "{\"count\": 4,\"lower\": 2,\"lower\": 3,\"median\": 4,\"upper\": 5}";
ObjectMapper mapper = new ObjectMapper();
mapper.enable(JsonParser.Feature.STRICT_DUPLICATE_DETECTION);
JsonNode readTree = mapper.readTree(s);
System.out.println(readTree.toString());

This will throw the following exception:

Exception in thread "main" com.fasterxml.jackson.core.JsonParseException: Duplicate field 'lower'

Here's the documentation.

like image 52
Darshan Mehta Avatar answered Sep 28 '22 02:09

Darshan Mehta