forked from rodnaph/sockso
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
5 changed files
with
124 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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) ); | |||
} | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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(); } | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,11 @@ | |||
|
|||
package com.pugh.sockso.web; | |||
|
|||
import junit.framework.TestCase; | |||
|
|||
public class JsonRendererTest extends TestCase { | |||
|
|||
public void testWhiteSpaceIsRemovedWhenRenderingTemplate() { | |||
} | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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() ); | |||
} | |||
|
|||
} |