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 2016/04/25 13:04:05 UTC
svn commit: r1740808 - in /sling/trunk/bundles/commons/json/src:
main/java/org/apache/sling/commons/json/io/JSONWriter.java
main/java/org/apache/sling/commons/json/io/package-info.java
test/java/org/apache/sling/commons/json/JSONWriterTest.java
Author: rombert
Date: Mon Apr 25 11:04:04 2016
New Revision: 1740808
URL: http://svn.apache.org/viewvc?rev=1740808&view=rev
Log:
SLING-5146 Add JSONObject/JSONArray support to JSONWriter
Closes #106
Submitted-By: Damien Antipa
Modified:
sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/io/JSONWriter.java
sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/io/package-info.java
sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/JSONWriterTest.java
Modified: sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/io/JSONWriter.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/io/JSONWriter.java?rev=1740808&r1=1740807&r2=1740808&view=diff
==============================================================================
--- sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/io/JSONWriter.java (original)
+++ sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/io/JSONWriter.java Mon Apr 25 11:04:04 2016
@@ -2,7 +2,9 @@ package org.apache.sling.commons.json.io
import java.io.IOException;
import java.io.Writer;
+import java.util.Iterator;
+import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
@@ -369,4 +371,74 @@ public class JSONWriter {
public JSONWriter value(Object o) throws JSONException {
return this.append(JSONObject.valueToString(o));
}
+
+ /**
+ * Append a JSON Object
+ *
+ * @param o
+ * @return
+ * @throws JSONException
+ */
+ public JSONWriter writeObject(JSONObject o) throws JSONException {
+ Iterator<String> keys = o.keys();
+
+ this.object();
+
+ while (keys.hasNext()) {
+ String key = keys.next();
+
+ this.key(key);
+
+ JSONObject objVal = o.optJSONObject(key);
+ if (objVal != null) {
+ this.writeObject(objVal);
+ continue;
+ }
+
+ JSONArray arrVal = o.optJSONArray(key);
+ if (arrVal != null) {
+ this.writeArray(arrVal);
+ continue;
+ }
+
+ Object obj = o.opt(key);
+ this.value(obj);
+ }
+
+ this.endObject();
+
+ return this;
+ }
+
+ /**
+ * Append a JSON Array
+ *
+ * @param a
+ * @return
+ * @throws JSONException
+ */
+ public JSONWriter writeArray(JSONArray a) throws JSONException {
+ this.array();
+
+ for (int i = 0; i < a.length(); i++) {
+ JSONObject objVal = a.optJSONObject(i);
+ if (objVal != null) {
+ this.writeObject(objVal);
+ continue;
+ }
+
+ JSONArray arrVal = a.optJSONArray(i);
+ if (arrVal != null) {
+ this.writeArray(arrVal);
+ continue;
+ }
+
+ Object obj = a.opt(i);
+ this.value(obj);
+ }
+
+ this.endArray();
+
+ return this;
+ }
}
Modified: sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/io/package-info.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/io/package-info.java?rev=1740808&r1=1740807&r2=1740808&view=diff
==============================================================================
--- sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/io/package-info.java (original)
+++ sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/io/package-info.java Mon Apr 25 11:04:04 2016
@@ -17,7 +17,7 @@
* under the License.
*/
-@Version("2.1.0")
+@Version("2.2.0")
package org.apache.sling.commons.json.io;
import aQute.bnd.annotation.Version;
Modified: sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/JSONWriterTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/JSONWriterTest.java?rev=1740808&r1=1740807&r2=1740808&view=diff
==============================================================================
--- sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/JSONWriterTest.java (original)
+++ sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/JSONWriterTest.java Mon Apr 25 11:04:04 2016
@@ -46,6 +46,19 @@ public class JSONWriterTest {
w.endObject();
return new DespacedRendering(output.toString());
}
+
+ private DespacedRendering writeObject() throws JSONException {
+ JSONArray arr = new JSONArray();
+ arr.put(1).put("two").put(3.0).put(false);
+
+ w.writeObject(
+ new JSONObject()
+ .put("foo", "bar")
+ .put("array", arr)
+ );
+
+ return new DespacedRendering(output.toString());
+ }
@Test
public void testSetTidy() {
@@ -61,6 +74,14 @@ public class JSONWriterTest {
"_foo_:_bar_",
"_array_:[1,_two_,3,false]");
}
+
+ @Test
+ public void testStandardObjectWrite() throws JSONException {
+ final DespacedRendering r = writeObject();
+ r.expect(
+ "_foo_:_bar_",
+ "_array_:[1,_two_,3,false]");
+ }
@Test
public void testTidyWrite() throws JSONException {
@@ -69,6 +90,15 @@ public class JSONWriterTest {
r.expect(
"-nl-_foo_:_bar_",
"-nl-_array_:[-nl-1,-nl-_two_,-nl-3,-nl-false-nl-]");
+ }
+
+ @Test
+ public void testTidyObjectWrite() throws JSONException {
+ w.setTidy(true);
+ final DespacedRendering r = writeObject();
+ r.expect(
+ "-nl-_foo_:_bar_",
+ "-nl-_array_:[-nl-1,-nl-_two_,-nl-3,-nl-false-nl-]");
}
@Test