From 606264451fd2b09a60836f8d77a405a67131aca8 Mon Sep 17 00:00:00 2001 From: Enrico Daga Date: Wed, 23 Mar 2022 17:42:08 +0000 Subject: [PATCH] Implement #233 --- .../sparqlanything/json/JSONTriplifier.java | 37 ++++++++++++++++++- .../json/test/MoreJSONTriplifierTest.java | 24 ++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) 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 d24e9ecc..1b197a32 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 @@ -295,8 +295,41 @@ private void transformMap(Map o, String dataSourceId, String containerId, @Override public void triplify(Properties properties, FacadeXGraphBuilder builder) throws IOException, TriplifierHTTPException { - // TODO Add support for JsonPath - transform(properties, builder); + + if(properties.containsKey("json.path") || properties.containsKey("json.path.1`")){ + transformFromJSONPath(properties, builder); + }else { + transform(properties, builder); + } + } + + private void transformFromJSONPath(Properties properties, FacadeXGraphBuilder builder) 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); + } + + try (us) { + collector.exec(); + Iterator>> matchesIterator = matches.iterator(); + // Only 1 data source expected + String rootId = Triplifier.getRootArgument(properties); + String dataSourceId = rootId; + builder.addRoot(dataSourceId, rootId); + int c = 0; + while(matchesIterator.hasNext()){ + Iterator it = matchesIterator.next().get().iterator(); + while(it.hasNext()){ + transformArrayItem(c, it.next(), dataSourceId, rootId, builder); + c++; + } + } + } } @Override 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 d1284fc7..55db0f3c 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 @@ -44,6 +44,10 @@ protected void properties(Properties properties) { properties.setProperty("blank-nodes", "false"); properties.setProperty("slice", "true"); properties.setProperty("json.path", "$.*"); + }else if(name.getMethodName().equals("testSliceArray$4")){ + properties.setProperty("blank-nodes", "false"); +// properties.setProperty("slice", "true"); + properties.setProperty("json.path", "$.*"); }else if(name.getMethodName().equals("testSliceArray_2$1")){ properties.setProperty("blank-nodes", "false"); // properties.setProperty("slice", "true"); @@ -86,6 +90,13 @@ protected void properties(Properties properties) { assertResultIsIsomorphicWithExpected(); } + @Test + public void testSliceArray$4(){ + L.info("Test simple array (one go + JsonPath)"); + //RDFDataMgr.write(System.err, result, Lang.N3); + assertResultIsIsomorphicWithExpected(); + } + @Test public void testSliceArray_2$1(){ L.info("Test array of objects (one go)"); @@ -104,6 +115,12 @@ protected void properties(Properties properties) { assertResultIsIsomorphicWithExpected(); } + @Test + public void testSliceArray_2$4(){ + L.info("Test array of objects (one go + JsonPath)"); + assertResultIsIsomorphicWithExpected(); + } + @Test public void testValueTypes_1$1(){ L.info("Test json value types (one go)"); @@ -124,4 +141,11 @@ protected void properties(Properties properties) { //RDFDataMgr.write(System.err, result, Lang.N3); assertResultIsIsomorphicWithExpected(); } + + @Test + public void testValueTypes_1$4(){ + L.info("Test json value types (one go + JsonPath)"); + //RDFDataMgr.write(System.err, result, Lang.N3); + assertResultIsIsomorphicWithExpected(); + } }