Author: cbrisson
Date: Sat Jan 20 09:11:49 2018
New Revision: 1821734
URL: http://svn.apache.org/viewvc?rev=1821734&view=rev
Log:
[tools] Nicify Json tool API
Added:
velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/JsonContent.java
Modified:
velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/JsonTool.java
Added:
velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/JsonContent.java
URL:
http://svn.apache.org/viewvc/velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/JsonContent.java?rev=1821734&view=auto
==============================================================================
---
velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/JsonContent.java
(added)
+++
velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/JsonContent.java
Sat Jan 20 09:11:49 2018
@@ -0,0 +1,205 @@
+package org.apache.velocity.tools.generic;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.util.Iterator;
+import java.util.Set;
+
+import org.json.simple.JSONArray;
+import org.json.simple.JSONObject;
+
+/**
+ * Container for *either* an array *or* an object
+ */
+
+public class JsonContent
+{
+ /**
+ * JSONObject content
+ */
+ private JSONObject jsonObject = null;
+
+ /**
+ * JSONArray content
+ */
+ private JSONArray jsonArray = null;
+
+ /**
+ * wraps the object into an hybrid JSON container if necessary
+ */
+ private static Object wrapIfNeeded(Object obj)
+ {
+ if (obj == null)
+ {
+ return obj;
+ }
+ else if (obj instanceof JSONArray)
+ {
+ return new JsonContent((JSONArray)obj);
+ }
+ else if (obj instanceof JSONObject)
+ {
+ return new JsonContent((JSONObject)obj);
+ }
+ else
+ {
+ return obj;
+ }
+ }
+
+ /**
+ * wraps an object
+ */
+ public JsonContent(JSONObject object)
+ {
+ jsonObject = object;
+ }
+
+ /**
+ * wraps an array
+ */
+ public JsonContent(JSONArray array)
+ {
+ jsonArray = array;
+ }
+
+ /**
+ * Get a value from root array
+ * @param key
+ * @return value, or null
+ */
+ public Object get(int index)
+ {
+ Object ret = null;
+ if (jsonArray != null)
+ {
+ ret = wrapIfNeeded(jsonArray.get(index));
+ }
+ else if (jsonObject != null)
+ {
+ ret = wrapIfNeeded(jsonObject.get(String.valueOf(index)));
+ }
+ return ret;
+ }
+
+ /**
+ * Get a property from root object
+ * @param key
+ * @return property value, or null
+ */
+ public Object get(String key)
+ {
+ Object ret = null;
+ if (jsonArray != null)
+ {
+ try
+ {
+ ret = wrapIfNeeded(jsonArray.get(Integer.parseInt(key)));
+ }
+ catch (NumberFormatException nfe) {}
+ }
+ else if (jsonObject != null)
+ {
+ ret = wrapIfNeeded(jsonObject.get(key));
+ }
+ return ret;
+ }
+
+ /**
+ * Iterate keys of root object.
+ * @return iterator
+ */
+ public Iterator<String> keys()
+ {
+ return jsonObject == null ? null : jsonObject.keySet().iterator();
+ }
+
+ /**
+ * Get set of root object keys.
+ * @return
+ */
+ public Set<String> keySet()
+ {
+ return jsonObject == null ? null : jsonObject.keySet();
+ }
+
+ /**
+ * Get an iterator. For a root object, returns an iterator over key names.
For a root array, returns an iterator
+ * over contained objects.
+ * @return iterator
+ */
+ public Iterator iterator()
+ {
+ if (jsonObject != null)
+ {
+ return jsonObject.keySet().iterator();
+ }
+ else if (jsonArray != null)
+ {
+ return jsonArray.iterator();
+ }
+ return null;
+ }
+
+ /**
+ * Get size of root object or array.
+ * @return size
+ */
+ public int size()
+ {
+ return jsonObject == null ? jsonArray == null ? null :
jsonArray.size() : jsonObject.size();
+ }
+
+ /**
+ * Convert JSON object or array into string
+ * @return JSON representation of the root object or array
+ */
+ public String toString()
+ {
+ return jsonObject == null ? jsonArray == null ? "null" :
jsonArray.toString() : jsonObject.toString();
+ }
+
+ /**
+ * Check if wrapped object is null
+ * @return true if wrapped object is null
+ */
+ public boolean isNull()
+ {
+ return jsonArray == null && jsonObject == null;
+ }
+
+ /**
+ * Check if wrapped object is a JSONObject
+ * @return true if wrapped object is a JSONObject
+ */
+ public boolean isObject()
+ {
+ return jsonObject != null;
+ }
+
+ /**
+ * Check if wrapped object is a JSONArray
+ * @return true if wrapped object is a JSONArray
+ */
+ public boolean isArray()
+ {
+ return jsonArray != null;
+ }
+}
Modified:
velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/JsonTool.java
URL:
http://svn.apache.org/viewvc/velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/JsonTool.java?rev=1821734&r1=1821733&r2=1821734&view=diff
==============================================================================
---
velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/JsonTool.java
(original)
+++
velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/JsonTool.java
Sat Jan 20 09:11:49 2018
@@ -79,153 +79,6 @@ import org.w3c.dom.Node;
@InvalidScope(Scope.SESSION)
public class JsonTool extends ImportSupport implements Iterable
{
-
- /**
- * Container for *either* an array *or* an object
- */
- public static class HybridJsonContainer
- {
- /**
- * JSONObject content
- */
- private JSONObject jsonObject = null;
-
- /**
- * JSONArray content
- */
- private JSONArray jsonArray = null;
-
- /**
- * wraps the object into an hybrid JSON container if necessary
- */
- private static Object wrapIfNeeded(Object obj)
- {
- if (obj == null)
- {
- return obj;
- }
- else if (obj instanceof JSONArray)
- {
- return new HybridJsonContainer((JSONArray)obj);
- }
- else if (obj instanceof JSONObject)
- {
- return new HybridJsonContainer((JSONObject)obj);
- }
- else
- {
- return obj;
- }
- }
-
- /**
- * wraps an object
- */
- public HybridJsonContainer(JSONObject object)
- {
- jsonObject = object;
- }
-
- /**
- * wraps an array
- */
- public HybridJsonContainer(JSONArray array)
- {
- jsonArray = array;
- }
-
- public Object get(int index)
- {
- Object ret = null;
- if (jsonArray != null)
- {
- ret = wrapIfNeeded(jsonArray.get(index));
- }
- else if (jsonObject != null)
- {
- ret = wrapIfNeeded(jsonObject.get(String.valueOf(index)));
- }
- return ret;
- }
-
- /**
- * Get a property from root object
- * @param key
- * @return property value, or null
- */
- public Object get(String key)
- {
- Object ret = null;
- if (jsonArray != null)
- {
- try
- {
- ret = wrapIfNeeded(jsonArray.get(Integer.parseInt(key)));
- }
- catch (NumberFormatException nfe) {}
- }
- else if (jsonObject != null)
- {
- ret = wrapIfNeeded(jsonObject.get(key));
- }
- return ret;
- }
-
- /**
- * Iterate keys of root object.
- * @return iterator
- */
- public Iterator<String> keys()
- {
- return jsonObject == null ? null : jsonObject.keySet().iterator();
- }
-
- /**
- * Get set of root object keys.
- * @return
- */
- public Set<String> keySet()
- {
- return jsonObject == null ? null : jsonObject.keySet();
- }
-
- /**
- * Get an iterator. For a root object, returns an iterator over key
names. For a root array, returns an iterator
- * over contained objects.
- * @return iterator
- */
- public Iterator iterator()
- {
- if (jsonObject != null)
- {
- return jsonObject.keySet().iterator();
- }
- else if (jsonArray != null)
- {
- return jsonArray.iterator();
- }
- return null;
- }
-
- /**
- * Get size of root object or array.
- * @return size
- */
- public int size()
- {
- return jsonObject == null ? jsonArray == null ? null :
jsonArray.size() : jsonObject.size();
- }
-
- /**
- * Convert JSON object or array into string
- * @return JSON representation of the root object or array
- */
- public String toString()
- {
- return jsonObject == null ? jsonArray == null ? "null" :
jsonArray.toString() : jsonObject.toString();
- }
- }
-
/**
* ImportSupport utility which provides underlying i/o
*/
@@ -241,7 +94,7 @@ public class JsonTool extends ImportSupp
importSupport.configure(config);
}
- private HybridJsonContainer root = null;
+ private JsonContent root = null;
/**
* Looks for the "file" parameter and automatically uses
@@ -294,11 +147,11 @@ public class JsonTool extends ImportSupp
Object result = new JSONParser().parse(reader);
if (result instanceof JSONObject)
{
- root = new HybridJsonContainer((JSONObject)result);
+ root = new JsonContent((JSONObject)result);
}
else if (result instanceof JSONArray)
{
- root = new HybridJsonContainer((JSONArray)result);
+ root = new JsonContent((JSONArray)result);
}
else throw new Exception("Expecting JSON array or object");
}
@@ -313,7 +166,7 @@ public class JsonTool extends ImportSupp
* Parses the given JSON string and uses the resulting {@link Document}
* as the root {@link Node}.
*/
- public void parse(String json)
+ public JsonTool parse(String json)
{
if (json != null)
{
@@ -326,12 +179,13 @@ public class JsonTool extends ImportSupp
getLog().error("could not parse given JSON string", e);
}
}
+ return this;
}
/**
* Reads and parses a local JSON resource file
*/
- public void read(String resource)
+ public JsonTool read(String resource)
{
if (resource != null)
{
@@ -360,12 +214,13 @@ public class JsonTool extends ImportSupp
}
}
}
+ return this;
}
/**
* Reads and parses a remote or local URL
*/
- public void fetch(String url)
+ public JsonTool fetch(String url)
{
if (url != null)
{
@@ -394,6 +249,7 @@ public class JsonTool extends ImportSupp
}
}
}
+ return this;
}
/**