Permalink
Browse files

refactoring JsonFactory -> JsonValueFactory

  • Loading branch information...
ropalka committed Sep 14, 2012
1 parent 522d9e1 commit d18211e5f833ac43ebf5b810493b0e1ae5a78020
@@ -27,23 +27,29 @@
/**
* @author <a href="mailto:opalka dot richard at gmail dot com">Richard Opalka</a>
*/
-public abstract class JsonFactory {
+public abstract class JsonValueFactory {
- protected JsonFactory() {
+ protected JsonValueFactory() {
}
- public static JsonFactory newInstance() {
- final JsonFactory jsonFactoryImpl = FactoryFinder.find( JsonFactory.class );
+ public static JsonValueFactory newInstance() {
+ final JsonValueFactory jsonFactoryImpl = FactoryFinder.find( JsonValueFactory.class );
if ( jsonFactoryImpl != null ) {
return jsonFactoryImpl;
}
- throw new IllegalStateException( "Factory not configured: " + JsonFactory.class.getName() );
+ throw new IllegalStateException( "Factory not configured: " + JsonValueFactory.class.getName() );
}
public abstract JsonObject newJsonObject();
public abstract JsonArray newJsonArray();
+ public abstract JsonBoolean newJsonBoolean( Boolean value );
+
+ public abstract JsonNumber newJsonNumber( Number value );
+
+ public abstract JsonString newJsonString( String value );
+
public abstract JsonValue readFrom( JsonReader reader ) throws IOException;
}
@@ -40,7 +40,6 @@ public boolean getBoolean() {
return value;
}
- // implemented java.lang.Object methods
@Override
public int hashCode() {
return value.hashCode();
@@ -99,7 +99,6 @@ public BigDecimal getBigDecimal() {
return new BigDecimal( value );
}
- // implemented java.lang.Object methods
@Override
public int hashCode() {
return value.hashCode();
@@ -36,7 +36,6 @@ public String getString() {
return value;
}
- // implemented java.lang.Object methods
@Override
public int hashCode() {
return value.hashCode();
@@ -29,9 +29,11 @@
import static org.fossnova.json.stream.JsonEvent.STRING;
import java.io.IOException;
+import java.math.BigDecimal;
+import java.math.BigInteger;
-import org.fossnova.json.JsonFactory;
import org.fossnova.json.JsonValue;
+import org.fossnova.json.JsonValueFactory;
import org.fossnova.json.stream.JsonEvent;
import org.fossnova.json.stream.JsonReader;
@@ -40,7 +42,7 @@
/**
* @author <a href="mailto:opalka dot richard at gmail dot com">Richard Opalka</a>
*/
-public final class JsonFactoryImpl extends JsonFactory {
+public final class JsonValueFactoryImpl extends JsonValueFactory {
@Override
public JsonObjectImpl newJsonObject() {
@@ -52,12 +54,53 @@ public JsonArrayImpl newJsonArray() {
return new JsonArrayImpl();
}
+ @Override
+ public JsonBooleanImpl newJsonBoolean( final Boolean value ) {
+ if ( value == null ) {
+ return null;
+ }
+ return value ? JsonBooleanImpl.TRUE : JsonBooleanImpl.FALSE;
+ }
+
+ @Override
+ public JsonNumberImpl newJsonNumber( final Number value ) {
+ if ( value == null ) {
+ return null;
+ }
+ if ( value instanceof Byte ) {
+ return new JsonNumberImpl( ( Byte ) value );
+ } else if ( value instanceof Short ) {
+ return new JsonNumberImpl( ( Short ) value );
+ } else if ( value instanceof Integer ) {
+ return new JsonNumberImpl( ( Integer ) value );
+ } else if ( value instanceof Long ) {
+ return new JsonNumberImpl( ( Long ) value );
+ } else if ( value instanceof Float ) {
+ return new JsonNumberImpl( ( Float ) value );
+ } else if ( value instanceof Double ) {
+ return new JsonNumberImpl( ( Double ) value );
+ } else if ( value instanceof BigInteger ) {
+ return new JsonNumberImpl( ( BigInteger ) value );
+ } else if ( value instanceof BigDecimal ) {
+ return new JsonNumberImpl( ( BigDecimal ) value );
+ }
+ throw new IllegalStateException();
+ }
+
+ @Override
+ public JsonStringImpl newJsonString( final String value ) {
+ if ( value == null ) {
+ return null;
+ }
+ return new JsonStringImpl( value );
+ }
+
@Override
public JsonValue readFrom( final JsonReader jsonReader ) throws IOException {
if ( jsonReader == null ) {
throw new IllegalArgumentException( "JSON reader cannot be null" );
}
- return readFrom( (JsonReaderImpl) jsonReader );
+ return readFrom( ( JsonReaderImpl ) jsonReader );
}
private JsonValue readFrom( final JsonReaderImpl jsonReader ) throws IOException {
@@ -1 +0,0 @@
-com.fossnova.json.JsonFactoryImpl
@@ -0,0 +1 @@
+com.fossnova.json.JsonValueFactoryImpl
@@ -29,7 +29,7 @@
import org.fossnova.json.JsonArray;
import org.fossnova.json.JsonBoolean;
-import org.fossnova.json.JsonFactory;
+import org.fossnova.json.JsonValueFactory;
import org.fossnova.json.JsonNumber;
import org.fossnova.json.JsonObject;
import org.fossnova.json.JsonString;
@@ -71,7 +71,7 @@ public void complexObject() throws IOException {
@Test
public void createJsonObjectFromStream() throws IOException {
- final JsonFactory jsonStructureFactory = JsonFactory.newInstance();
+ final JsonValueFactory jsonStructureFactory = JsonValueFactory.newInstance();
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 );
@@ -129,12 +129,12 @@ private static String serializeJson( final JsonArray jsonArray ) throws IOExcept
private static JsonValue deserializeJson( final String jsonString ) throws IOException {
final ByteArrayInputStream bais = new ByteArrayInputStream( jsonString.getBytes() );
final JsonReader jsonReader = JsonStreamFactory.newInstance().newJsonReader( bais );
- final JsonFactory jsonFactory = JsonFactory.newInstance();
+ final JsonValueFactory jsonFactory = JsonValueFactory.newInstance();
return jsonFactory.readFrom( jsonReader );
}
private static JsonArray createSimpleArray() {
- final JsonFactory jsonFactory = JsonFactory.newInstance();
+ final JsonValueFactory jsonFactory = JsonValueFactory.newInstance();
final JsonArray jsonArray = jsonFactory.newJsonArray();
jsonArray.add( (String) null );
jsonArray.add( true );
@@ -146,7 +146,7 @@ private static JsonArray createSimpleArray() {
}
private static JsonObject createSimpleObject() {
- final JsonFactory jsonFactory = JsonFactory.newInstance();
+ final JsonValueFactory jsonFactory = JsonValueFactory.newInstance();
final JsonObject jsonObject = jsonFactory.newJsonObject();
jsonObject.put( "1", (String) null );
jsonObject.put( "2", true );
@@ -158,7 +158,7 @@ private static JsonObject createSimpleObject() {
}
private static JsonObject createComplexObject() {
- final JsonFactory jsonFactory = JsonFactory.newInstance();
+ final JsonValueFactory jsonFactory = JsonValueFactory.newInstance();
final JsonObject jsonObject = jsonFactory.newJsonObject();
jsonObject.put( "1", "b1" );
jsonObject.put( "2", ( String ) null );

0 comments on commit d18211e

Please sign in to comment.