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 2015/10/22 12:09:29 UTC
svn commit: r1709977 - in /sling/trunk/bundles/commons/json/src:
main/java/org/apache/sling/commons/json/io/JSONRenderer.java
test/java/org/apache/sling/commons/json/io/JSONRendererTest.java
Author: rombert
Date: Thu Oct 22 10:09:29 2015
New Revision: 1709977
URL: http://svn.apache.org/viewvc?rev=1709977&view=rev
Log:
SLING-5184 - JSONRenderer.quote should not use synchronized StringWriter
with unspecified length
* Use StringBuilder instead of StringWriter
* Initalize StringBuilder with the minimum length
Submitted-By: Joel Richard
Closes #108
Modified:
sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/io/JSONRenderer.java
sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/io/JSONRendererTest.java
Modified: sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/io/JSONRenderer.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/io/JSONRenderer.java?rev=1709977&r1=1709976&r2=1709977&view=diff
==============================================================================
--- sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/io/JSONRenderer.java (original)
+++ sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/io/JSONRenderer.java Thu Oct 22 10:09:29 2015
@@ -17,7 +17,6 @@
package org.apache.sling.commons.json.io;
import java.io.IOException;
-import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Iterator;
@@ -188,70 +187,65 @@ public class JSONRenderer {
/** Quote the supplied string for JSON */
public String quote(String string) {
- final StringWriter sw = new StringWriter();
- try {
- quote(sw, string);
- } catch(IOException ioex) {
- throw new RuntimeException("IOException in quote()", ioex);
- }
- return sw.toString();
- }
-
- /** Quote the supplied string for JSON, to the supplied Writer */
- public void quote(Writer w, String string) throws IOException {
if (string == null || string.length() == 0) {
- w.write("\"\"");
- return;
+ return "\"\"";
}
- char b;
- char c = 0;
- int i;
- int len = string.length();
- String t;
+ char b;
+ char c = 0;
+ int i;
+ int len = string.length();
+ StringBuilder sb = new StringBuilder(len + 2);
+ String t;
- w.write('"');
+ sb.append('"');
for (i = 0; i < len; i += 1) {
b = c;
c = string.charAt(i);
switch (c) {
- case '\\':
- case '"':
- w.write('\\');
- w.write(c);
- break;
- case '/':
- if (b == '<') {
- w.write('\\');
- }
- w.write(c);
- break;
- case '\b':
- w.write("\\b");
- break;
- case '\t':
- w.write("\\t");
- break;
- case '\n':
- w.write("\\n");
- break;
- case '\f':
- w.write("\\f");
- break;
- case '\r':
- w.write("\\r");
- break;
- default:
- if (c < ' ' || (c >= '\u0080' && c < '\u00a0') ||
- (c >= '\u2000' && c < '\u2100')) {
- t = "000" + Integer.toHexString(c);
- w.write("\\u" + t.substring(t.length() - 4));
- } else {
- w.write(c);
- }
+ case '\\':
+ case '"':
+ sb.append('\\');
+ sb.append(c);
+ break;
+ case '/':
+ if (b == '<') {
+ sb.append('\\');
+ }
+ sb.append(c);
+ break;
+ case '\b':
+ sb.append("\\b");
+ break;
+ case '\t':
+ sb.append("\\t");
+ break;
+ case '\n':
+ sb.append("\\n");
+ break;
+ case '\f':
+ sb.append("\\f");
+ break;
+ case '\r':
+ sb.append("\\r");
+ break;
+ default:
+ if (c < ' ' || (c >= '\u0080' && c < '\u00a0') ||
+ (c >= '\u2000' && c < '\u2100')) {
+ t = "000" + Integer.toHexString(c);
+ sb.append("\\u").append(t.substring(t.length() - 4));
+ } else {
+ sb.append(c);
+ }
}
}
- w.write('"');
+ sb.append('"');
+ return sb.toString();
+ }
+
+ /** Quote the supplied string for JSON, to the supplied Writer */
+ public void quote(Writer w, String string) throws IOException {
+ w.write(quote(string));
}
/**
Modified: sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/io/JSONRendererTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/io/JSONRendererTest.java?rev=1709977&r1=1709976&r2=1709977&view=diff
==============================================================================
--- sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/io/JSONRendererTest.java (original)
+++ sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/io/JSONRendererTest.java Thu Oct 22 10:09:29 2015
@@ -25,6 +25,9 @@ import org.apache.sling.commons.json.uti
import org.apache.sling.commons.json.util.TestJSONObject;
import org.junit.Test;
+import java.io.IOException;
+import java.io.StringWriter;
+
/** Most of the JSONRenderer code is tested indirectly
* via existing JSONObject and JSONArray tests - this
@@ -120,5 +123,19 @@ public class JSONRendererTest {
"-nl-___children___:[-nl-{-nl-___name___:_k0_,-nl-_name_:_k0_",
"-nl-_thisis_:_k0_-nl-},-nl-{-nl-___name___:_k1_,-nl-_name_:_k1_,-nl-_thisis_:_k1_-nl-}-nl-]"
);
- }
+ }
+
+ /**
+ * Just to make sure that quote(writer, string) which is not indirectly tested anymore still works. For other quote
+ * tests see JSONObjectToStringTest.testQuote*
+ */
+ @Test
+ public void testQuoteWriter() throws IOException {
+ StringWriter writer = new StringWriter();
+ renderer.quote(writer, "\\");
+ renderer.quote(writer, "");
+ renderer.quote(writer, null);
+ assertEquals("\"\\\\\"\"\"\"\"", writer.toString());
+ }
+
}
\ No newline at end of file