Skip to content

Commit

Permalink
use Charset instead of String as parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
ropalka committed Oct 31, 2012
1 parent 33d29a0 commit e0bf22f
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 35 deletions.
5 changes: 3 additions & 2 deletions modules/api/src/main/java/org/fossnova/json/JsonArray.java
Expand Up @@ -22,6 +22,7 @@
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.Writer; import java.io.Writer;
import java.nio.charset.Charset;
import java.util.List; import java.util.List;


import org.fossnova.json.stream.JsonException; import org.fossnova.json.stream.JsonException;
Expand Down Expand Up @@ -297,11 +298,11 @@ public interface JsonArray extends JsonValue, List< JsonValue > {
/** /**
* Serializes this JSON array to the writer using specified character set. * Serializes this JSON array to the writer using specified character set.
* @param output to write to * @param output to write to
* @param charsetName character set name * @param charset character set
* @throws IOException if some I/O error occurs * @throws IOException if some I/O error occurs
* @throws JsonException if wrong JSON is detected * @throws JsonException if wrong JSON is detected
*/ */
void writeTo( OutputStream output, String charsetName ) throws IOException, JsonException; void writeTo( OutputStream output, Charset charset ) throws IOException, JsonException;


/** /**
* Clones this JSON array. * Clones this JSON array.
Expand Down
5 changes: 3 additions & 2 deletions modules/api/src/main/java/org/fossnova/json/JsonObject.java
Expand Up @@ -22,6 +22,7 @@
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.Writer; import java.io.Writer;
import java.nio.charset.Charset;
import java.util.Map; import java.util.Map;


import org.fossnova.json.stream.JsonException; import org.fossnova.json.stream.JsonException;
Expand Down Expand Up @@ -146,11 +147,11 @@ public interface JsonObject extends JsonValue, Map< String, JsonValue > {
/** /**
* Serializes this JSON object to the writer using specified character set. * Serializes this JSON object to the writer using specified character set.
* @param output to write to * @param output to write to
* @param charsetName character set name * @param charset character set
* @throws IOException if some I/O error occurs * @throws IOException if some I/O error occurs
* @throws JsonException if wrong JSON is detected * @throws JsonException if wrong JSON is detected
*/ */
void writeTo( OutputStream output, String charsetName ) throws IOException, JsonException; void writeTo( OutputStream output, Charset charset ) throws IOException, JsonException;


/** /**
* Clones this JSON object. * Clones this JSON object.
Expand Down
Expand Up @@ -22,6 +22,7 @@
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.Reader; import java.io.Reader;
import java.nio.charset.Charset;


import org.fossnova.finder.FactoryFinder; import org.fossnova.finder.FactoryFinder;
import org.fossnova.json.stream.JsonException; import org.fossnova.json.stream.JsonException;
Expand Down Expand Up @@ -130,10 +131,10 @@ public static JsonValueFactory newInstance() {
/** /**
* Creates either JSON array or object instance. * Creates either JSON array or object instance.
* @param data JSON available via input stream * @param data JSON available via input stream
* @param charsetName character set name * @param charset character set
* @return JSON array or object instance * @return JSON array or object instance
* @throws IOException if some I/O error occurs * @throws IOException if some I/O error occurs
* @throws JsonException if wrong JSON is detected * @throws JsonException if wrong JSON is detected
*/ */
public abstract JsonValue readFrom( InputStream data, String charsetName ) throws IOException, JsonException; public abstract JsonValue readFrom( InputStream data, Charset charset ) throws IOException, JsonException;
} }
Expand Up @@ -22,8 +22,8 @@
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.Reader; import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.io.Writer; import java.io.Writer;
import java.nio.charset.Charset;


import org.fossnova.finder.FactoryFinder; import org.fossnova.finder.FactoryFinder;


Expand Down Expand Up @@ -72,33 +72,29 @@ public static JsonStreamFactory newInstance() {
* Creates new JSON reader with default character set. * Creates new JSON reader with default character set.
* @param stream input * @param stream input
* @return JSON reader instance * @return JSON reader instance
* @throws UnsupportedEncodingException if default character set is not supported
*/ */
public abstract JsonReader newJsonReader( InputStream stream ) throws UnsupportedEncodingException; public abstract JsonReader newJsonReader( InputStream stream );


/** /**
* Creates new JSON writer with default character set. * Creates new JSON writer with default character set.
* @param stream output * @param stream output
* @return JSON writer instance * @return JSON writer instance
* @throws UnsupportedEncodingException if default character set is not supported
*/ */
public abstract JsonWriter newJsonWriter( OutputStream stream ) throws UnsupportedEncodingException; public abstract JsonWriter newJsonWriter( OutputStream stream );


/** /**
* Creates new JSON reader with specified character set. * Creates new JSON reader with specified character set.
* @param stream input * @param stream input
* @param charsetName character set name * @param charset character set
* @return JSON reader instance * @return JSON reader instance
* @throws UnsupportedEncodingException if specified character set is not supported
*/ */
public abstract JsonReader newJsonReader( InputStream stream, String charsetName ) throws UnsupportedEncodingException; public abstract JsonReader newJsonReader( InputStream stream, Charset charset );


/** /**
* Creates new JSON writer with specified character set. * Creates new JSON writer with specified character set.
* @param stream output * @param stream output
* @param charsetName character set name * @param charset character set
* @return JSON writer instance * @return JSON writer instance
* @throws UnsupportedEncodingException if specified character set is not supported
*/ */
public abstract JsonWriter newJsonWriter( OutputStream stream, String charsetName ) throws UnsupportedEncodingException; public abstract JsonWriter newJsonWriter( OutputStream stream, Charset charset );
} }
Expand Up @@ -24,6 +24,7 @@
import java.io.Writer; import java.io.Writer;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;
import java.nio.charset.Charset;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
Expand Down Expand Up @@ -59,8 +60,8 @@ public final void writeTo( final OutputStream output ) throws IOException, JsonE
writeTo( writer ); writeTo( writer );
} }


public final void writeTo( final OutputStream output, final String charsetName ) throws IOException, JsonException { public final void writeTo( final OutputStream output, final Charset charset ) throws IOException, JsonException {
final JsonWriter writer = JsonStreamFactory.newInstance().newJsonWriter( output, charsetName ); final JsonWriter writer = JsonStreamFactory.newInstance().newJsonWriter( output, charset );
writeTo( writer ); writeTo( writer );
} }


Expand Down
Expand Up @@ -34,6 +34,7 @@
import java.io.StringReader; import java.io.StringReader;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;
import java.nio.charset.Charset;


import org.fossnova.json.JsonValue; import org.fossnova.json.JsonValue;
import org.fossnova.json.JsonValueFactory; import org.fossnova.json.JsonValueFactory;
Expand Down Expand Up @@ -123,8 +124,8 @@ public JsonValue readFrom( final InputStream input ) throws IOException, JsonExc
} }


@Override @Override
public JsonValue readFrom( final InputStream input, final String charsetName ) throws IOException, JsonException { public JsonValue readFrom( final InputStream input, final Charset charset ) throws IOException, JsonException {
final JsonReader reader = JsonStreamFactory.newInstance().newJsonReader( input, charsetName ); final JsonReader reader = JsonStreamFactory.newInstance().newJsonReader( input, charset );
return readFrom( reader ); return readFrom( reader );
} }


Expand Down
Expand Up @@ -24,7 +24,6 @@
import java.io.OutputStream; import java.io.OutputStream;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.Reader; import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.io.Writer; import java.io.Writer;
import java.nio.charset.Charset; import java.nio.charset.Charset;


Expand All @@ -35,7 +34,7 @@
*/ */
public final class JsonStreamFactoryImpl extends JsonStreamFactory { public final class JsonStreamFactoryImpl extends JsonStreamFactory {


private static final String DEFAULT_CHARSET = Charset.defaultCharset().name(); private static final Charset DEFAULT_CHARSET = Charset.defaultCharset();


public JsonStreamFactoryImpl() { public JsonStreamFactoryImpl() {
} }
Expand All @@ -47,15 +46,15 @@ public JsonReaderImpl newJsonReader( final Reader reader ) {
} }


@Override @Override
public JsonReaderImpl newJsonReader( final InputStream stream ) throws UnsupportedEncodingException { public JsonReaderImpl newJsonReader( final InputStream stream ) {
return newJsonReader( stream, DEFAULT_CHARSET ); return newJsonReader( stream, DEFAULT_CHARSET );
} }


@Override @Override
public JsonReaderImpl newJsonReader( final InputStream stream, final String charsetName ) throws UnsupportedEncodingException { public JsonReaderImpl newJsonReader( final InputStream stream, final Charset charset ) {
assertNotNullParameter( stream ); assertNotNullParameter( stream );
assertNotNullParameter( charsetName ); assertNotNullParameter( charset );
return newJsonReader( new InputStreamReader( stream, charsetName ) ); return newJsonReader( new InputStreamReader( stream, charset ) );
} }


@Override @Override
Expand All @@ -65,15 +64,15 @@ public JsonWriterImpl newJsonWriter( final Writer writer ) {
} }


@Override @Override
public JsonWriterImpl newJsonWriter( final OutputStream stream ) throws UnsupportedEncodingException { public JsonWriterImpl newJsonWriter( final OutputStream stream ) {
return newJsonWriter( stream, DEFAULT_CHARSET ); return newJsonWriter( stream, DEFAULT_CHARSET );
} }


@Override @Override
public JsonWriterImpl newJsonWriter( final OutputStream stream, final String charsetName ) throws UnsupportedEncodingException { public JsonWriterImpl newJsonWriter( final OutputStream stream, final Charset charset ) {
assertNotNullParameter( stream ); assertNotNullParameter( stream );
assertNotNullParameter( charsetName ); assertNotNullParameter( charset );
return newJsonWriter( new OutputStreamWriter( stream, charsetName ) ); return newJsonWriter( new OutputStreamWriter( stream, charset ) );
} }


private static void assertNotNullParameter( final Object o ) { private static void assertNotNullParameter( final Object o ) {
Expand Down
Expand Up @@ -22,7 +22,6 @@
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;


Expand All @@ -44,7 +43,7 @@ public final class ValidJsonWriterTestCase extends AbstractJsonStreamsTestCase {
private JsonWriter writer; private JsonWriter writer;


@Before @Before
public void init() throws UnsupportedEncodingException { public void init() {
baos = new ByteArrayOutputStream(); baos = new ByteArrayOutputStream();
writer = JsonStreamFactory.newInstance().newJsonWriter( new OutputStreamWriter( baos ) ); writer = JsonStreamFactory.newInstance().newJsonWriter( new OutputStreamWriter( baos ) );
} }
Expand Down Expand Up @@ -239,7 +238,7 @@ public void controlsEncoding() throws IOException, JsonException {
Assert.assertEquals( expected, getWriterOutput() ); Assert.assertEquals( expected, getWriterOutput() );
} }


private String getWriterOutput() throws UnsupportedEncodingException { private String getWriterOutput() {
return baos.toString( "UTF-8" ); return baos.toString();
} }
} }

0 comments on commit e0bf22f

Please sign in to comment.