Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
rodnaph committed Mar 31, 2012
1 parent 947cca2 commit 651f57e
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/com/pugh/sockso/web/HttpResponse.java
Expand Up @@ -29,7 +29,6 @@
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.EOFException; import java.io.EOFException;
import java.io.PrintStream;


import java.sql.Timestamp; import java.sql.Timestamp;
import java.sql.SQLException; import java.sql.SQLException;
Expand Down Expand Up @@ -370,7 +369,10 @@ public void showRss( final Renderer renderer ) throws IOException {


public void showJson( 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\""
);


} }


Expand Down
25 changes: 25 additions & 0 deletions 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) );
}

}
66 changes: 66 additions & 0 deletions 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(); }

}
11 changes: 11 additions & 0 deletions 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() {
}

}
18 changes: 18 additions & 0 deletions 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.