@Test public void single_match_value_can_be_mapped(){ MapFunction mapFunction = new ToStringMapFunction(); String stringResult = parse(JSON_DOCUMENT).map("$.string-property", mapFunction).read("$.string-property"); assertThat(stringResult.endsWith("converted")).isTrue(); }
@Test(expected = InvalidModificationException.class) public void rootCannotBeMapped(){ MapFunction mapFunction = new MapFunction() { @Override public Object map(Object currentValue, Configuration configuration) { return currentValue.toString()+"converted"; } }; Object o = parse(JSON_DOCUMENT).map("$", mapFunction).json(); }
@Test public void multi_match_path_can_be_mapped(){ MapFunction mapFunction = new ToStringMapFunction(); List<Double> doubleResult = parse(JSON_DOCUMENT).read("$..display-price"); for(Double dRes : doubleResult){ assertThat(dRes).isInstanceOf(Double.class); } List<String> stringResult = parse(JSON_DOCUMENT).map("$..display-price", mapFunction).read("$..display-price"); for(String sRes : stringResult){ assertThat(sRes).isInstanceOf(String.class); assertThat(sRes.endsWith("converted")).isTrue(); } }
@Test public void object_can_be_mapped(){ TypeRef<List<String>> typeRef = new TypeRef<List<String>>() {}; MapFunction mapFunction = new ToStringMapFunction(); DocumentContext documentContext = JsonPath.using(JACKSON_CONFIGURATION).parse(JSON_DOCUMENT); Object list = documentContext.read("$..book"); assertThat(list).isInstanceOf(List.class); String result = documentContext.map("$..book", mapFunction).read("$..book", typeRef).get(0); assertThat(result).isInstanceOf(String.class); assertThat(result).endsWith("converted"); }
@Test public void mapPropertyWithPOJO() { String someJson = "" + "{\n" + " \"a\": \"a\",\n" + " \"b\": \"b\"\n" + "}"; ObjectMapper om = new ObjectMapper(); om.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); Configuration c = Configuration .builder() .mappingProvider(new JacksonMappingProvider()) .jsonProvider(new JacksonJsonNodeJsonProvider(om)) .build(); DocumentContext context = JsonPath.using(c).parse(someJson); String someJsonStr = context.jsonString(); DocumentContext altered = context.map("$['a', 'b', 'c']", new MapFunction() { @Override public Object map(Object currentValue, Configuration configuration) { return currentValue; } }); assertThat(altered.jsonString()).isEqualTo(someJsonStr); }
protected String getCommentTreeDb(String rid, String sortedBy, String sortDir) { String sql = "select from Comment where in_HasComment[0] = " + rid + " ORDER BY " + sortedBy + " " + sortDir; String json = null; OrientGraph graph = ServiceLocator.getInstance().getGraph(); try { OSQLSynchQuery<ODocument> query = new OSQLSynchQuery<ODocument>(sql); List<ODocument> list = graph.getRawGraph().command(query).execute(); if(list.size() > 0) { json = OJSONWriter.listToJSON(list, "rid,fetchPlan:[*]in_HasComment:-2 [*]out_ReportSpam:-2 [*]out_UpVote:-2 [*]out_DownVote:-2 in_Create[]:0 [*]out_Create:-2 [*]out_Update:-2 [*]out_HasComment:-1"); // need to fixed the in_Create within the json using json path. DocumentContext dc = JsonPath.parse(json); MapFunction propsFunction = new StripPropsMapFunction(); dc.map("$..in_UpVote[*]", propsFunction); dc.map("$..in_DownVote[*]", propsFunction); dc.map("$..in_ReportSpam[*]", propsFunction); MapFunction createFunction = new StripInCreateMapFunction(); json = dc.map("$..in_Create[0]", createFunction).jsonString(); } } catch (Exception e) { logger.error("Exception:", e); } finally { graph.shutdown(); } return json; }