@Override public Parser<String, Object> makeParser() { return new JSONPathParser(getFlattenSpec(), objectMapper); }
@JsonCreator public JSONFlatDataParser( @JacksonInject @Json ObjectMapper jsonMapper, @JsonProperty("keyFieldName") final String keyFieldName, @JsonProperty("valueFieldName") final String valueFieldName ) { Preconditions.checkArgument(!Strings.isNullOrEmpty(keyFieldName), "[keyFieldName] cannot be empty"); Preconditions.checkArgument(!Strings.isNullOrEmpty(valueFieldName), "[valueFieldName] cannot be empty"); this.keyFieldName = keyFieldName; this.valueFieldName = valueFieldName; // Copy jsonMapper; don't want to share canonicalization tables, etc., with the global ObjectMapper. this.parser = new DelegateParser( new JSONPathParser( new JSONPathSpec( false, ImmutableList.of( new JSONPathFieldSpec(JSONPathFieldType.ROOT, keyFieldName, keyFieldName), new JSONPathFieldSpec(JSONPathFieldType.ROOT, valueFieldName, valueFieldName) ) ), jsonMapper.copy() ), keyFieldName, valueFieldName ); }
@Test public void testSimple() { List<JSONPathFieldSpec> fields = new ArrayList<>(); final Parser<String, Object> jsonParser = new JSONPathParser(new JSONPathSpec(true, fields), null); final Map<String, Object> jsonMap = jsonParser.parseToMap(json); Assert.assertEquals( "jsonMap", ImmutableMap.of("one", "foo", "two", ImmutableList.of("bar", "baz"), "three", "qux"), jsonMap ); }
@Test public void testWithNumbers() { List<JSONPathFieldSpec> fields = new ArrayList<>(); final Parser<String, Object> jsonParser = new JSONPathParser(new JSONPathSpec(true, fields), null); final Map<String, Object> jsonMap = jsonParser.parseToMap(numbersJson); Assert.assertEquals( "jsonMap", ImmutableMap.of("five", 5.0, "six", 6L, "many", 1234567878900L, "toomany", 1.23456789E21), jsonMap ); }
@Test public void testWithWhackyCharacters() { List<JSONPathFieldSpec> fields = new ArrayList<>(); final Parser<String, Object> jsonParser = new JSONPathParser(new JSONPathSpec(true, fields), null); final Map<String, Object> jsonMap = jsonParser.parseToMap(whackyCharacterJson); Assert.assertEquals( "jsonMap", ImmutableMap.of("one", "foo?"), jsonMap ); }
@Test public void testRejectDuplicates() { List<JSONPathFieldSpec> fields = new ArrayList<>(); fields.add(new JSONPathFieldSpec(JSONPathFieldType.PATH, "met-array", "$.met.a")); fields.add(new JSONPathFieldSpec(JSONPathFieldType.PATH, "met-array", "$.met.a")); thrown.expect(IllegalArgumentException.class); thrown.expectMessage("Cannot have duplicate field definition: met-array"); final Parser<String, Object> jsonParser = new JSONPathParser(new JSONPathSpec(false, fields), null); jsonParser.parseToMap(nestedJson); }
@Test public void testRejectDuplicates2() { List<JSONPathFieldSpec> fields = new ArrayList<>(); fields.add(new JSONPathFieldSpec(JSONPathFieldType.PATH, "met-array", "$.met.a")); fields.add(new JSONPathFieldSpec(JSONPathFieldType.JQ, "met-array", ".met.a")); thrown.expect(IllegalArgumentException.class); thrown.expectMessage("Cannot have duplicate field definition: met-array"); final Parser<String, Object> jsonParser = new JSONPathParser(new JSONPathSpec(false, fields), null); jsonParser.parseToMap(nestedJson); }
@Test public void testParseFail() { List<JSONPathFieldSpec> fields = new ArrayList<>(); thrown.expect(ParseException.class); thrown.expectMessage("Unable to parse row [" + notJson + "]"); final Parser<String, Object> jsonParser = new JSONPathParser(new JSONPathSpec(true, fields), null); jsonParser.parseToMap(notJson); } }
fields.add(new JSONPathFieldSpec(JSONPathFieldType.JQ, "jq-met-array", ".met.a")); final Parser<String, Object> jsonParser = new JSONPathParser(new JSONPathSpec(false, fields), null); final Map<String, Object> jsonMap = jsonParser.parseToMap(nestedJson);
final Parser<String, Object> jsonParser = new JSONPathParser(new JSONPathSpec(true, fields), null); final Map<String, Object> jsonMap = jsonParser.parseToMap(nestedJson);
@Override public Parser<String, Object> makeParser() { return new JSONPathParser(flattenSpec, objectMapper); }