Permalink
Browse files

read/write JSON structures directly from JSON reader/writer

  • Loading branch information...
1 parent 3f1f83a commit 6ea938f07558383f301b6fabab7ffd2ae032640c @ropalka ropalka committed Sep 13, 2012
@@ -20,11 +20,9 @@
package org.fossnova.json;
import java.io.IOException;
-import java.io.InputStream;
-import java.io.Reader;
-import java.io.UnsupportedEncodingException;
import org.fossnova.finder.FactoryFinder;
+import org.fossnova.json.stream.JsonReader;
/**
* @author <a href="mailto:opalka dot richard at gmail dot com">Richard Opalka</a>
@@ -46,10 +44,6 @@ public static JsonFactory newInstance() {
public abstract JsonArray newJsonArray();
- public abstract JsonStructure readFrom( Reader reader ) throws IOException;
-
- public abstract JsonStructure readFrom( InputStream stream ) throws IOException;
-
- public abstract JsonStructure readFrom( InputStream stream, String charsetName ) throws UnsupportedEncodingException, IOException;
+ public abstract JsonStructure readFrom( JsonReader reader ) throws IOException;
}
@@ -20,14 +20,12 @@
package org.fossnova.json;
import java.io.IOException;
-import java.io.OutputStream;
-import java.io.Writer;
+
+import org.fossnova.json.stream.JsonWriter;
/**
* @author <a href="mailto:opalka dot richard at gmail dot com">Richard Opalka</a>
*/
public interface JsonStructure extends JsonValue {
- void writeTo( OutputStream stream ) throws IOException;
- void writeTo( OutputStream stream, String charsetName ) throws IOException;
- void writeTo( Writer writer ) throws IOException;
+ void writeTo( JsonWriter writer ) throws IOException;
}
@@ -29,17 +29,14 @@
import static org.fossnova.json.stream.JsonEvent.STRING;
import java.io.IOException;
-import java.io.InputStream;
-import java.io.Reader;
-import java.io.UnsupportedEncodingException;
-import org.fossnova.json.JsonStructure;
import org.fossnova.json.JsonFactory;
+import org.fossnova.json.JsonStructure;
import org.fossnova.json.JsonValue;
import org.fossnova.json.stream.JsonEvent;
+import org.fossnova.json.stream.JsonReader;
import com.fossnova.json.stream.JsonReaderImpl;
-import com.fossnova.json.stream.JsonStreamFactoryImpl;
/**
* @author <a href="mailto:opalka dot richard at gmail dot com">Richard Opalka</a>
@@ -57,21 +54,11 @@ public JsonArrayImpl newJsonArray() {
}
@Override
- public JsonStructure readFrom( final Reader reader ) throws IOException {
- final JsonReaderImpl jsonReader = new JsonStreamFactoryImpl().newJsonReader( reader );
- return readFrom( jsonReader );
- }
-
- @Override
- public JsonStructure readFrom( final InputStream stream ) throws UnsupportedEncodingException, IOException {
- final JsonReaderImpl jsonReader = new JsonStreamFactoryImpl().newJsonReader( stream );
- return readFrom( jsonReader );
- }
-
- @Override
- public JsonStructure readFrom( final InputStream stream, final String charsetName ) throws UnsupportedEncodingException, IOException {
- final JsonReaderImpl jsonReader = new JsonStreamFactoryImpl().newJsonReader( stream, charsetName );
- return readFrom( jsonReader );
+ public JsonStructure readFrom( final JsonReader jsonReader ) throws IOException {
+ if ( jsonReader == null ) {
+ throw new IllegalArgumentException( "JSON reader cannot be null" );
+ }
+ return readFrom( (JsonReaderImpl) jsonReader );
}
private JsonStructure readFrom( final JsonReaderImpl jsonReader ) throws IOException {
@@ -20,40 +20,24 @@
package com.fossnova.json;
import java.io.IOException;
-import java.io.OutputStream;
-import java.io.Writer;
import java.math.BigDecimal;
import java.math.BigInteger;
import org.fossnova.json.JsonStructure;
+import org.fossnova.json.stream.JsonWriter;
-import com.fossnova.json.stream.JsonStreamFactoryImpl;
import com.fossnova.json.stream.JsonWriterImpl;
/**
* @author <a href="mailto:opalka dot richard at gmail dot com">Richard Opalka</a>
*/
abstract class JsonStructureImpl implements JsonStructure {
- public final void writeTo( final OutputStream stream ) throws IOException {
- final JsonWriterImpl jsonWriter = new JsonStreamFactoryImpl().newJsonWriter( stream );
- writeTo( jsonWriter );
- jsonWriter.flush();
- jsonWriter.close();
- }
-
- public final void writeTo( final OutputStream stream, final String charsetName ) throws IOException {
- final JsonWriterImpl jsonWriter = new JsonStreamFactoryImpl().newJsonWriter( stream, charsetName );
- writeTo( jsonWriter );
- jsonWriter.flush();
- jsonWriter.close();
- }
-
- public final void writeTo( final Writer writer ) throws IOException {
- final JsonWriterImpl jsonWriter = new JsonStreamFactoryImpl().newJsonWriter( writer );
- writeTo( jsonWriter );
- jsonWriter.flush();
- jsonWriter.close();
+ public final void writeTo( final JsonWriter jsonWriter ) throws IOException {
+ if ( jsonWriter == null ) {
+ throw new IllegalArgumentException( "JSON writer cannot be null" );
+ }
+ writeTo( (JsonWriterImpl) jsonWriter );
}
protected abstract void writeTo( final JsonWriterImpl jsonWriter ) throws IOException;
@@ -28,9 +28,7 @@
import java.io.Writer;
import java.nio.charset.Charset;
-import org.fossnova.json.stream.JsonReader;
import org.fossnova.json.stream.JsonStreamFactory;
-import org.fossnova.json.stream.JsonWriter;
/**
* @author <a href="mailto:opalka dot richard at gmail dot com">Richard Opalka</a>
@@ -27,11 +27,14 @@
import org.fossnova.json.JsonArray;
import org.fossnova.json.JsonBoolean;
+import org.fossnova.json.JsonFactory;
import org.fossnova.json.JsonNumber;
import org.fossnova.json.JsonObject;
import org.fossnova.json.JsonString;
-import org.fossnova.json.JsonFactory;
import org.fossnova.json.JsonValue;
+import org.fossnova.json.stream.JsonReader;
+import org.fossnova.json.stream.JsonStreamFactory;
+import org.fossnova.json.stream.JsonWriter;
import org.junit.Test;
/**
@@ -44,78 +47,79 @@ public void createJsonObjectFromScratch() throws IOException {
JsonFactory jsonStructureFactory = JsonFactory.newInstance();
JsonObject o = jsonStructureFactory.newJsonObject();
o.put( "1", "b1" );
- o.put( "2", (String)null );
- o.put("3", true );
+ o.put( "2", ( String ) null );
+ o.put( "3", true );
o.put( "4", false );
o.put( "5", 1 );
JsonArray a = jsonStructureFactory.newJsonArray();
- o.put("6", a);
+ o.put( "6", a );
JsonObject o1 = jsonStructureFactory.newJsonObject();
- o.put("7", o1);
- assertJsonString( o, "1", "b1");
- assertJsonNull( o, "2");
+ o.put( "7", o1 );
+ assertJsonString( o, "1", "b1" );
+ assertJsonNull( o, "2" );
assertJsonBoolean( o, "3", true );
assertJsonBoolean( o, "4", false );
assertJsonNumber( o, "5", 1 );
Assert.assertTrue( o.containsKey( "4" ) );
Assert.assertTrue( o.containsValue( 1 ) );
Assert.assertTrue( o.containsValue( true ) );
Assert.assertTrue( o.containsValue( false ) );
- Assert.assertTrue( o.containsValue( (Boolean) null ) );
+ Assert.assertTrue( o.containsValue( ( Boolean ) null ) );
Assert.assertTrue( o.containsValue( a ) );
Assert.assertTrue( o.containsValue( o1 ) );
ByteArrayOutputStream baos = new ByteArrayOutputStream();
- o.writeTo( baos );
- System.out.println("--- START ---");
- System.out.println( new String(baos.toByteArray()));
- System.out.println("--- END ---");
+ JsonWriter jsonWriter = JsonStreamFactory.newInstance().newJsonWriter( baos );
+ o.writeTo( jsonWriter );
+ System.out.println( "--- START ---" );
+ System.out.println( new String( baos.toByteArray() ) );
+ System.out.println( "--- END ---" );
}
-
+
@Test
public void createJsonObjectFromStream() throws IOException {
final JsonFactory jsonStructureFactory = JsonFactory.newInstance();
- final ByteArrayInputStream bais = new ByteArrayInputStream("{\"1\":\"b1\",\"2\":null,\"3\":true,\"4\":false,\"5\":1,\"6\":[],\"7\":{}}".getBytes());
- final JsonObject o = (JsonObject) jsonStructureFactory.readFrom( bais );
+ final ByteArrayInputStream bais = new ByteArrayInputStream( "{\"1\":\"b1\",\"2\":null,\"3\":true,\"4\":false,\"5\":1,\"6\":[],\"7\":{}}".getBytes() );
+ final JsonReader jsonReader = JsonStreamFactory.newInstance().newJsonReader( bais );
+ final JsonObject o = ( JsonObject ) jsonStructureFactory.readFrom( jsonReader );
JsonArray a = jsonStructureFactory.newJsonArray();
JsonObject o1 = jsonStructureFactory.newJsonObject();
- assertJsonString( o, "1", "b1");
- assertJsonNull( o, "2");
+ assertJsonString( o, "1", "b1" );
+ assertJsonNull( o, "2" );
assertJsonBoolean( o, "3", true );
assertJsonBoolean( o, "4", false );
assertJsonNumber( o, "5", 1 );
Assert.assertTrue( o.containsKey( "4" ) );
Assert.assertTrue( o.containsValue( 1 ) );
Assert.assertTrue( o.containsValue( true ) );
Assert.assertTrue( o.containsValue( false ) );
- Assert.assertTrue( o.containsValue( (Boolean) null ) );
+ Assert.assertTrue( o.containsValue( ( Boolean ) null ) );
Assert.assertTrue( o.containsValue( a ) );
Assert.assertTrue( o.containsValue( o1 ) );
}
-
+
private static void assertJsonString( final JsonObject o, final String key, final String expectedValue ) {
final JsonValue jsonValue = o.get( key );
Assert.assertTrue( jsonValue instanceof JsonString );
- final JsonString jsonString = (JsonString)jsonValue;
+ final JsonString jsonString = ( JsonString ) jsonValue;
Assert.assertEquals( expectedValue, jsonString.getString() );
}
-
+
private static void assertJsonBoolean( final JsonObject o, final String key, final boolean expectedValue ) {
final JsonValue jsonValue = o.get( key );
Assert.assertTrue( jsonValue instanceof JsonBoolean );
- final JsonBoolean jsonBoolean = (JsonBoolean)jsonValue;
+ final JsonBoolean jsonBoolean = ( JsonBoolean ) jsonValue;
Assert.assertEquals( expectedValue, jsonBoolean.getBoolean() );
}
-
+
private static void assertJsonNumber( final JsonObject o, final String key, final int expectedValue ) {
final JsonValue jsonValue = o.get( key );
Assert.assertTrue( jsonValue instanceof JsonNumber );
- final JsonNumber jsonNumber = (JsonNumber)jsonValue;
+ final JsonNumber jsonNumber = ( JsonNumber ) jsonValue;
Assert.assertEquals( expectedValue, jsonNumber.getInt() );
}
-
+
private static void assertJsonNull( final JsonObject o, final String key ) {
Assert.assertTrue( o.keySet().contains( key ) );
Assert.assertNull( o.get( key ) );
}
-
}

0 comments on commit 6ea938f

Please sign in to comment.