diff --git a/sparql-anything-json/src/main/java/com/github/sparqlanything/json/JSONTriplifier.java b/sparql-anything-json/src/main/java/com/github/sparqlanything/json/JSONTriplifier.java index 1b197a32..867a40e6 100644 --- a/sparql-anything-json/src/main/java/com/github/sparqlanything/json/JSONTriplifier.java +++ b/sparql-anything-json/src/main/java/com/github/sparqlanything/json/JSONTriplifier.java @@ -296,19 +296,20 @@ private void transformMap(Map o, String dataSourceId, String containerId, public void triplify(Properties properties, FacadeXGraphBuilder builder) throws IOException, TriplifierHTTPException { - if(properties.containsKey("json.path") || properties.containsKey("json.path.1`")){ - transformFromJSONPath(properties, builder); + List jsonPaths = Triplifier.getPropertyValues(properties, "json.path"); + if(!jsonPaths.isEmpty()){ + transformFromJSONPath(properties, builder, jsonPaths); }else { transform(properties, builder); } } - private void transformFromJSONPath(Properties properties, FacadeXGraphBuilder builder) throws TriplifierHTTPException, IOException { + private void transformFromJSONPath(Properties properties, FacadeXGraphBuilder builder, List jsonPaths) throws TriplifierHTTPException, IOException { JsonSurfer surfer = new JsonSurfer(JacksonParser.INSTANCE, JacksonProvider.INSTANCE); final InputStream us = Triplifier.getInputStream(properties); Collector collector = surfer.collector(us); Set>> matches = new HashSet>>(); - List jsonPaths = Triplifier.getPropertyValues(properties, "json.path"); + for(String jpath: jsonPaths) { ValueBox> m = collector.collectAll(jpath); matches.add(m); @@ -488,7 +489,8 @@ public Slice next() { @Override public Iterable slice(Properties properties) throws IOException, TriplifierHTTPException { - if(properties.containsKey("json.path") || properties.containsKey("json.path.1`")){ + List jsonPaths = Triplifier.getPropertyValues(properties, "json.path"); + if(!jsonPaths.isEmpty()){ return sliceFromJSONPath(properties); }else { return sliceFromArray(properties); diff --git a/sparql-anything-json/src/test/java/com/github/sparqlanything/json/test/BaseTest.java b/sparql-anything-json/src/test/java/com/github/sparqlanything/json/test/BaseTest.java index bb708d9b..24aabef9 100644 --- a/sparql-anything-json/src/test/java/com/github/sparqlanything/json/test/BaseTest.java +++ b/sparql-anything-json/src/test/java/com/github/sparqlanything/json/test/BaseTest.java @@ -39,6 +39,8 @@ import org.jsfr.json.JsonSurferJackson; import org.jsfr.json.ValueBox; import org.jsfr.json.compiler.JsonPathCompiler; +import org.jsfr.json.provider.JacksonProvider; +import org.junit.Ignore; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -82,34 +84,27 @@ public void testContent() throws TriplifierHTTPException { } // -// @Test -// public void jsonPathTest(){ -// InputStream is = getClass().getClassLoader().getResourceAsStream("testarray.json"); -//// InputStream is = getClass().getClassLoader().getResourceAsStream("SliceArray_2.json"); -////// JsonSurfer surfer = new JsonSurfer(JacksonParser.INSTANCE, JacksonProvider.INSTANCE); -//// Collector collector = surfer.collector(is); -//// ValueBox> matches = collector.collectAll("$.*"); -//// collector.exec(); -// final List parsers = new ArrayList(); -// JsonSurfer surfer = JsonSurferJackson.createSurfer(new JsonFactory(){ -// @Override -// public JsonParser createParser(InputStream f) throws IOException { -// parsers.add (super.createParser(f)); -// return parsers.get(parsers.size() - 1); -// } -// }); -// Collector collector = surfer.collector(is); -// ValueBox> matches = collector.collectAll("$.*"); -// collector.exec(); -// Iterator iter = matches.get().iterator(); -// while(iter.hasNext()){ -// Object o = iter.next(); -// System.err.println(o.getClass()); -// //ObjectNode on = (ObjectNode) o; -// Iterator iten = ((Map) o).entrySet().iterator(); -// while(iten.hasNext() ) { -// System.err.println(iten.next()); -// } -// } -// } + @Ignore + @Test + public void jsonPathTest(){ + InputStream is = getClass().getClassLoader().getResourceAsStream("MultiJsonPath.json"); +// InputStream is = getClass().getClassLoader().getResourceAsStream("SliceArray_2.json"); + JsonSurfer surfer = new JsonSurfer(JacksonParser.INSTANCE, JacksonProvider.INSTANCE); + Collector collector = surfer.collector(is); + ValueBox> matches = collector.collectAll("$..[?(@.number == 1.2)]"); + collector.exec(); + + Iterator iter = matches.get().iterator(); + while(iter.hasNext()){ + Object o = iter.next(); + System.err.println(o.getClass()); + //ObjectNode on = (ObjectNode) o; + if(o instanceof Map) { + Iterator iten = ((Map) o).entrySet().iterator(); + while (iten.hasNext()) { + System.err.println(" > " + iten.next()); + } + } + } + } } diff --git a/sparql-anything-json/src/test/java/com/github/sparqlanything/json/test/MoreJSONTriplifierTest.java b/sparql-anything-json/src/test/java/com/github/sparqlanything/json/test/MoreJSONTriplifierTest.java index ad9e6b2d..6eaaddc2 100644 --- a/sparql-anything-json/src/test/java/com/github/sparqlanything/json/test/MoreJSONTriplifierTest.java +++ b/sparql-anything-json/src/test/java/com/github/sparqlanything/json/test/MoreJSONTriplifierTest.java @@ -103,6 +103,14 @@ protected void properties(Properties properties) { properties.setProperty("slice", "true"); properties.setProperty("json.path", "$.content"); } + + // testMultiJsonPath$1 + if(name.getMethodName().equals("testMultiJsonPath$1")){ + properties.setProperty("blank-nodes", "false"); + // properties.setProperty("slice", "true"); + properties.setProperty("json.path.1", "$..[?(@.letter == 'A')]"); + properties.setProperty("json.path.2", "$..[?(@.number == 2)]"); + } } @Test @@ -207,7 +215,17 @@ protected void properties(Properties properties) { @Test public void testSliceObject$3(){ L.info("Test simple Json object (slicing + JsonPath)"); - RDFDataMgr.write(System.err, result, Lang.N3); +// RDFDataMgr.write(System.err, result, Lang.N3); + assertResultIsIsomorphicWithExpected(); // Assert.assertTrue(resultException.getMessage().equals("Not a JSON array")); } + + @Test + public void testMultiJsonPath$1(){ + L.info("Test multiple json paths (one go + JsonPath)"); + System.out.println(result.size()); + RDFDataMgr.write(System.err, result, Lang.N3); + // XXX output graph is not deterministic as the order of the solutions can change ... + Assert.assertTrue(result.size() == 13); + } } diff --git a/sparql-anything-json/src/test/resources/MultiJsonPath.json b/sparql-anything-json/src/test/resources/MultiJsonPath.json new file mode 100644 index 00000000..19adc364 --- /dev/null +++ b/sparql-anything-json/src/test/resources/MultiJsonPath.json @@ -0,0 +1,6 @@ +[ + {"letter" : "A", "number": 1, "boolean": true}, + {"letter" : "B", "number": 2, "boolean": false, "next": {"letter" : "X", "number": 1.1, "boolean": true } }, + {"letter" : "C", "number": 3, "boolean": true, "next": {"letter" : "X", "number": 1.2, "boolean": false } }, + {"letter" : "D", "number": 4, "boolean": true, "next": {"letter" : "X", "number": 1.3, "boolean": true } } +] \ No newline at end of file diff --git a/sparql-anything-json/src/test/resources/MultiJsonPath.ttl b/sparql-anything-json/src/test/resources/MultiJsonPath.ttl new file mode 100644 index 00000000..112ced04 --- /dev/null +++ b/sparql-anything-json/src/test/resources/MultiJsonPath.ttl @@ -0,0 +1,38 @@ +@prefix rdf: . +@prefix rdfs: . +@prefix fx: . +@prefix xyz: . +@prefix xsd: . + + + + true ; + + "A" ; + + "1"^^ . + + + a ; + + ; + + . + + + + true ; + + "X" ; + + "1.1"^^ . + + + + false ; + + "B" ; + + ; + + "2"^^ . \ No newline at end of file