Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

introducing more convenience methods for de/serializing JSON

  • Loading branch information...
commit 96b7719b94a25b92594b7b23e4546d5fe895de44 1 parent ac4cc3b
@ropalka ropalka authored
View
31 modules/api/src/main/java/org/fossnova/json/JsonArray.java
@@ -20,6 +20,8 @@
package org.fossnova.json;
import java.io.IOException;
+import java.io.OutputStream;
+import java.io.Writer;
import java.util.List;
import org.fossnova.json.stream.JsonException;
@@ -270,11 +272,36 @@
/**
* Serializes this JSON array to the writer.
- * @param writer to write to
+ * @param output to write to
* @throws IOException if some I/O error occurs
* @throws JsonException if wrong JSON is detected
*/
- void writeTo( JsonWriter writer ) throws IOException, JsonException;
+ void writeTo( JsonWriter output ) throws IOException, JsonException;
+
+ /**
+ * Serializes this JSON array to the writer.
+ * @param output to write to
+ * @throws IOException if some I/O error occurs
+ * @throws JsonException if wrong JSON is detected
+ */
+ void writeTo( Writer output ) throws IOException, JsonException;
+
+ /**
+ * Serializes this JSON array to the stream using default character set.
+ * @param output to write to
+ * @throws IOException if some I/O error occurs
+ * @throws JsonException if wrong JSON is detected
+ */
+ void writeTo( OutputStream output ) throws IOException, JsonException;
+
+ /**
+ * Serializes this JSON array to the writer using specified character set.
+ * @param output to write to
+ * @param charsetName character set name
+ * @throws IOException if some I/O error occurs
+ * @throws JsonException if wrong JSON is detected
+ */
+ void writeTo( OutputStream output, String charsetName ) throws IOException, JsonException;
/**
* Clones this JSON array.
View
31 modules/api/src/main/java/org/fossnova/json/JsonObject.java
@@ -20,6 +20,8 @@
package org.fossnova.json;
import java.io.IOException;
+import java.io.OutputStream;
+import java.io.Writer;
import java.util.Map;
import org.fossnova.json.stream.JsonException;
@@ -119,11 +121,36 @@
/**
* Serializes this JSON object to the writer.
- * @param writer to write to
+ * @param output to write to
* @throws IOException if some I/O error occurs
* @throws JsonException if wrong JSON is detected
*/
- void writeTo( JsonWriter writer ) throws IOException, JsonException;
+ void writeTo( JsonWriter output ) throws IOException, JsonException;
+
+ /**
+ * Serializes this JSON object to the writer.
+ * @param output to write to
+ * @throws IOException if some I/O error occurs
+ * @throws JsonException if wrong JSON is detected
+ */
+ void writeTo( Writer output ) throws IOException, JsonException;
+
+ /**
+ * Serializes this JSON object to the stream using default character set.
+ * @param output to write to
+ * @throws IOException if some I/O error occurs
+ * @throws JsonException if wrong JSON is detected
+ */
+ void writeTo( OutputStream output ) throws IOException, JsonException;
+
+ /**
+ * Serializes this JSON object to the writer using specified character set.
+ * @param output to write to
+ * @param charsetName character set name
+ * @throws IOException if some I/O error occurs
+ * @throws JsonException if wrong JSON is detected
+ */
+ void writeTo( OutputStream output, String charsetName ) throws IOException, JsonException;
/**
* Clones this JSON object.
View
43 modules/api/src/main/java/org/fossnova/json/JsonValueFactory.java
@@ -20,6 +20,8 @@
package org.fossnova.json;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.Reader;
import org.fossnova.finder.FactoryFinder;
import org.fossnova.json.stream.JsonException;
@@ -91,10 +93,47 @@ public static JsonValueFactory newInstance() {
/**
* Creates either JSON array or object instance.
- * @param reader JSON reader
+ * @param input to read JSON from
* @return JSON array or object instance
* @throws IOException if some I/O error occurs
* @throws JsonException if wrong JSON is detected
*/
- public abstract JsonValue readFrom( JsonReader reader ) throws IOException, JsonException;
+ public abstract JsonValue readFrom( JsonReader input ) throws IOException, JsonException;
+
+ /**
+ * Creates either JSON array or object instance.
+ * @param input to read JSON from
+ * @return JSON array or object instance
+ * @throws IOException if some I/O error occurs
+ * @throws JsonException if wrong JSON is detected
+ */
+ public abstract JsonValue readFrom( Reader input ) throws IOException, JsonException;
+
+ /**
+ * Creates either JSON array or object instance.
+ * @param input to read JSON from
+ * @return JSON array or object instance
+ * @throws IOException if some I/O error occurs
+ * @throws JsonException if wrong JSON is detected
+ */
+ public abstract JsonValue readFrom( String input ) throws IOException, JsonException;
+
+ /**
+ * Creates either JSON array or object instance.
+ * @param input to read JSON from
+ * @return JSON array or object instance
+ * @throws IOException if some I/O error occurs
+ * @throws JsonException if wrong JSON is detected
+ */
+ public abstract JsonValue readFrom( InputStream input ) throws IOException, JsonException;
+
+ /**
+ * Creates either JSON array or object instance.
+ * @param data JSON available via input stream
+ * @param charsetName character set name
+ * @return JSON array or object instance
+ * @throws IOException if some I/O error occurs
+ * @throws JsonException if wrong JSON is detected
+ */
+ public abstract JsonValue readFrom( InputStream data, String charsetName ) throws IOException, JsonException;
}
View
24 modules/impl/src/main/java/com/fossnova/json/JsonStructureImpl.java
@@ -20,6 +20,8 @@
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 java.util.Collection;
@@ -28,6 +30,7 @@
import org.fossnova.json.JsonValue;
import org.fossnova.json.stream.JsonException;
+import org.fossnova.json.stream.JsonStreamFactory;
import org.fossnova.json.stream.JsonWriter;
import com.fossnova.json.stream.JsonWriterImpl;
@@ -39,13 +42,28 @@
private static final long serialVersionUID = 1L;
- public final void writeTo( final JsonWriter jsonWriter ) throws IOException, JsonException {
- if ( jsonWriter == null ) {
+ public final void writeTo( final JsonWriter output ) throws IOException, JsonException {
+ if ( output == null ) {
throw new NullPointerException( "JSON writer cannot be null" );
}
- writeTo( ( JsonWriterImpl ) jsonWriter );
+ writeTo( ( JsonWriterImpl ) output );
}
+ public final void writeTo( final Writer output ) throws IOException, JsonException {
+ final JsonWriter writer = JsonStreamFactory.newInstance().newJsonWriter( output );
+ writeTo( writer );
+ }
+
+ public final void writeTo( final OutputStream output ) throws IOException, JsonException {
+ final JsonWriter writer = JsonStreamFactory.newInstance().newJsonWriter( output );
+ writeTo( writer );
+ }
+
+ public final void writeTo( final OutputStream output, final String charsetName ) throws IOException, JsonException {
+ final JsonWriter writer = JsonStreamFactory.newInstance().newJsonWriter( output, charsetName );
+ writeTo( writer );
+ }
+
protected abstract void writeTo( final JsonWriterImpl jsonWriter ) throws IOException, JsonException;
protected final JsonStringImpl toJsonString( final String value ) {
View
35 modules/impl/src/main/java/com/fossnova/json/JsonValueFactoryImpl.java
@@ -29,6 +29,9 @@
import static org.fossnova.json.stream.JsonEvent.STRING;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.Reader;
+import java.io.StringReader;
import java.math.BigDecimal;
import java.math.BigInteger;
@@ -37,6 +40,7 @@
import org.fossnova.json.stream.JsonEvent;
import org.fossnova.json.stream.JsonException;
import org.fossnova.json.stream.JsonReader;
+import org.fossnova.json.stream.JsonStreamFactory;
import com.fossnova.json.stream.JsonReaderImpl;
@@ -94,9 +98,34 @@ public JsonStringImpl newJsonString( final String value ) {
}
@Override
- public JsonValue readFrom( final JsonReader jsonReader ) throws IOException, JsonException {
- assertNotNullParameter( jsonReader );
- return readFrom( ( JsonReaderImpl ) jsonReader );
+ public JsonValue readFrom( final JsonReader input ) throws IOException, JsonException {
+ assertNotNullParameter( input );
+ return readFrom( ( JsonReaderImpl ) input );
+ }
+
+ @Override
+ public JsonValue readFrom( final Reader input ) throws IOException, JsonException {
+ final JsonReader reader = JsonStreamFactory.newInstance().newJsonReader( input );
+ return readFrom( reader );
+ }
+
+ @Override
+ public JsonValue readFrom( final String input ) throws IOException, JsonException {
+ assertNotNullParameter( input );
+ final Reader reader = new StringReader( input );
+ return readFrom( reader );
+ }
+
+ @Override
+ public JsonValue readFrom( final InputStream input ) throws IOException, JsonException {
+ final JsonReader reader = JsonStreamFactory.newInstance().newJsonReader( input );
+ return readFrom( reader );
+ }
+
+ @Override
+ public JsonValue readFrom( final InputStream input, final String charsetName ) throws IOException, JsonException {
+ final JsonReader reader = JsonStreamFactory.newInstance().newJsonReader( input, charsetName );
+ return readFrom( reader );
}
private JsonValue readFrom( final JsonReaderImpl jsonReader ) throws IOException, JsonException {
Please sign in to comment.
Something went wrong with that request. Please try again.