private Mapper createMapper() { if (useAnnotations && !caseInsensitiveFields && !acceptSingleValueAsArray && ignoreSet == null && Str.isEmpty(view) && respectIgnore) { return new MapperSimple(fieldAccessType.create(true)); } return new MapperComplex(fieldAccessType, useAnnotations, caseInsensitiveFields, ignoreSet, view, respectIgnore, acceptSingleValueAsArray); }
/** * This converts a list of maps to objects. * I always forget that this exists. I need to remember. * * @param list the input list * @param <T> generics * @return a new list */ @Override public <T> List<T> convertListOfMapsToObjects(List<Map> list, Class<T> componentType) { List<Object> newList = new ArrayList<>( list.size() ); for ( Object obj : list ) { if ( obj instanceof Value) { obj = ( ( Value ) obj ).toValue(); } if ( obj instanceof Map ) { Map map = ( Map ) obj; if ( map instanceof ValueMapImpl) { newList.add( fromValueMap( ( Map<String, Value> ) map, componentType ) ); } else { newList.add( fromMap( map, componentType ) ); } } else { newList.add( Conversions.coerce(componentType, obj) ); } } return ( List<T> ) newList; }
/** * Processes an array of maps. * @param newInstance new instance we are injecting field into * @param field field we are injecting a value into */ private void processArrayOfMaps( Object newInstance, FieldAccess field, Map<String, Object>[] maps) { List<Map<String, Object>> list = Lists.list(maps); handleCollectionOfMaps( newInstance, field, list); }
handleCollectionOfMaps( newInstance, field, ( Collection<Map<String, Object>> ) collection ); return; handleCollectionOfValues( newInstance, field, ( Collection<Value> ) collection ); return; if (collection instanceof List) { try { Object value = fromList( (List) collection, field.getComponentClass()); field.setValue(newInstance, value); } catch (Exception ex) {
switch (value.type()) { case MAP: objValue = fromValueMap( ( Map<String, Value> ) objValue, clazz ); break; case LIST: objValue = fromList((List<Object>) objValue, clazz); break; case ARRAY_STRING: case ARRAY_OBJECT: handleCollectionOfValues( newInstance, field, ( Collection<Value> ) objValue );
field.setObject(newInstance, fromList((List) acollectionOfValues, field.type())); return; Object oValue = value.toValue(); if ( oValue instanceof Map ) { newCollection.add( fromValueMap( ( Map ) oValue, componentClass ) ); o = value.toValue(); if (o instanceof List) { o = fromList( (List)o, componentClass); if (componentClass.isInstance( o )) { Array.set(array, index, o); o = fromMap((Map) o, componentClass); if (componentClass.isInstance( o )) { Array.set(array, index, o);
Class<?> clazz = field.type(); if ( !clazz.isInterface() && !Typ.isAbstract( clazz ) ) { objectValue = fromValueMap( ( Map<String, Value> ) objectValue, field.type() ); } else { String className = (( Map<String, Value> ) objectValue) Class<?> cls = Reflection.loadClass( className ); objectValue = fromValueMap( ( Map<String, Value> ) objectValue, cls ); handleCollectionOfValues( newInstance, field, ( Collection<Value> ) objectValue ); } else {
fromValueMapHandleValueCase( newInstance, field, ( Value ) value ); } else { fromMapHandleNonValueCase( newInstance, field, value );
value = fromList((List) value, valueType); value = fromMap((Map) value, valueType); key = fromList((List) key, keyType); key = fromMap((Map) key, keyType); item = fromMap( ( Map<String, Object> ) item, parameterClass ); convertedArgumentList.set( index, item ); return true; value = fromList(listItem, parameterClass ); item = fromMap( (Map<String, Object>) item, Reflection.loadClass(className)); convertedArgumentList.set(index, item); return true; o = fromList( fromList, componentType ); newList.add( o ); o = fromList(fromList, componentType); o = fromMap( fromMap, componentType ); newList.add( o ); convertedArgumentList.set(index, fromList((List<Object>) item, parameterClass)); return true; case MAP:
/** * fromMap converts a map into a Java object. * This version will see if there is a class parameter in the map, and dies if there is not. * @param map map to create the object from. * @return new object */ @Override public Object fromMap(Map<String, Object> map) { String clazz = (String) map.get( "class" ); Class cls = Reflection.loadClass( clazz ); return fromMap(map, cls); }
/** * Creates an object from a value map. * * This does some special handling to take advantage of us using the value map so it avoids creating * a bunch of array objects and collections. Things you have to worry about when writing a * high-speed JSON serializer. * @return new object from value map */ @Override @SuppressWarnings("unchecked") public Object fromValueMap(final Map<String, Value> valueMap ) { try { String className = valueMap.get( "class" ).toString(); Class<?> cls = Reflection.loadClass( className ); return fromValueMap( valueMap, cls ); } catch ( Exception ex ) { return handle(Object.class, Str.sputs("fromValueMap", "map", valueMap, "fieldAccessor", fieldsAccessor), ex); } }
private <T> ConstructorAccess<T> lookupConstructorMeta(int size, List<Object> convertedArguments, ClassMeta<T> classMeta, ConstructorAccess<T> constructorToMatch, boolean[] flag, boolean loose) { /* Iterate through the constructors and see if one matches the arguments passed after coercion. */ loop: for ( ConstructorAccess constructor : classMeta.constructors() ) { /* Get the parameters on the constructor and see if the size matches what was passed. */ Class[] parameterTypes = constructor.parameterTypes(); if ( parameterTypes.length == size ) { /* Iterate through each parameter and see if it can be converted. */ for ( int index = 0; index < size; index++ ) { /* The match and convert does the bulk of the work. */ if ( !matchAndConvertArgs( convertedArguments, constructor, parameterTypes, index, flag, loose ) ) continue loop; } constructorToMatch = constructor; } } return constructorToMatch; }
constructorToMatch = lookupConstructorMeta( size, convertedArguments, classMeta, constructorToMatch, flag, false); constructorToMatch = lookupConstructorMeta( size, convertedArguments, classMeta, constructorToMatch, flag, true);
handleCollectionOfMaps( newInstance, field, ( Collection<Map<String, Object>> ) collection ); return; handleCollectionOfValues( newInstance, field, ( Collection<Value> ) collection ); return; if (collection instanceof List) { try { Object value = fromList( (List) collection, field.getComponentClass()); field.setValue(newInstance, value); } catch (Exception ex) {
switch (value.type()) { case MAP: objValue = fromValueMap( ( Map<String, Value> ) objValue, clazz ); break; case LIST: objValue = fromList((List<Object>) objValue, clazz); break; case ARRAY_STRING: case ARRAY_OBJECT: handleCollectionOfValues( newInstance, field, ( Collection<Value> ) objValue );
field.setObject(newInstance, fromList((List) acollectionOfValues, field.type())); return; Object oValue = value.toValue(); if ( oValue instanceof Map ) { newCollection.add( fromValueMap( ( Map ) oValue, componentClass ) ); o = value.toValue(); if (o instanceof List) { o = fromList( (List)o, componentClass); if (componentClass.isInstance( o )) { Array.set(array, index, o); o = fromMap((Map) o, componentClass); if (componentClass.isInstance( o )) { Array.set(array, index, o);
Class<?> clazz = field.type(); if ( !clazz.isInterface() && !Typ.isAbstract( clazz ) ) { objectValue = fromValueMap( ( Map<String, Value> ) objectValue, field.type() ); } else { String className = (( Map<String, Value> ) objectValue) Class<?> cls = Reflection.loadClass( className ); objectValue = fromValueMap( ( Map<String, Value> ) objectValue, cls ); handleCollectionOfValues( newInstance, field, ( Collection<Value> ) objectValue ); } else {
fromValueMapHandleValueCase( newInstance, field, ( Value ) value ); } else { fromMapHandleNonValueCase( newInstance, field, value );
value = fromList((List) value, valueType); value = fromMap((Map) value, valueType); key = fromList((List) key, keyType); key = fromMap((Map) key, keyType); item = fromMap( ( Map<String, Object> ) item, parameterClass ); convertedArgumentList.set( index, item ); return true; value = fromList(listItem, parameterClass ); item = fromMap( (Map<String, Object>) item, Reflection.loadClass(className)); convertedArgumentList.set(index, item); return true; o = fromList( fromList, componentType ); newList.add( o ); o = fromList(fromList, componentType); o = fromMap( fromMap, componentType ); newList.add( o ); convertedArgumentList.set(index, fromList((List<Object>) item, parameterClass)); return true; case MAP:
/** * fromMap converts a map into a Java object. * This version will see if there is a class parameter in the map, and dies if there is not. * @param map map to create the object from. * @return new object */ @Override public Object fromMap(Map<String, Object> map) { String clazz = (String) map.get( "class" ); Class cls = Reflection.loadClass( clazz ); return fromMap(map, cls); }