Repository: zest-qi4j Updated Branches: refs/heads/master [created] a675e78c5
QI-318 org.qi4j.api.json clean up Project: http://git-wip-us.apache.org/repos/asf/zest-qi4j/repo Commit: http://git-wip-us.apache.org/repos/asf/zest-qi4j/commit/3a7fb782 Tree: http://git-wip-us.apache.org/repos/asf/zest-qi4j/tree/3a7fb782 Diff: http://git-wip-us.apache.org/repos/asf/zest-qi4j/diff/3a7fb782 Branch: refs/heads/master Commit: 3a7fb782233e85e2b96d497a7a79105429def771 Parents: 103f971 Author: Paul Merlin <[email protected]> Authored: Thu Feb 7 08:31:15 2013 +0100 Committer: Paul Merlin <[email protected]> Committed: Thu Feb 7 08:31:15 2013 +0100 ---------------------------------------------------------------------- .../org/qi4j/api/json/JSONDeserializer.java | 529 ------------------- .../org/qi4j/api/json/JSONObjectSerializer.java | 92 ---- .../java/org/qi4j/api/json/JSONSerializer.java | 336 ------------ .../org/qi4j/api/json/JSONWriterSerializer.java | 83 --- .../main/java/org/qi4j/api/json/package.html | 5 - .../runtime/types/JodaDateTimeTypeTest.java | 41 -- .../types/JodaLocalDateTimeTypeTest.java | 41 -- .../runtime/types/JodaLocalDateTypeTest.java | 40 -- .../org/qi4j/runtime/types/ValueLookupTest.java | 69 --- .../qi4j/runtime/value/CollectionTypeTest.java | 443 ---------------- 10 files changed, 1679 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/zest-qi4j/blob/3a7fb782/core/api/src/main/java/org/qi4j/api/json/JSONDeserializer.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/json/JSONDeserializer.java b/core/api/src/main/java/org/qi4j/api/json/JSONDeserializer.java deleted file mode 100644 index 20b1f50..0000000 --- a/core/api/src/main/java/org/qi4j/api/json/JSONDeserializer.java +++ /dev/null @@ -1,529 +0,0 @@ -package org.qi4j.api.json; - -import org.qi4j.api.util.Base64Encoder; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.math.BigDecimal; -import java.math.BigInteger; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.Date; -import java.util.HashMap; -import java.util.HashSet; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import org.joda.time.DateTime; -import org.joda.time.DateTimeZone; -import org.joda.time.LocalDate; -import org.joda.time.LocalDateTime; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; -import org.qi4j.api.association.AssociationDescriptor; -import org.qi4j.api.common.QualifiedName; -import org.qi4j.api.entity.EntityReference; -import org.qi4j.api.property.DefaultValues; -import org.qi4j.api.property.PropertyDescriptor; -import org.qi4j.api.structure.Module; -import org.qi4j.api.type.CollectionType; -import org.qi4j.api.type.EnumType; -import org.qi4j.api.type.MapType; -import org.qi4j.api.type.ValueCompositeType; -import org.qi4j.api.type.ValueType; -import org.qi4j.api.unitofwork.UnitOfWork; -import org.qi4j.api.util.Dates; -import org.qi4j.api.value.ValueBuilder; -import org.qi4j.api.value.ValueDescriptor; -import org.qi4j.functional.Function; -import org.qi4j.functional.Iterables; - -import static org.qi4j.functional.Iterables.first; - -/** - * Deserialize values to JSON using json.org - */ -public class JSONDeserializer -{ - private static final Set<Class<?>> NON_STRING_CLASSES = new HashSet<Class<?>>(); - - /** - * Check if the given value type should be represented as a JSON string. This can be used - * by clients to figure out whether to add "" around a string to be deserialized. - * - * @param valueType - */ - public static boolean isString( ValueType valueType ) - { - if( !valueType.getClass().equals( ValueType.class ) ) - { - return false; - } - else - { - return !NON_STRING_CLASSES.contains( first( valueType.types() ) ); - } - } - - private static Map<Class<?>, Function<Object, Object>> typeFunctions = new HashMap<Class<?>, Function<Object, Object>>(); - - public static <T> void registerDeserializer( Class<T> type, Function<Object, T> typeFunction ) - { - typeFunctions.put( type, (Function<Object, Object>) typeFunction ); - } - - private static <T> Function<Object, T> identity() - { - return new Function<Object, T>() - { - @Override - public T map( Object json ) - { - return (T) json; - } - }; - } - - static - { - NON_STRING_CLASSES.add( Boolean.class ); - NON_STRING_CLASSES.add( Short.class ); - NON_STRING_CLASSES.add( Integer.class ); - NON_STRING_CLASSES.add( Long.class ); - NON_STRING_CLASSES.add( Byte.class ); - NON_STRING_CLASSES.add( Float.class ); - NON_STRING_CLASSES.add( Double.class ); - - registerDeserializer( String.class, new Function<Object, String>() - { - @Override - public String map( Object o ) - { - return o.toString(); - } - } ); - - // Primitive types - registerDeserializer( Boolean.class, JSONDeserializer.<Boolean>identity() ); - registerDeserializer( Integer.class, new Function<Object, Integer>() - { - @Override - public Integer map( Object o ) - { - return ( (Number) o ).intValue(); - } - } ); - - registerDeserializer( Long.class, new Function<Object, Long>() - { - @Override - public Long map( Object o ) - { - return ( (Number) o ).longValue(); - } - } ); - - registerDeserializer( Short.class, new Function<Object, Short>() - { - @Override - public Short map( Object o ) - { - return ( (Number) o ).shortValue(); - } - } ); - - registerDeserializer( Byte.class, new Function<Object, Byte>() - { - @Override - public Byte map( Object o ) - { - return ( (Number) o ).byteValue(); - } - } ); - - registerDeserializer( Float.class, new Function<Object, Float>() - { - @Override - public Float map( Object o ) - { - return ( (Number) o ).floatValue(); - } - } ); - - registerDeserializer( Double.class, new Function<Object, Double>() - { - @Override - public Double map( Object o ) - { - return ( (Number) o ).doubleValue(); - } - } ); - - // Number types - registerDeserializer( BigDecimal.class, new Function<Object, BigDecimal>() - { - @Override - public BigDecimal map( Object json ) - { - return new BigDecimal( json.toString() ); - } - } ); - registerDeserializer( BigInteger.class, new Function<Object, BigInteger>() - { - @Override - public BigInteger map( Object json ) - { - return new BigInteger( json.toString() ); - } - } ); - - // Date types - registerDeserializer( Date.class, new Function<Object, Date>() - { - @Override - public Date map( Object json ) - { - return Dates.fromString( json.toString() ); - } - } ); - registerDeserializer( DateTime.class, new Function<Object, DateTime>() - { - @Override - public DateTime map( Object json ) - { - return new DateTime( json, DateTimeZone.UTC ); - } - } ); - registerDeserializer( LocalDateTime.class, new Function<Object, LocalDateTime>() - { - @Override - public LocalDateTime map( Object json ) - { - return new LocalDateTime( json ); - } - } ); - registerDeserializer( LocalDate.class, new Function<Object, LocalDate>() - { - @Override - public LocalDate map( Object json ) - { - return new LocalDate( json ); - } - } ); - - // Other supported types - registerDeserializer( EntityReference.class, new Function<Object, EntityReference>() - { - @Override - public EntityReference map( Object json ) - { - return EntityReference.parseEntityReference( json.toString() ); - } - } ); - } - - private Module module; - - public JSONDeserializer( Module module ) - { - this.module = module; - } - - public Object deserialize( Object json, ValueType valueType ) - throws JSONException - { - if( json == JSONObject.NULL ) - { - return null; - } - - Class<?> first = first( valueType.types() ); - Function<Object, ? extends Object> typeFunction = typeFunctions.get( first ); - - if( typeFunction != null ) - { - return typeFunction.map( json ); - } - - if( valueType instanceof CollectionType ) - { - CollectionType collectionType = (CollectionType) valueType; - - JSONArray array = (JSONArray) json; - - Collection<Object> coll; - if( first.equals( Set.class ) ) - { - coll = new LinkedHashSet<Object>(); - - for( int i = 0; i < array.length(); i++ ) - { - Object value = array.get( i ); - coll.add( deserialize( value, collectionType.collectedType() ) ); - } - } - else - { - coll = new ArrayList<Object>(); - - for( int i = 0; i < array.length(); i++ ) - { - Object value = array.get( i ); - coll.add( deserialize( value, collectionType.collectedType() ) ); - } - } - - return coll; - } - else if( valueType instanceof EnumType ) - { - try - { - Class enumType = first; - - // Get enum value - return Enum.valueOf( enumType, (String) json ); - } - catch( Exception e ) - { - throw new IllegalArgumentException( e ); - } - } - else if( valueType instanceof MapType ) - { - if( json instanceof String ) - { - try - { - // Legacy handling of serialized maps - String serializedString = (String) json; - byte[] bytes = serializedString.getBytes( "UTF-8" ); - bytes = Base64Encoder.decode( bytes ); - ByteArrayInputStream bin = new ByteArrayInputStream( bytes ); - ObjectInputStream oin = new ObjectInputStream( bin ); - Object result = oin.readObject(); - oin.close(); - - return result; - } - catch( IOException e ) - { - throw new IllegalStateException( "Could not deserialize value", e ); - } - catch( ClassNotFoundException e ) - { - throw new IllegalStateException( "Could not find class for serialized value", e ); - } - } - else - { - MapType mapType = (MapType) valueType; - - // New array-based handling - JSONArray array = (JSONArray) json; - - Map<Object, Object> map = (Map<Object, Object>) DefaultValues.getDefaultValue( Map.class ); - - for( int i = 0; i < array.length(); i++ ) - { - JSONObject entry = array.getJSONObject( i ); - Object key = deserialize( entry.get( "key" ), mapType.getKeyType() ); - Object value = deserialize( entry.get( "value" ), mapType.getValueType() ); - map.put( key, value ); - } - - return map; - } - } - else if( valueType instanceof ValueCompositeType ) - { - JSONObject jsonObject = (JSONObject) json; - - ValueCompositeType actualValueType = (ValueCompositeType) valueType; - String actualType = jsonObject.optString( "_type" ); - if( !actualType.equals( "" ) ) - { - ValueDescriptor descriptor = module.valueDescriptor( actualType ); - - if( descriptor == null ) - { - throw new IllegalArgumentException( "Could not find any value of type '" + actualType + "' in module" + module ); - } - - actualValueType = descriptor.valueType(); - } - - final Map<QualifiedName, Object> values = new HashMap<QualifiedName, Object>(); - for( PropertyDescriptor persistentProperty : actualValueType.properties() ) - { - Object valueJson = null; - try - { - valueJson = jsonObject.opt( persistentProperty.qualifiedName().name() ); - - Object value = null; - if( valueJson != null && !valueJson.equals( JSONObject.NULL ) ) - { - value = deserialize( valueJson, persistentProperty.valueType() ); - - if( persistentProperty.isImmutable() ) - { - if( value instanceof Set ) - { - value = Collections.unmodifiableSet( (Set<? extends Object>) value ); - } - else if( value instanceof List ) - { - value = Collections.unmodifiableList( (List<? extends Object>) value ); - } - else if( value instanceof Map ) - { - value = Collections.unmodifiableMap( (Map<? extends Object, ? extends Object>) value ); - } - } - } - - values.put( persistentProperty.qualifiedName(), value ); - } - catch( JSONException e ) - { - // Not found in JSON or wrong format - try defaulting it - try - { - Class<?> type = first( persistentProperty.valueType().types() ); - Object defaultValue = DefaultValues.getDefaultValue( type ); - values.put( persistentProperty.qualifiedName(), defaultValue ); - } - catch( RuntimeException e1 ) - { - // Didn't work, throw the exception - throw e; - } - } - } - - for( AssociationDescriptor associationDescriptor : actualValueType.associations() ) - { - String valueJson = jsonObject.optString( associationDescriptor.qualifiedName().name() ); - if( valueJson != null && valueJson.length() > 0 ) - { - values.put( associationDescriptor.qualifiedName(), EntityReference.parseEntityReference( valueJson ) ); - } - } - - for( AssociationDescriptor associationDescriptor : actualValueType.manyAssociations() ) - { - JSONArray jsonArray = jsonObject.optJSONArray( associationDescriptor.qualifiedName().name() ); - if( jsonArray != null ) - { - List<EntityReference> refs = new ArrayList<EntityReference>(); - for( int i = 0; i < jsonArray.length(); i++ ) - { - refs.add( EntityReference.parseEntityReference( jsonArray.getString( i ) ) ); - } - } - } - - Class<?> type = first( actualValueType.types() ); - ValueBuilder valueBuilder = module.newValueBuilderWithState( - type, - new Function<PropertyDescriptor, Object>() - { - @Override - public Object map( PropertyDescriptor descriptor ) - { - return values.get( descriptor.qualifiedName() ); - } - }, new Function<AssociationDescriptor, EntityReference>() - { - @Override - public EntityReference map( - AssociationDescriptor associationDescriptor - ) - { - Object ref = values.get( associationDescriptor - .qualifiedName() ); - if( ref == null ) - { - return null; - } - else - { - return (EntityReference) ref; - } - } - }, new Function<AssociationDescriptor, Iterable<EntityReference>>() - { - @Override - public Iterable<EntityReference> map( AssociationDescriptor associationDescriptor ) - { - Object ref = values.get( associationDescriptor.qualifiedName() ); - if( ref == null ) - { - return Iterables.empty(); - } - else - { - return (Iterable<EntityReference>) ref; - } - } - } - ); - - return valueBuilder.newInstance(); - } - else - { - try - { - if( json instanceof JSONObject ) - { - // ValueComposite deserialization - JSONObject jsonObject = (JSONObject) json; - String type = jsonObject.getString( "_type" ); - - ValueDescriptor valueDescriptor = module.valueDescriptor( type ); - return deserialize( json, valueDescriptor.valueType() ); - } - else - { - String serializedString = (String) json; - byte[] bytes = serializedString.getBytes( "UTF-8" ); - bytes = Base64Encoder.decode( bytes ); - ByteArrayInputStream bin = new ByteArrayInputStream( bytes ); - ObjectInputStream oin = new ObjectInputStream( bin ); - Object result = oin.readObject(); - oin.close(); - - if( result instanceof EntityReference ) - { - EntityReference ref = (EntityReference) result; - Class<?> type = first( valueType.types() ); - if( !type.equals( EntityReference.class ) ) - { - Class mixinType = type; - if( module.isUnitOfWorkActive() ) - { - UnitOfWork unitOfWork = module.currentUnitOfWork(); - result = unitOfWork.get( mixinType, ref.identity() ); - } - } - } - - return result; - } - } - catch( IOException e ) - { - throw new IllegalStateException( "Could not deserialize value", e ); - } - catch( ClassNotFoundException e ) - { - throw new IllegalStateException( "Could not find class for serialized value", e ); - } - } - } -} http://git-wip-us.apache.org/repos/asf/zest-qi4j/blob/3a7fb782/core/api/src/main/java/org/qi4j/api/json/JSONObjectSerializer.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/json/JSONObjectSerializer.java b/core/api/src/main/java/org/qi4j/api/json/JSONObjectSerializer.java deleted file mode 100644 index a06278a..0000000 --- a/core/api/src/main/java/org/qi4j/api/json/JSONObjectSerializer.java +++ /dev/null @@ -1,92 +0,0 @@ -package org.qi4j.api.json; - -import java.util.Stack; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; - -/** - * JSON Object Serializer. - */ -public class JSONObjectSerializer - extends JSONSerializer -{ - Stack<Object> stack = new Stack<Object>(); - Stack<String> keys = new Stack<String>(); - - private Object root; - - @Override - public JSONSerializer key( String key ) - throws JSONException - { - keys.push( key ); - return this; - } - - @Override - public JSONSerializer value( Object value ) - throws JSONException - { - if( stack.empty() ) - { - root = value; - } - else if( stack.peek() instanceof JSONArray ) - { - ( (JSONArray) stack.peek() ).put( value ); - } - else - { - ( (JSONObject) stack.peek() ).put( keys.pop(), value ); - } - return this; - } - - @Override - public JSONSerializer objectStart() - throws JSONException - { - JSONObject jsonObject = new JSONObject(); - if( stack.isEmpty() ) - { - root = jsonObject; - } - stack.push( jsonObject ); - return this; - } - - @Override - public JSONSerializer objectEnd() - throws JSONException - { - value( stack.pop() ); - return this; - } - - @Override - public JSONSerializer arrayStart() - throws JSONException - { - JSONArray jsonArray = new JSONArray(); - if( stack.isEmpty() ) - { - root = jsonArray; - } - stack.push( jsonArray ); - return this; - } - - @Override - public JSONSerializer arrayEnd() - throws JSONException - { - value( stack.pop() ); - return this; - } - - public Object getRoot() - { - return root; - } -} http://git-wip-us.apache.org/repos/asf/zest-qi4j/blob/3a7fb782/core/api/src/main/java/org/qi4j/api/json/JSONSerializer.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/json/JSONSerializer.java b/core/api/src/main/java/org/qi4j/api/json/JSONSerializer.java deleted file mode 100644 index 6724167..0000000 --- a/core/api/src/main/java/org/qi4j/api/json/JSONSerializer.java +++ /dev/null @@ -1,336 +0,0 @@ -package org.qi4j.api.json; - -import org.qi4j.api.util.Base64Encoder; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.ObjectOutputStream; -import java.math.BigDecimal; -import java.math.BigInteger; -import java.util.Collection; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; -import org.joda.time.DateTime; -import org.joda.time.LocalDate; -import org.joda.time.LocalDateTime; -import org.json.JSONException; -import org.json.JSONObject; -import org.qi4j.api.Qi4j; -import org.qi4j.api.association.Association; -import org.qi4j.api.association.AssociationDescriptor; -import org.qi4j.api.association.AssociationStateHolder; -import org.qi4j.api.association.ManyAssociation; -import org.qi4j.api.composite.Composite; -import org.qi4j.api.entity.EntityComposite; -import org.qi4j.api.entity.EntityReference; -import org.qi4j.api.entity.Identity; -import org.qi4j.api.property.Property; -import org.qi4j.api.property.PropertyDescriptor; -import org.qi4j.api.type.CollectionType; -import org.qi4j.api.type.EnumType; -import org.qi4j.api.type.MapType; -import org.qi4j.api.type.ValueCompositeType; -import org.qi4j.api.type.ValueType; -import org.qi4j.api.util.Dates; -import org.qi4j.api.value.ValueComposite; -import org.qi4j.api.value.ValueDescriptor; -import org.qi4j.functional.Function; -import org.qi4j.functional.Functions; - -import static org.qi4j.functional.Iterables.first; - -/** - * Base json.org JSON Serializer. - */ -public abstract class JSONSerializer -{ - private static Map<Class, Function<Object, Object>> typeFunctions = new HashMap<Class, Function<Object, Object>>(); - - public static <T> void registerSerializer( Class<T> type, Function<T, Object> typeFunction ) - { - typeFunctions.put( type, (Function<Object, Object>) typeFunction ); - } - - static - { - registerSerializer( String.class, Functions.<Object, String>identity() ); - - // Primitive types - registerSerializer( Boolean.class, Functions.<Object, Boolean>identity() ); - registerSerializer( Integer.class, Functions.<Object, Integer>identity() ); - registerSerializer( Long.class, Functions.<Object, Long>identity() ); - registerSerializer( Short.class, Functions.<Object, Short>identity() ); - registerSerializer( Byte.class, Functions.<Object, Byte>identity() ); - registerSerializer( Float.class, Functions.<Object, Float>identity() ); - registerSerializer( Double.class, Functions.<Object, Double>identity() ); - - // Number types - registerSerializer( BigDecimal.class, new Function<BigDecimal, Object>() - { - @Override - public Object map( BigDecimal bigDecimal ) - { - return bigDecimal; - } - } ); - - registerSerializer( BigInteger.class, new Function<BigInteger, Object>() - { - @Override - public Object map( BigInteger bigInteger ) - { - return bigInteger; - } - } ); - - // Date types - registerSerializer( Date.class, new Function<Date, Object>() - { - @Override - public Object map( Date date ) - { - return Dates.toUtcString( date ); - } - } ); - - registerSerializer( DateTime.class, new Function<DateTime, Object>() - { - @Override - public Object map( DateTime date ) - { - return date.toString(); - } - } ); - registerSerializer( LocalDateTime.class, new Function<LocalDateTime, Object>() - { - @Override - public Object map( LocalDateTime date ) - { - return date.toString(); - } - } ); - registerSerializer( LocalDate.class, new Function<LocalDate, Object>() - { - @Override - public Object map( LocalDate date ) - { - return date.toString(); - } - } ); - - // Other supported types - registerSerializer( EntityReference.class, new Function<EntityReference, Object>() - { - @Override - public Object map( EntityReference date ) - { - return date.toString(); - } - } ); - } - - private boolean includeTypeInformation = true; - - public void setIncludeType( boolean includeTypeInformation ) - { - this.includeTypeInformation = includeTypeInformation; - } - - public void serialize( Object value ) - throws JSONException - { - serialize( (ValueComposite) value ); - } - - public void serialize( ValueComposite value ) - throws JSONException - { - ValueDescriptor valueDescriptor = (ValueDescriptor) Qi4j.DESCRIPTOR_FUNCTION.map( value ); - - ValueType valueType = valueDescriptor.valueType(); - - serialize( value, valueType ); - } - - public void serialize( Object value, ValueType valueType ) - throws JSONException - { - // Check for null first - if( value == null ) - { - value( null ); - return; - } - - // Try functions second - Function<Object, Object> typeFunction = typeFunctions.get( first( valueType.types() ) ); - if( typeFunction != null ) - { - value( typeFunction.map( value ) ); - } - else if( valueType instanceof ValueCompositeType ) // Handle all other types - { - ValueCompositeType valueCompositeType = (ValueCompositeType) valueType; - - objectStart(); - ValueComposite valueComposite = (ValueComposite) value; - - if( !first( valueCompositeType.types() ).equals( first( Qi4j.DESCRIPTOR_FUNCTION - .map( valueComposite ) - .types() ) ) ) - { - // Actual value is a subtype - use it instead - ValueDescriptor valueDescriptor = (ValueDescriptor) Qi4j.DESCRIPTOR_FUNCTION.map( valueComposite ); - - valueCompositeType = valueDescriptor.valueType(); - - if( includeTypeInformation ) - { - key( "_type" ).value( first( valueDescriptor.valueType().types() ).getName() ); - } - } - - AssociationStateHolder state = (AssociationStateHolder) Qi4j.INSTANCE_FUNCTION - .map( valueComposite ) - .state(); - for( PropertyDescriptor persistentProperty : valueCompositeType.properties() ) - { - Property<?> property = state.propertyFor( persistentProperty.accessor() ); - key( persistentProperty.qualifiedName() - .name() ).serialize( property.get(), persistentProperty.valueType() ); - } - for( AssociationDescriptor associationDescriptor : valueCompositeType.associations() ) - { - Association<?> association = state.associationFor( associationDescriptor.accessor() ); - - Object instance = association.get(); - if( instance != null ) - { - key( associationDescriptor.qualifiedName().name() ).value( ( (Identity) instance ).identity() - .get() ); - } - } - for( AssociationDescriptor associationDescriptor : valueCompositeType.manyAssociations() ) - { - ManyAssociation<?> manyAssociation = state.manyAssociationFor( associationDescriptor.accessor() ); - key( associationDescriptor.qualifiedName() - .name() ).serialize( manyAssociation.toList(), new CollectionType( List.class, new ValueType( String.class ) ) ); - } - objectEnd(); - } - else if( valueType instanceof CollectionType ) - { - CollectionType collectionType = (CollectionType) valueType; - - arrayStart(); - - Collection collection = (Collection) value; - for( Object collectionValue : collection ) - { - serialize( collectionValue, collectionType.collectedType() ); - } - - arrayEnd(); - } - else if( valueType instanceof MapType ) - { - arrayStart(); - - MapType mapType = (MapType) valueType; - Map map = (Map) value; - Set<Map.Entry> set = map.entrySet(); - for( Map.Entry<Object, Object> entry : set ) - { - objectStart(); - key( "key" ).serialize( entry.getKey(), mapType.getKeyType() ); - key( "value" ).serialize( entry.getValue(), mapType.getValueType() ); - objectEnd(); - } - - arrayEnd(); - } - else if( valueType instanceof EnumType ) - { - value( value.toString() ); - } - else - { -/* TODO How to handle deserialization? - // Try for the actual value class first - typeFunction = typeFunctions.get( value.getClass() ); - if( typeFunction != null ) - { - value( typeFunction.map( value ) ); - return; - } -*/ - - // Check if we are serializing an Entity - if( value instanceof EntityComposite ) - { - // Store reference instead - value = EntityReference.getEntityReference( value ); - } - else if( value instanceof ValueComposite ) - { - // Serialize ValueComposite JSON instead - try - { - JSONObjectSerializer JSONObjectSerializer = new JSONObjectSerializer(); - JSONObjectSerializer.serialize( (ValueComposite) value ); - - JSONObject object = (JSONObject) JSONObjectSerializer.getRoot(); - - ValueDescriptor descriptor = (ValueDescriptor) Qi4j.DESCRIPTOR_FUNCTION.map( (Composite) value ); - - if( includeTypeInformation ) - { - object.put( "_type", first( descriptor.types() ).getName() ); - } - value( object ); - return; - } - catch( JSONException e ) - { - throw new IllegalStateException( "Could not JSON serialize value", e ); - } - } - - // Serialize value - try - { - ByteArrayOutputStream bout = new ByteArrayOutputStream(); - ObjectOutputStream out = new ObjectOutputStream( bout ); - out.writeUnshared( value ); - out.close(); - byte[] bytes = Base64Encoder.encode( bout.toByteArray(), true ); - String stringValue = new String( bytes, "UTF-8" ); - value( stringValue ); - } - catch( IOException e ) - { - throw new IllegalArgumentException( "Could not serialize value", e ); - } - } - } - - public abstract JSONSerializer key( String key ) - throws JSONException; - - public abstract JSONSerializer value( Object value ) - throws JSONException; - - public abstract JSONSerializer objectStart() - throws JSONException; - - public abstract JSONSerializer objectEnd() - throws JSONException; - - public abstract JSONSerializer arrayStart() - throws JSONException; - - public abstract JSONSerializer arrayEnd() - throws JSONException; -} http://git-wip-us.apache.org/repos/asf/zest-qi4j/blob/3a7fb782/core/api/src/main/java/org/qi4j/api/json/JSONWriterSerializer.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/json/JSONWriterSerializer.java b/core/api/src/main/java/org/qi4j/api/json/JSONWriterSerializer.java deleted file mode 100644 index a511f9e..0000000 --- a/core/api/src/main/java/org/qi4j/api/json/JSONWriterSerializer.java +++ /dev/null @@ -1,83 +0,0 @@ -package org.qi4j.api.json; - -import java.io.Writer; -import org.json.JSONException; -import org.json.JSONStringer; -import org.json.JSONWriter; - -/** - * Serialize values to JSON using json.org - */ -public class JSONWriterSerializer - extends JSONSerializer -{ - JSONWriter json; - - public JSONWriterSerializer() - { - this.json = new JSONStringer(); - } - - public JSONWriterSerializer( Writer writer ) - { - this.json = new JSONWriter( writer ); - } - - public JSONWriterSerializer( JSONWriter writer ) - { - this.json = writer; - } - - @Override - public JSONSerializer key( String value ) - throws JSONException - { - json.key( value ); - return this; - } - - @Override - public JSONSerializer value( Object value ) - throws JSONException - { - json.value( value ); - return this; - } - - @Override - public JSONSerializer objectStart() - throws JSONException - { - json.object(); - return this; - } - - @Override - public JSONSerializer objectEnd() - throws JSONException - { - json.endObject(); - return this; - } - - @Override - public JSONSerializer arrayStart() - throws JSONException - { - json.array(); - return this; - } - - @Override - public JSONSerializer arrayEnd() - throws JSONException - { - json.endArray(); - return this; - } - - public JSONWriter getJSON() - { - return json; - } -} http://git-wip-us.apache.org/repos/asf/zest-qi4j/blob/3a7fb782/core/api/src/main/java/org/qi4j/api/json/package.html ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/qi4j/api/json/package.html b/core/api/src/main/java/org/qi4j/api/json/package.html deleted file mode 100644 index ff9d020..0000000 --- a/core/api/src/main/java/org/qi4j/api/json/package.html +++ /dev/null @@ -1,5 +0,0 @@ -<html> - <body> - <h2>JSON API.</h2> - </body> -</html> http://git-wip-us.apache.org/repos/asf/zest-qi4j/blob/3a7fb782/core/runtime/src/test/java/org/qi4j/runtime/types/JodaDateTimeTypeTest.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/test/java/org/qi4j/runtime/types/JodaDateTimeTypeTest.java b/core/runtime/src/test/java/org/qi4j/runtime/types/JodaDateTimeTypeTest.java deleted file mode 100644 index bb38438..0000000 --- a/core/runtime/src/test/java/org/qi4j/runtime/types/JodaDateTimeTypeTest.java +++ /dev/null @@ -1,41 +0,0 @@ -package org.qi4j.runtime.types; - -import org.joda.time.DateTime; -import org.joda.time.DateTimeZone; -import org.junit.Before; -import org.junit.Test; -import org.qi4j.api.json.JSONDeserializer; -import org.qi4j.api.json.JSONObjectSerializer; -import org.qi4j.api.type.ValueType; - -import static org.junit.Assert.assertEquals; - -public class JodaDateTimeTypeTest -{ - - private ValueType underTest; - - @Before - public void setup() - { - underTest = new ValueType( DateTime.class ); - } - - @Test - public void givenLocalDateTypeWhenConvertingToJsonExpectValidString() - throws Exception - { - JSONObjectSerializer serializer = new JSONObjectSerializer(); - serializer.serialize( new DateTime( "2020-03-04T13:24:35", DateTimeZone.UTC ), underTest ); - Object value = serializer.getRoot(); - assertEquals( "2020-03-04T13:24:35.000Z", value.toString() ); - } - - @Test - public void givenLocalDateTypeWhenConvertingFromJsonExpectValidLocalDate() - throws Exception - { - Object value = new JSONDeserializer( null ).deserialize( "2020-03-04T12:23:33Z", underTest ); - assertEquals( new DateTime( "2020-03-04T12:23:33Z", DateTimeZone.UTC ), value ); - } -} http://git-wip-us.apache.org/repos/asf/zest-qi4j/blob/3a7fb782/core/runtime/src/test/java/org/qi4j/runtime/types/JodaLocalDateTimeTypeTest.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/test/java/org/qi4j/runtime/types/JodaLocalDateTimeTypeTest.java b/core/runtime/src/test/java/org/qi4j/runtime/types/JodaLocalDateTimeTypeTest.java deleted file mode 100644 index 694d4dd..0000000 --- a/core/runtime/src/test/java/org/qi4j/runtime/types/JodaLocalDateTimeTypeTest.java +++ /dev/null @@ -1,41 +0,0 @@ -package org.qi4j.runtime.types; - -import org.joda.time.DateTimeZone; -import org.joda.time.LocalDateTime; -import org.junit.Before; -import org.junit.Test; -import org.qi4j.api.json.JSONDeserializer; -import org.qi4j.api.json.JSONObjectSerializer; -import org.qi4j.api.type.ValueType; - -import static org.junit.Assert.assertEquals; - -public class JodaLocalDateTimeTypeTest -{ - - private ValueType underTest; - - @Before - public void setup() - { - underTest = new ValueType( LocalDateTime.class ); - } - - @Test - public void givenLocalDateTypeWhenConvertingToJsonExpectValidString() - throws Exception - { - JSONObjectSerializer serializer = new JSONObjectSerializer(); - serializer.serialize( new LocalDateTime( "2020-03-04T13:23:00", DateTimeZone.UTC ), underTest ); - Object value = serializer.getRoot(); - assertEquals( "2020-03-04T13:23:00.000", value.toString() ); - } - - @Test - public void givenLocalDateTypeWhenConvertingFromJsonExpectValidLocalDate() - throws Exception - { - Object value = new JSONDeserializer( null ).deserialize( "2020-03-04T12:23:09", underTest ); - assertEquals( new LocalDateTime( "2020-03-04T12:23:09", DateTimeZone.UTC ), value ); - } -} http://git-wip-us.apache.org/repos/asf/zest-qi4j/blob/3a7fb782/core/runtime/src/test/java/org/qi4j/runtime/types/JodaLocalDateTypeTest.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/test/java/org/qi4j/runtime/types/JodaLocalDateTypeTest.java b/core/runtime/src/test/java/org/qi4j/runtime/types/JodaLocalDateTypeTest.java deleted file mode 100644 index fb51b6d..0000000 --- a/core/runtime/src/test/java/org/qi4j/runtime/types/JodaLocalDateTypeTest.java +++ /dev/null @@ -1,40 +0,0 @@ -package org.qi4j.runtime.types; - -import org.joda.time.LocalDate; -import org.junit.Before; -import org.junit.Test; -import org.qi4j.api.json.JSONDeserializer; -import org.qi4j.api.json.JSONObjectSerializer; -import org.qi4j.api.type.ValueType; - -import static org.junit.Assert.assertEquals; - -public class JodaLocalDateTypeTest -{ - - private ValueType underTest; - - @Before - public void setup() - { - underTest = new ValueType( LocalDate.class ); - } - - @Test - public void givenLocalDateTypeWhenConvertingToJsonExpectValidString() - throws Exception - { - JSONObjectSerializer serializer = new JSONObjectSerializer(); - serializer.serialize( new LocalDate( "2020-03-04" ), underTest ); - Object value = serializer.getRoot(); - assertEquals( "2020-03-04", value.toString() ); - } - - @Test - public void givenLocalDateTypeWhenConvertingFromJsonExpectValidLocalDate() - throws Exception - { - Object value = new JSONDeserializer( null ).deserialize( "2020-03-04", underTest ); - assertEquals( new LocalDate( "2020-03-04" ), value ); - } -} http://git-wip-us.apache.org/repos/asf/zest-qi4j/blob/3a7fb782/core/runtime/src/test/java/org/qi4j/runtime/types/ValueLookupTest.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/test/java/org/qi4j/runtime/types/ValueLookupTest.java b/core/runtime/src/test/java/org/qi4j/runtime/types/ValueLookupTest.java deleted file mode 100644 index d2eb8e8..0000000 --- a/core/runtime/src/test/java/org/qi4j/runtime/types/ValueLookupTest.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (c) 2012, Paul Merlin. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -package org.qi4j.runtime.types; - -import org.junit.Test; -import org.qi4j.api.common.Visibility; -import org.qi4j.api.property.Property; -import org.qi4j.api.value.ValueComposite; -import org.qi4j.bootstrap.ApplicationAssembly; -import org.qi4j.bootstrap.AssemblyException; -import org.qi4j.bootstrap.LayerAssembly; -import org.qi4j.bootstrap.ModuleAssembly; -import org.qi4j.test.AbstractQi4jTest; - -public class ValueLookupTest - extends AbstractQi4jTest -{ - - @Override - public void assemble( ModuleAssembly testModule ) - throws AssemblyException - { - LayerAssembly testLayer = testModule.layer(); - ApplicationAssembly app = testLayer.application(); - - LayerAssembly domainLayer = app.layer( "domain" ); - LayerAssembly commonLayer = app.layer( "common" ); - - ModuleAssembly domainModule = domainLayer.module( "domain" ); - ModuleAssembly commonModule = commonLayer.module( "common" ); - - domainModule.values( DomainValueUsingCommonValue.class ).visibleIn( Visibility.application ); - commonModule.values( CommonValue.class ).visibleIn( Visibility.application ); - - testLayer.uses( domainLayer, commonLayer ); - domainLayer.uses( commonLayer ); - } - - public interface DomainValueUsingCommonValue - extends ValueComposite - { - - Property<CommonValue> commonValue(); - - } - - public interface CommonValue - extends ValueComposite - { - } - - @Test - public void testFromPresentation() - { - // If we get there, problem is solved as it fails during assembly. - } - -} http://git-wip-us.apache.org/repos/asf/zest-qi4j/blob/3a7fb782/core/runtime/src/test/java/org/qi4j/runtime/value/CollectionTypeTest.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/test/java/org/qi4j/runtime/value/CollectionTypeTest.java b/core/runtime/src/test/java/org/qi4j/runtime/value/CollectionTypeTest.java deleted file mode 100644 index f0debe6..0000000 --- a/core/runtime/src/test/java/org/qi4j/runtime/value/CollectionTypeTest.java +++ /dev/null @@ -1,443 +0,0 @@ -/* - * Copyright 2009 Niclas Hedhman. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - * implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.qi4j.runtime.value; - -import java.math.BigDecimal; -import java.math.BigInteger; -import java.util.ArrayList; -import java.util.Collection; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Set; -import org.json.JSONArray; -import org.junit.Test; -import org.qi4j.api.json.JSONDeserializer; -import org.qi4j.api.json.JSONObjectSerializer; -import org.qi4j.api.type.CollectionType; -import org.qi4j.api.type.ValueType; - -import static org.junit.Assert.assertEquals; - -public class CollectionTypeTest -{ - - @Test - public void givenCollectionTypeWithByteAndNullElementWhenSerializingExpectCorrectJsonOutput() - throws Exception - { - ValueType collectedType = new ValueType( Byte.class ); - CollectionType collectionType = new CollectionType( Collection.class, collectedType ); - - Collection<Byte> value = byteCollection(); - JSONObjectSerializer serializer = new JSONObjectSerializer(); - serializer.serialize( value, collectionType ); - Object json = serializer.getRoot(); - assertEquals( byteJson(), json.toString() ); - } - - @Test - public void givenCollectionTypeWithShortAndNullElementWhenSerializingExpectCorrectJsonOutput() - throws Exception - { - ValueType collectedType = new ValueType( Short.class ); - CollectionType collectionType = new CollectionType( Collection.class, collectedType ); - - Collection<Short> value = shortCollection(); - JSONObjectSerializer serializer = new JSONObjectSerializer(); - serializer.serialize( value, collectionType ); - Object json = serializer.getRoot(); - assertEquals( shortJson(), json.toString() ); - } - - @Test - public void givenCollectionTypeWithIntegerAndNullElementWhenSerializingExpectCorrectJsonOutput() - throws Exception - { - ValueType collectedType = new ValueType( Integer.class ); - CollectionType collectionType = new CollectionType( Collection.class, collectedType ); - - Collection<Integer> value = integerCollection(); - JSONObjectSerializer serializer = new JSONObjectSerializer(); - serializer.serialize( value, collectionType ); - Object json = serializer.getRoot(); - assertEquals( integerJson(), json.toString() ); - } - - @Test - public void givenCollectionTypeWithLongAndNullElementWhenSerializingExpectCorrectJsonOutput() - throws Exception - { - ValueType collectedType = new ValueType( Long.class ); - CollectionType collectionType = new CollectionType( Collection.class, collectedType ); - - Collection<Long> value = longCollection(); - JSONObjectSerializer serializer = new JSONObjectSerializer(); - serializer.serialize( value, collectionType ); - Object json = serializer.getRoot(); - assertEquals( longJson(), json.toString() ); - } - - @Test - public void givenCollectionTypeWithFloatAndNullElementWhenSerializingExpectCorrectJsonOutput() - throws Exception - { - ValueType collectedType = new ValueType( Float.class ); - CollectionType collectionType = new CollectionType( Collection.class, collectedType ); - - Collection<Float> value = floatCollection(); - JSONObjectSerializer serializer = new JSONObjectSerializer(); - serializer.serialize( value, collectionType ); - Object json = serializer.getRoot(); - assertEquals( floatJson(), json.toString() ); - } - - @Test - public void givenCollectionTypeWithDoubleAndNullElementWhenSerializingExpectCorrectJsonOutput() - throws Exception - { - ValueType collectedType = new ValueType( Double.class ); - CollectionType collectionType = new CollectionType( Collection.class, collectedType ); - - Collection<Double> value = doubleCollection(); - JSONObjectSerializer serializer = new JSONObjectSerializer(); - serializer.serialize( value, collectionType ); - Object json = serializer.getRoot(); - assertEquals( doubleJson(), json.toString() ); - } - - @Test - public void givenCollectionTypeWithBigIntegerAndNullElementWhenSerializingExpectCorrectJsonOutput() - throws Exception - { - ValueType collectedType = new ValueType( BigInteger.class ); - CollectionType collectionType = new CollectionType( Collection.class, collectedType ); - - Collection<BigInteger> value = bigIntegerCollection(); - JSONObjectSerializer serializer = new JSONObjectSerializer(); - serializer.serialize( value, collectionType ); - Object json = serializer.getRoot(); - assertEquals( bigIntegerJson(), json.toString() ); - } - - @Test - public void givenCollectionTypeWithBigDecimalAndNullElementWhenSerializingExpectCorrectJsonOutput() - throws Exception - { - ValueType collectedType = new ValueType( BigDecimal.class ); - CollectionType collectionType = new CollectionType( Collection.class, collectedType ); - - Collection<BigDecimal> value = bigDecimalCollection(); - JSONObjectSerializer serializer = new JSONObjectSerializer(); - serializer.serialize( value, collectionType ); - Object json = serializer.getRoot(); - assertEquals( bigDecimalJson(), json.toString() ); - } - - @Test - public void givenJsonOfByteListWhenDeserializingExpectCorrectValueOutput() - throws Exception - { - ValueType collectedType = new ValueType( Byte.class ); - CollectionType collectionType = new CollectionType( List.class, collectedType ); - Object json = new JSONArray( byteJson() ); - List<Byte> result = (List<Byte>) new JSONDeserializer( null ).deserialize( json, collectionType ); - ArrayList<Byte> bytes = byteCollection(); - for( int i = 0; i < result.size(); i++ ) - { - Byte resultByte = result.get( i ); - if( resultByte != null ) - { - assertEquals( Byte.class, resultByte.getClass() ); - } - assertEquals( bytes.get( i ), resultByte ); - } - } - - @Test - public void givenJsonOfByteSetWhenDeserializingExpectCorrectValueOutput() - throws Exception - { - ValueType collectedType = new ValueType( Byte.class ); - CollectionType collectionType = new CollectionType( Set.class, collectedType ); - Object json = new JSONArray( byteJson() ); - Set<Byte> result = (Set<Byte>) new JSONDeserializer( null ).deserialize( json, collectionType ); - Set<Byte> bytes = new LinkedHashSet<Byte>( byteCollection() ); - assertEquals( bytes, result ); - } - - @Test - public void givenJsonOfByteCollectionWhenDeserializingExpectCorrectValueOutput() - throws Exception - { - ValueType collectedType = new ValueType( Byte.class ); - CollectionType collectionType = new CollectionType( Collection.class, collectedType ); - Object json = new JSONArray( byteJson() ); - List<Byte> result = (List<Byte>) new JSONDeserializer( null ).deserialize( json, collectionType ); - assertEquals( byteCollection(), result ); - } - - @Test - public void givenJsonOfShortCollectionWhenDeserializingExpectCorrectValueOutput() - throws Exception - { - ValueType collectedType = new ValueType( Short.class ); - CollectionType collectionType = new CollectionType( Collection.class, collectedType ); - Object json = new JSONArray( shortJson() ); - List<Short> result = (List<Short>) new JSONDeserializer( null ).deserialize( json, collectionType ); - assertEquals( shortCollection(), result ); - } - - @Test - public void givenJsonOfIntegerCollectionWhenDeserializingExpectCorrectValueOutput() - throws Exception - { - ValueType collectedType = new ValueType( Integer.class ); - CollectionType collectionType = new CollectionType( Collection.class, collectedType ); - Object json = new JSONArray( integerJson() ); - List<Integer> result = (List<Integer>) new JSONDeserializer( null ).deserialize( json, collectionType ); - assertEquals( integerCollection(), result ); - } - - @Test - public void givenJsonOfLongCollectionWhenDeserializingExpectCorrectValueOutput() - throws Exception - { - ValueType collectedType = new ValueType( Long.class ); - CollectionType collectionType = new CollectionType( Collection.class, collectedType ); - Object json = new JSONArray( longJson() ); - List<Long> result = (List<Long>) new JSONDeserializer( null ).deserialize( json, collectionType ); - assertEquals( longCollection(), result ); - } - - @Test - public void givenJsonOfFloatCollectionWhenDeserializingExpectCorrectValueOutput() - throws Exception - { - ValueType collectedType = new ValueType( Float.class ); - CollectionType collectionType = new CollectionType( Collection.class, collectedType ); - Object json = new JSONArray( floatJson() ); - List<Float> result = (List<Float>) new JSONDeserializer( null ).deserialize( json, collectionType ); - assertEquals( floatCollection(), result ); - } - - @Test - public void givenJsonOfDoubleCollectionWhenDeserializingExpectCorrectValueOutput() - throws Exception - { - ValueType collectedType = new ValueType( Double.class ); - CollectionType collectionType = new CollectionType( Collection.class, collectedType ); - Object json = new JSONArray( doubleJson() ); - List<Double> result = (List<Double>) new JSONDeserializer( null ).deserialize( json, collectionType ); - assertEquals( doubleCollection(), result ); - } - - @Test - public void givenJsonOfBigIntegerCollectionWhenDeserializingExpectCorrectValueOutput() - throws Exception - { - ValueType collectedType = new ValueType( BigInteger.class ); - CollectionType collectionType = new CollectionType( Collection.class, collectedType ); - Object json = new JSONArray( bigIntegerJson() ); - List<BigInteger> result = (List<BigInteger>) new JSONDeserializer( null ).deserialize( json, collectionType ); - assertEquals( bigIntegerCollection(), result ); - } - - @Test - public void givenJsonOfBigDecimalCollectionWhenDeserializingExpectCorrectValueOutput() - throws Exception - { - ValueType collectedType = new ValueType( BigDecimal.class ); - CollectionType collectionType = new CollectionType( Collection.class, collectedType ); - Object json = new JSONArray( bigDecimalJson() ); - List<BigDecimal> result = (List<BigDecimal>) new JSONDeserializer( null ).deserialize( json, collectionType ); - assertEquals( bigDecimalCollection(), result ); - } - - @Test - public void givenJsonOfBigIntegerCollectionWithQuotesWhenDeserializingExpectCorrectValueOutput() - throws Exception - { - ValueType collectedType = new ValueType( BigInteger.class ); - CollectionType collectionType = new CollectionType( Collection.class, collectedType ); - Object json = new JSONArray( bigIntegerJsonWithQuotes() ); - List<BigDecimal> result = (List<BigDecimal>) new JSONDeserializer( null ).deserialize( json, collectionType ); - assertEquals( bigIntegerCollection(), result ); - } - - @Test - public void givenJsonOfBigDecimalCollectionWithQuotesWhenDeserializingExpectCorrectValueOutput() - throws Exception - { - ValueType collectedType = new ValueType( BigDecimal.class ); - CollectionType collectionType = new CollectionType( Collection.class, collectedType ); - Object json = new JSONArray( bigDecimalJsonWithQuotes() ); - List<BigDecimal> result = (List<BigDecimal>) new JSONDeserializer( null ).deserialize( json, collectionType ); - assertEquals( bigDecimalCollection(), result ); - } - - private String byteJson() - { - return "[9,null,-12,-12,127,-128,73]"; - } - - private ArrayList<Byte> byteCollection() - { - ArrayList<Byte> value = new ArrayList<Byte>(); - value.add( (byte) 9 ); - value.add( null ); - value.add( (byte) -12 ); - value.add( (byte) -12 ); - value.add( (byte) 127 ); - value.add( (byte) -128 ); - value.add( (byte) 73 ); - return value; - } - - private String shortJson() - { - return "[-32768,32767,-82,null]"; - } - - private Collection<Short> shortCollection() - { - Collection<Short> value = new ArrayList<Short>(); - value.add( (short) -32768 ); - value.add( (short) 32767 ); - value.add( (short) -82 ); - value.add( null ); - return value; - } - - private String integerJson() - { - return "[2147483647,-283,null,-2147483648,238]"; - } - - private Collection<Integer> integerCollection() - { - Collection<Integer> value = new ArrayList<Integer>(); - value.add( Integer.MAX_VALUE ); - value.add( -283 ); - value.add( null ); - value.add( Integer.MIN_VALUE ); - value.add( 238 ); - return value; - } - - private String longJson() - { - return "[98239723,-1298233,-1,0,null,1,9223372036854775807,-9223372036854775808]"; - } - - private Collection<Long> longCollection() - { - Collection<Long> value = new ArrayList<Long>(); - value.add( 98239723L ); - value.add( -1298233L ); - value.add( -1L ); - value.add( 0L ); - value.add( null ); - value.add( 1L ); - value.add( Long.MAX_VALUE ); - value.add( Long.MIN_VALUE ); - return value; - } - - private String floatJson() - { - return "[-1,1,1,0,3.4028235E38,1.4E-45,null,0.123456,-0.232321]"; - } - - private Collection<Float> floatCollection() - { - Collection<Float> value = new ArrayList<Float>(); - value.add( -1f ); - value.add( 1f ); - value.add( 1f ); - value.add( 0f ); - value.add( Float.MAX_VALUE ); - value.add( Float.MIN_VALUE ); - value.add( null ); - value.add( 0.123456f ); - value.add( -0.232321f ); - return value; - } - - private String doubleJson() - { - return "[-1,1,0,1.7976931348623157E308,null,4.9E-324,0.123456,-0.232321]"; - } - - private Collection<Double> doubleCollection() - { - Collection<Double> value = new ArrayList<Double>(); - value.add( -1.0 ); - value.add( 1.0 ); - value.add( 0.0 ); - value.add( Double.MAX_VALUE ); - value.add( null ); - value.add( Double.MIN_VALUE ); - value.add( 0.123456 ); - value.add( -0.232321 ); - return value; - } - - private String bigIntegerJson() - { - return "[-1,0,1,null,10,-1827368263823729372397239829332,2398723982982379827373972398723]"; - } - - private String bigIntegerJsonWithQuotes() - { - return "[\"-1\",\"0\",\"1\",null,\"10\",\"-1827368263823729372397239829332\",\"2398723982982379827373972398723\"]"; - } - - private Collection<BigInteger> bigIntegerCollection() - { - Collection<BigInteger> value = new ArrayList<BigInteger>(); - value.add( new BigInteger( "-1" ) ); - value.add( BigInteger.ZERO ); - value.add( BigInteger.ONE ); - value.add( null ); - value.add( BigInteger.TEN ); - value.add( new BigInteger( "-1827368263823729372397239829332" ) ); - value.add( new BigInteger( "2398723982982379827373972398723" ) ); - return value; - } - - private String bigDecimalJson() - { - return "[1.2,3.4,null,5.6]"; - } - - private String bigDecimalJsonWithQuotes() - { - return "[\"1.2\",\"3.4\",null,\"5.6\"]"; - } - - private Collection<BigDecimal> bigDecimalCollection() - { - Collection<BigDecimal> value = new ArrayList<BigDecimal>(); - value.add( new BigDecimal( "1.2" ) ); - value.add( new BigDecimal( "3.4" ) ); - value.add( null ); - value.add( new BigDecimal( "5.6" ) ); - return value; - } -}
