Skip to content
Browse files

Added JsonWriter object to strip whitespace as it's written to the st…

…ream to reduce size of JSON output, achieves about 40% (fixes #91)
  • Loading branch information...
1 parent 947cca2 commit 651f57e63ba9fcae401bc730b1a47d3da2a35a7a @rodnaph rodnaph committed Mar 31, 2012
View
6 src/com/pugh/sockso/web/HttpResponse.java
@@ -29,7 +29,6 @@
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
-import java.io.PrintStream;
import java.sql.Timestamp;
import java.sql.SQLException;
@@ -370,7 +369,10 @@ public void showRss( final Renderer renderer ) throws IOException {
public void showJson( final Renderer renderer ) throws IOException {
- showTemplate( renderer, "application/json; charset=\"UTF-8\"" );
+ showTemplate(
+ new JsonRenderer( renderer ),
+ "application/json; charset=\"UTF-8\""
+ );
}
View
25 src/com/pugh/sockso/web/JsonRenderer.java
@@ -0,0 +1,25 @@
+
+package com.pugh.sockso.web;
+
+import java.io.Writer;
+import java.io.IOException;
+
+import org.jamon.Renderer;
+
+public class JsonRenderer implements Renderer {
+
+ private final Renderer renderer;
+
+ public JsonRenderer( final Renderer renderer ) {
+ this.renderer = renderer;
+ }
+
+ public String asString() {
+ return null;
+ }
+
+ public void renderTo( final Writer writer ) throws IOException {
+ this.renderer.renderTo( new JsonWriter(writer) );
+ }
+
+}
View
66 src/com/pugh/sockso/web/JsonWriter.java
@@ -0,0 +1,66 @@
+
+package com.pugh.sockso.web;
+
+import java.io.Writer;
+import java.io.IOException;
+
+public class JsonWriter extends Writer {
+
+ /**
+ * Wrapped writer object
+ */
+ private final Writer writer;
+
+ /**
+ * Indicates if we're currently in a string literal
+ */
+ private boolean inString;
+
+ /**
+ * Create a JSON writer that wraps the specified standard writer
+ *
+ * @param writer
+ *
+ */
+
+ public JsonWriter( final Writer writer ) {
+ this.writer = writer;
+ this.inString = false;
+ }
+
+ /**
+ * Remove whitespace from JSON as we write it
+ *
+ * @param cs
+ * @param x
+ * @param y
+ *
+ * @throws IOException
+ *
+ */
+
+ public void write( final char[] cs, int x, int y ) throws IOException {
+
+ char[] newChars = new char[ y ];
+ int size = 0;
+
+ for ( int i=x; i<y; i++ ) {
+ char c = cs[ i ];
+ if ( c == '"' && (i == x || cs[i-1] != '\\') ) { inString = !inString; }
+ if ( inString || !Character.isWhitespace(c) ) {
+ newChars[ size++ ] = c;
+ }
+ }
+
+ writer.write( newChars, 0, size );
+
+ }
+
+ /**
+ * Proxy on to wrapped writer object
+ */
+
+ public void close() throws IOException { this.writer.close(); }
+ public void flush() throws IOException { this.writer.flush(); }
+
+}
View
11 test/com/pugh/sockso/web/JsonRendererTest.java
@@ -0,0 +1,11 @@
+
+package com.pugh.sockso.web;
+
+import junit.framework.TestCase;
+
+public class JsonRendererTest extends TestCase {
+
+ public void testWhiteSpaceIsRemovedWhenRenderingTemplate() {
+ }
+
+}
View
18 test/com/pugh/sockso/web/JsonWriterTest.java
@@ -0,0 +1,18 @@
+
+package com.pugh.sockso.web;
+
+import com.pugh.sockso.tests.SocksoTestCase;
+
+import java.io.StringWriter;
+
+public class JsonWriterTest extends SocksoTestCase {
+
+ public void testWhiteSpaceIsRemovedFromJson() throws Exception {
+ String json = "{ id: 123, name: \"foo \\\"bar\" }";
+ StringWriter writer = new StringWriter();
+ JsonWriter jsonWriter = new JsonWriter( writer );
+ jsonWriter.write( json );
+ assertEquals( "{id:123,name:\"foo \\\"bar\"}", writer.toString() );
+ }
+
+}

0 comments on commit 651f57e

Please sign in to comment.
Something went wrong with that request. Please try again.