ceepan
ceepan

Reputation: 95

Use of Jayway JsonPath with Jackson

I have been using JsonPath. However after an issue yesterday where I discovered that the default JsonSmartJsonProvider didn't report an error with an invalid document at parse time, I modified my setup to use Jackson as below

public JsonPathExtractor(String document) throws DocumentFormatException
{
    try
    {
        Configuration.setDefaults(new Configuration.Defaults() 
        {
              private final JsonProvider jsonProvider = new JacksonJsonProvider();
                private final MappingProvider mappingProvider = new JacksonMappingProvider();

            @Override
            public JsonProvider jsonProvider() 
            {
                return jsonProvider;
            }

            @Override
            public MappingProvider mappingProvider() 
            {
                return mappingProvider;
            }

            @Override
            public Set<Option> options() 
            {
                return EnumSet.noneOf(Option.class);
            }
            });

        // Get an object representation of the JSON to allow values to be extracted
        this.document = Configuration.defaultConfiguration().jsonProvider().parse(document);
    }
    catch(Exception e)
    {
        throw new DocumentFormatException("Invalid JSON document", e);
    }
}

However I see a difference in behaviour, in that if I get a path which has a few fields, they are not quoted, whereas they were when using JsonSmartJsonProvider.
Example JSON

{
  "firstName": "John",
  "lastName": "Smith",
  "isAlive": true,
  "age": 25,
  "height_cm": 167.6,
  "address": {
    "streetAddress": "21 2nd Street",
    "city": "New York",
    "state": "NY",
    "postalCode": "10021-3100"
  },
  "phoneNumbers": [
    {
      "type": "home",
      "number": "212 555-1234"
    },
    {
      "type": "office",
      "number": "646 555-4567"
    }
  ],
  "children": [],
  "spouse": null
}

With the call:

Object obj = JsonPath.read(document, "$.phoneNumbers"); 

When using JacksonMappingProvider I get

[{type=home, number=212 555-1234}, {type=office, number=646 555-4567}] 

When using JsonSmartJsonProvider I get:

[{"type":"home","number":"212 555-1234"},{"type":"office","number":"646 555-4567"}]  

If I want Jackson to behave the same way, is there something else that I can configure?

Upvotes: 2

Views: 6291

Answers (1)

glytching
glytching

Reputation: 47895

There's a difference between the way in which Jackson has handled the values and the way in which they are printed out.

When using JsonSmartJsonProvider this line ...

JsonPath.read(parse, "$.phoneNumbers");

... returns a JSONArray and the toString() method - which is called when you 'print' the JSONArray instance is smart enough to know it is dealing with JSON so it prints that state as a JSON string. For example:

[{"type":"home","number":"212 555-1234"},{"type":"office","number":"646 555-4567"}]

But when you use a JacksonJsonProvider then this line ...

JsonPath.read(parse, "$.phoneNumbers");

... returns a List of LinkedHashMap and the toString() implementation invoked when you 'print' that instance is not JSON aware so it prints this:

[{type=home, number=212 555-1234}, {type=office, number=646 555-4567}]

If you want to print JSON when using the JacksonJsonProvider then you have to print it using something which is JSON aware. Here's an example:

String payload = "{\n" +
        "  \"firstName\": \"John\",\n" +
        "  \"lastName\": \"Smith\",\n" +
        "  \"isAlive\": true,\n" +
        "  \"age\": 25,\n" +
        "  \"height_cm\": 167.6,\n" +
        "  \"address\": {\n" +
        "    \"streetAddress\": \"21 2nd Street\",\n" +
        "    \"city\": \"New York\",\n" +
        "    \"state\": \"NY\",\n" +
        "    \"postalCode\": \"10021-3100\"\n" +
        "  },\n" +
        "  \"phoneNumbers\": [\n" +
        "    {\n" +
        "      \"type\": \"home\",\n" +
        "      \"number\": \"212 555-1234\"\n" +
        "    },\n" +
        "    {\n" +
        "      \"type\": \"office\",\n" +
        "      \"number\": \"646 555-4567\"\n" +
        "    }\n" +
        "  ],\n" +
        "  \"children\": [],\n" +
        "  \"spouse\": null\n" +
        "}";

// this is a simpler way of declaring and using the JacksonJsonProvider
ObjectMapper objectMapper = new ObjectMapper();

Configuration conf = Configuration.builder()
        .jsonProvider(new JacksonJsonProvider(objectMapper))
        .build();

Object obj = JsonPath.using(conf).parse(payload).read("$.phoneNumbers");

// prints out:
//      [{type=home, number=212 555-1234}, {type=office, number=646 555-4567}]
System.out.println(obj);

// prints out:
//      [{"type":"home","number":"212 555-1234"},{"type":"office","number":"646 555-4567"}]
System.out.println(objectMapper.writer().writeValueAsString(obj));

Upvotes: 3

Related Questions