You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:59:02 UTC

[sling-org-apache-sling-resource-inventory] 04/07: SLING-6898: Remove commons.json from Resource Inventory.

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.resource.inventory-1.0.8
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-resource-inventory.git

commit 0159c943787290710b5ae2df7a4278e7caa70dda
Author: Karl Pauls <pa...@apache.org>
AuthorDate: Wed May 31 12:51:16 2017 +0000

    SLING-6898: Remove commons.json from Resource Inventory.
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/contrib/extensions/resource-inventory@1797029 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            |  4 +-
 .../resource/inventory/impl/JsonObjectCreator.java | 79 +++++++++++-----------
 .../impl/ResourceInventoryPrinterFactory.java      | 13 ++--
 .../resource/inventory/impl/ResourceTraversor.java | 33 ++++-----
 .../inventory/impl/JsonObjectCreatorTest.java      | 71 +++++++++----------
 .../inventory/impl/ResourceTraversorTest.java      | 15 ++--
 6 files changed, 113 insertions(+), 102 deletions(-)

diff --git a/pom.xml b/pom.xml
index e7f09df..76f494e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -75,8 +75,8 @@
         </dependency>
         <dependency>
             <groupId>org.apache.sling</groupId>
-            <artifactId>org.apache.sling.commons.json</artifactId>
-            <version>2.0.6</version>
+            <artifactId>org.apache.sling.commons.johnzon</artifactId>
+            <version>1.0.0</version>
             <scope>provided</scope>
         </dependency>
         <dependency>
diff --git a/src/main/java/org/apache/sling/resource/inventory/impl/JsonObjectCreator.java b/src/main/java/org/apache/sling/resource/inventory/impl/JsonObjectCreator.java
index 34d5d23..b08e418 100644
--- a/src/main/java/org/apache/sling/resource/inventory/impl/JsonObjectCreator.java
+++ b/src/main/java/org/apache/sling/resource/inventory/impl/JsonObjectCreator.java
@@ -20,18 +20,21 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
-import java.util.Arrays;
 import java.util.Calendar;
 import java.util.Iterator;
 import java.util.Locale;
 import java.util.Map;
 
+import javax.json.Json;
+import javax.json.JsonArrayBuilder;
+import javax.json.JsonException;
+import javax.json.JsonObject;
+import javax.json.JsonObjectBuilder;
+import javax.json.JsonValue;
+
 import org.apache.commons.lang3.ArrayUtils;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ValueMap;
-import org.apache.sling.commons.json.JSONArray;
-import org.apache.sling.commons.json.JSONException;
-import org.apache.sling.commons.json.JSONObject;
 import org.slf4j.LoggerFactory;
 
 /**
@@ -41,8 +44,7 @@ import org.slf4j.LoggerFactory;
 public abstract class JsonObjectCreator {
 
     /** Dump given resource in JSON, optionally recursing into its objects */
-    public static JSONObject create(final Resource resource)
-    throws JSONException {
+    public static JsonObjectBuilder create(final Resource resource) {
         final ValueMap valueMap = resource.adaptTo(ValueMap.class);
 
         @SuppressWarnings("unchecked")
@@ -50,7 +52,7 @@ public abstract class JsonObjectCreator {
                 ? valueMap
                 : resource.adaptTo(Map.class);
 
-        final JSONObject obj = new JSONObject();
+        final JsonObjectBuilder obj = Json.createObjectBuilder();
 
         if (propertyMap == null) {
 
@@ -59,22 +61,26 @@ public abstract class JsonObjectCreator {
             if (value != null) {
 
                 // single value property or just plain String resource or...
-                obj.put(resource.getName(), value);
+                obj.add(resource.getName(), value);
 
             } else {
 
                 // Try multi-value "property"
                 final String[] values = resource.adaptTo(String[].class);
                 if (values != null) {
-                    obj.put(resource.getName(), new JSONArray(Arrays.asList(values)));
+                    JsonArrayBuilder array = Json.createArrayBuilder();
+                    for (String v : values) {
+                        array.add(v);
+                    }
+                    obj.add(resource.getName(), array);
                 }
 
             }
             if ( resource.getResourceType() != null ) {
-                obj.put("sling:resourceType", resource.getResourceType());
+                obj.add("sling:resourceType", resource.getResourceType());
             }
             if ( resource.getResourceSuperType() != null ) {
-                obj.put("sling:resourceSuperType", resource.getResourceSuperType());
+                obj.add("sling:resourceSuperType", resource.getResourceSuperType());
             }
 
         } else {
@@ -117,35 +123,36 @@ public abstract class JsonObjectCreator {
     }
 
     /** Dump only a value in the correct format */
-    private static Object getValue(final Object value) {
+    private static JsonValue getValue(final Object value) {
+        JsonObjectBuilder builder = Json.createObjectBuilder();
         if ( value instanceof InputStream ) {
             // input stream is already handled
-            return 0;
+            builder.add("key", 0);
         } else if ( value instanceof Calendar ) {
-            return format((Calendar)value);
+            builder.add("key",format((Calendar)value));
         } else if ( value instanceof Boolean ) {
-            return value;
+            builder.add("key", (Boolean) value);
         } else if ( value instanceof Long ) {
-            return value;
+            builder.add("key", (Long) value);
         } else if ( value instanceof Integer ) {
-            return value;
+            builder.add("key", (Integer) value);
         } else if ( value != null ) {
-            return value.toString();
+            builder.add("key", value.toString());
         } else {
-            return ""; // assume empty string
+            builder.add("key", ""); // assume empty string
         }
+        return builder.build().get("key");
     }
 
     /** Dump a single node */
-    private static void createSingleResource(final Resource n, final JSONObject parent)
-            throws JSONException {
-        parent.put(n.getName(), create(n));
+    private static void createSingleResource(final Resource n, final JsonObjectBuilder parent) {
+        parent.add(n.getName(), create(n));
     }
 
     /**
      * Write a single property
      */
-    private static void createProperty(final JSONObject obj,
+    private static void createProperty(final JsonObjectBuilder obj,
                                  final ValueMap valueMap,
                                  final String key,
                                  final Object value) {
@@ -174,11 +181,7 @@ public abstract class JsonObjectCreator {
             }
             // write out empty array
             if ( values.length == 0 ) {
-                try {
-                    obj.put(key, new JSONArray());
-                } catch ( final JSONException ignore ) {
-                    // we ignore this
-                }
+                obj.add(key, Json.createArrayBuilder());
                 return;
             }
         }
@@ -192,15 +195,15 @@ public abstract class JsonObjectCreator {
             // in the name, and the value should be the size of the binary data
             try {
                 if (values == null) {
-                    obj.put(":" + key, getLength(valueMap, -1, key, (InputStream)value));
+                    obj.add(":" + key, getLength(valueMap, -1, key, (InputStream)value));
                 } else {
-                    final JSONArray result = new JSONArray();
+                    final JsonArrayBuilder result = Json.createArrayBuilder();
                     for (int i = 0; i < values.length; i++) {
-                        result.put(getLength(valueMap, i, key, (InputStream)values[i]));
+                        result.add(getLength(valueMap, i, key, (InputStream)values[i]));
                     }
-                    obj.put(":" + key, result);
+                    obj.add(":" + key, result);
                 }
-            } catch ( final JSONException ignore ) {
+            } catch ( final JsonException ignore ) {
                 // we ignore this
                 LoggerFactory.getLogger(JsonObjectCreator.class).warn("Unable to create JSON value", ignore);
             }
@@ -209,15 +212,15 @@ public abstract class JsonObjectCreator {
 
         try {
             if (!value.getClass().isArray()) {
-                obj.put(key, getValue(value));
+                obj.add(key, getValue(value));
             } else {
-                final JSONArray result = new JSONArray();
+                final JsonArrayBuilder result = Json.createArrayBuilder();
                 for (Object v : values) {
-                    result.put(getValue(v));
+                    result.add(getValue(v));
                 }
-                obj.put(key, result);
+                obj.add(key, result);
             }
-        } catch ( final JSONException ignore ) {
+        } catch ( final JsonException ignore ) {
             // we ignore this
             LoggerFactory.getLogger(JsonObjectCreator.class).warn("Unable to create JSON value", ignore);
         }
diff --git a/src/main/java/org/apache/sling/resource/inventory/impl/ResourceInventoryPrinterFactory.java b/src/main/java/org/apache/sling/resource/inventory/impl/ResourceInventoryPrinterFactory.java
index a3618e4..347619e 100644
--- a/src/main/java/org/apache/sling/resource/inventory/impl/ResourceInventoryPrinterFactory.java
+++ b/src/main/java/org/apache/sling/resource/inventory/impl/ResourceInventoryPrinterFactory.java
@@ -19,6 +19,11 @@
 package org.apache.sling.resource.inventory.impl;
 
 import java.io.PrintWriter;
+import java.io.StringWriter;
+
+import javax.json.Json;
+import javax.json.JsonException;
+import javax.json.stream.JsonGenerator;
 
 import org.apache.felix.inventory.Format;
 import org.apache.felix.inventory.InventoryPrinter;
@@ -26,7 +31,6 @@ import org.apache.sling.api.resource.LoginException;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.api.resource.ResourceResolverFactory;
-import org.apache.sling.commons.json.JSONException;
 import org.osgi.service.component.annotations.Activate;
 import org.osgi.service.component.annotations.Component;
 import org.osgi.service.component.annotations.ConfigurationPolicy;
@@ -84,12 +88,13 @@ public class ResourceInventoryPrinterFactory implements InventoryPrinter {
             if ( rootResource != null ) {
                 final ResourceTraversor rt = new ResourceTraversor(rootResource);
                 rt.collectResources();
-                printWriter.write(rt.getJSONObject().toString(2));
-
+                StringWriter writer = new StringWriter();
+                Json.createGenerator(writer).write(rt.getJsonObject()).close();
+                printWriter.write(writer.toString());
             }
         } catch (final LoginException e) {
             // ignore
-        } catch (final JSONException ignore) {
+        } catch (final JsonException ignore) {
             LoggerFactory.getLogger(this.getClass()).warn("Unable to create resource json", ignore);
         } finally {
             if ( resolver != null ) {
diff --git a/src/main/java/org/apache/sling/resource/inventory/impl/ResourceTraversor.java b/src/main/java/org/apache/sling/resource/inventory/impl/ResourceTraversor.java
index ed2f161..0a8d960 100644
--- a/src/main/java/org/apache/sling/resource/inventory/impl/ResourceTraversor.java
+++ b/src/main/java/org/apache/sling/resource/inventory/impl/ResourceTraversor.java
@@ -19,18 +19,19 @@ package org.apache.sling.resource.inventory.impl;
 
 import java.util.Iterator;
 
+import javax.json.JsonException;
+import javax.json.JsonObject;
+import javax.json.JsonObjectBuilder;
+
 import org.apache.sling.api.resource.Resource;
-import org.apache.sling.commons.json.JSONException;
-import org.apache.sling.commons.json.JSONObject;
 
 public class ResourceTraversor {
 
-    private final JSONObject startObject;
+    private final JsonObjectBuilder startObject;
 
     private final Resource startResource;
 
-    public ResourceTraversor(final Resource resource)
-    throws JSONException {
+    public ResourceTraversor(final Resource resource) {
         this.startResource = resource;
         this.startObject = this.adapt(resource);
     }
@@ -40,7 +41,7 @@ public class ResourceTraversor {
      * startObject.
      * @throws JSONException
      */
-    public void collectResources() throws JSONException {
+    public void collectResources() throws JsonException {
         collectChildren(startResource, this.startObject);
     }
 
@@ -50,13 +51,13 @@ public class ResourceTraversor {
      * @throws JSONException
      */
     private void collectChildren(final Resource resource,
-            final JSONObject jsonObj)
-    throws JSONException {
+            final JsonObjectBuilder jsonObj)
+    throws JsonException {
 
         final Iterator<Resource> children = resource.listChildren();
         while (children.hasNext()) {
             final Resource res = children.next();
-            final JSONObject json = collectResource(res, jsonObj);
+            final JsonObjectBuilder json = collectResource(res, jsonObj);
             collectChildren(res, json);
         }
     }
@@ -68,10 +69,10 @@ public class ResourceTraversor {
      * @param level The level where this resource is located.
      * @throws JSONException
      */
-    private JSONObject collectResource(final Resource resource, final JSONObject parent)
-    throws JSONException {
-        final JSONObject o = adapt(resource);
-        parent.put(resource.getName(), o);
+    private JsonObjectBuilder collectResource(final Resource resource, final JsonObjectBuilder parent)
+    throws JsonException {
+        final JsonObjectBuilder o = adapt(resource);
+        parent.add(resource.getName(), o);
         return o;
     }
 
@@ -82,11 +83,11 @@ public class ResourceTraversor {
      * @return The JSON representation of the Resource
      * @throws JSONException
      */
-    private JSONObject adapt(final Resource resource) throws JSONException {
+    private JsonObjectBuilder adapt(final Resource resource) throws JsonException {
         return JsonObjectCreator.create(resource);
     }
 
-    public JSONObject getJSONObject() {
-        return startObject;
+    public JsonObject getJsonObject() {
+        return startObject.build();
     }
 }
diff --git a/src/test/java/org/apache/sling/resource/inventory/impl/JsonObjectCreatorTest.java b/src/test/java/org/apache/sling/resource/inventory/impl/JsonObjectCreatorTest.java
index 4b072b0..5468951 100644
--- a/src/test/java/org/apache/sling/resource/inventory/impl/JsonObjectCreatorTest.java
+++ b/src/test/java/org/apache/sling/resource/inventory/impl/JsonObjectCreatorTest.java
@@ -19,11 +19,12 @@ package org.apache.sling.resource.inventory.impl;
 import java.util.HashMap;
 import java.util.Map;
 
+import javax.json.JsonArray;
+import javax.json.JsonObject;
+
 import org.apache.sling.api.resource.LoginException;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceResolver;
-import org.apache.sling.commons.json.JSONArray;
-import org.apache.sling.commons.json.JSONObject;
 import org.apache.sling.testing.resourceresolver.MockHelper;
 import org.apache.sling.testing.resourceresolver.MockResource;
 import org.apache.sling.testing.resourceresolver.MockResourceResolverFactory;
@@ -60,15 +61,15 @@ public class JsonObjectCreatorTest {
         });
         Resource resource = new MockResource("/some/path", properties, resolver);
 
-        JSONObject json = JsonObjectCreator.create(resource);
+        JsonObject json = JsonObjectCreator.create(resource).build();
 
-        assertEquals(10, json.get("byte"));
-        assertEquals(10, json.get("int"));
-        assertEquals("10.0", json.get("float"));
-        assertEquals("10.0", json.get("double"));
-        assertEquals("10", json.get("string"));
-        assertEquals(false, json.get("boolean"));
-        assertEquals("object", json.get("object"));
+        assertEquals(10, json.getInt("byte"));
+        assertEquals(10, json.getInt("int"));
+        assertEquals("10.0", json.getString("float"));
+        assertEquals("10.0", json.getString("double"));
+        assertEquals("10", json.getString("string"));
+        assertEquals(false, json.getBoolean("boolean"));
+        assertEquals("object", json.getString("object"));
     }
 
     @Test
@@ -87,27 +88,27 @@ public class JsonObjectCreatorTest {
         properties.put("charArray", new char[]{'a', 'b'});
         Resource resource = new MockResource("/some/path", properties, resolver);
 
-        JSONObject json = JsonObjectCreator.create(resource);
-        assertEquals(0, json.getJSONArray("emptyArray").length());
-        JSONArray array;
-        array = json.getJSONArray("stringArray");
-        assertEquals("10", array.get(0));
-        array = json.getJSONArray("intArray");
-        assertEquals(10, array.get(0));
-        array = json.getJSONArray("doubleArray");
-        assertEquals("10.0", array.get(0));
-        array = json.getJSONArray("byteArray");
-        assertEquals("10", array.get(0));
-        array = json.getJSONArray("floatArray");
-        assertEquals("10.0", array.get(0));
-        array = json.getJSONArray("shortArray");
-        assertEquals("10", array.get(0));
-        array = json.getJSONArray("longArray");
-        assertEquals(10L, array.get(0));
-        array = json.getJSONArray("booleanArray");
-        assertEquals(true, array.get(0));
-        array = json.getJSONArray("charArray");
-        assertEquals("a", array.get(0));
+        JsonObject json = JsonObjectCreator.create(resource).build();
+        assertEquals(0, json.getJsonArray("emptyArray").size());
+        JsonArray array;
+        array = json.getJsonArray("stringArray");
+        assertEquals("10", array.getString(0));
+        array = json.getJsonArray("intArray");
+        assertEquals(10, array.getInt(0));
+        array = json.getJsonArray("doubleArray");
+        assertEquals("10.0", array.getString(0));
+        array = json.getJsonArray("byteArray");
+        assertEquals("10", array.getString(0));
+        array = json.getJsonArray("floatArray");
+        assertEquals("10.0", array.getString(0));
+        array = json.getJsonArray("shortArray");
+        assertEquals("10", array.getString(0));
+        array = json.getJsonArray("longArray");
+        assertEquals(10L, array.getJsonNumber(0).longValue());
+        array = json.getJsonArray("booleanArray");
+        assertEquals(true, array.getBoolean(0));
+        array = json.getJsonArray("charArray");
+        assertEquals("a", array.getString(0));
 
     }
 
@@ -122,10 +123,10 @@ public class JsonObjectCreatorTest {
                 .commit();
         Resource resource = resolver.getResource("/some");
 
-        JSONObject json = JsonObjectCreator.create(resource);
-        assertEquals("v1", json.get("p1"));
-        JSONObject path = json.getJSONObject("path");
-        assertEquals("v2", path.get("p2"));
+        JsonObject json = JsonObjectCreator.create(resource).build();
+        assertEquals("v1", json.getString("p1"));
+        JsonObject path = json.getJsonObject("path");
+        assertEquals("v2", path.getString("p2"));
 
     }
 }
\ No newline at end of file
diff --git a/src/test/java/org/apache/sling/resource/inventory/impl/ResourceTraversorTest.java b/src/test/java/org/apache/sling/resource/inventory/impl/ResourceTraversorTest.java
index 4a6a767..098d112 100644
--- a/src/test/java/org/apache/sling/resource/inventory/impl/ResourceTraversorTest.java
+++ b/src/test/java/org/apache/sling/resource/inventory/impl/ResourceTraversorTest.java
@@ -18,10 +18,11 @@ package org.apache.sling.resource.inventory.impl;
 
 import java.util.Collections;
 
+import javax.json.JsonObject;
+
 import org.apache.sling.api.resource.LoginException;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceResolver;
-import org.apache.sling.commons.json.JSONObject;
 import org.apache.sling.testing.resourceresolver.MockHelper;
 import org.apache.sling.testing.resourceresolver.MockResource;
 import org.apache.sling.testing.resourceresolver.MockResourceResolverFactory;
@@ -52,19 +53,19 @@ public class ResourceTraversorTest {
         Resource resource = resolver.getResource("/some");
         ResourceTraversor traversor = new ResourceTraversor(resource);
         traversor.collectResources();
-        JSONObject json = traversor.getJSONObject();
-        assertEquals("v1", json.get("p1"));
-        JSONObject path = json.getJSONObject("path");
+        JsonObject json = traversor.getJsonObject();
+        assertEquals("v1", json.getString("p1"));
+        JsonObject path = json.getJsonObject("path");
         assertNotNull(path);
-        assertEquals("v2", path.get("p2"));
+        assertEquals("v2", path.getString("p2"));
 
     }
 
     @Test
     public void testGetJSONObject() throws Exception {
         Resource resource = new MockResource("/some/path", Collections.<String, Object>singletonMap("p1", "v1"), resolver);
-        JSONObject json = new ResourceTraversor(resource).getJSONObject();
-        assertEquals("v1", json.get("p1"));
+        JsonObject json = new ResourceTraversor(resource).getJsonObject();
+        assertEquals("v1", json.getString("p1"));
 
     }
 

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.