@Override protected Mapper initialValue() { /** * MapperComplex(FieldAccessMode fieldAccessType, boolean useAnnotations, boolean caseInsensitiveFields, Set<String> ignoreSet, String view, boolean respectIgnore, boolean acceptSingleValueAsArray) { fieldsAccessor = FieldAccessMode.create( fieldAccessType, useAnnotations, caseInsensitiveFields ); this.ignoreSet = ignoreSet; this.view = view; this.respectIgnore = respectIgnore; this.acceptSingleValueAsArray = acceptSingleValueAsArray; this.outputType = true; } */ return new MapperComplex(false, FieldAccessMode.PROPERTY_THEN_FIELD, true, false, Collections.emptySet(), null, true, true); } };
/** * This converts a list of maps to objects. * I always forget that this exists. I need to remember. * * @param respectIgnore honor @JsonIgnore, transients, etc. of the field * @param view the view of the object which can ignore certain fields given certain views * @param fieldsAccessor how we are going to access the fields (by field, by property, combination) * @param componentType The component type of the created list * @param list the input list * @param ignoreProperties properties to ignore * @param <T> generics * @return a new list */ public static <T> List<T> convertListOfMapsToObjects( boolean respectIgnore, String view, FieldsAccessor fieldsAccessor, Class<T> componentType, List<Map> list, Set<String> ignoreProperties) { return new MapperComplex(fieldsAccessor, ignoreProperties,view, respectIgnore).convertListOfMapsToObjects(list, componentType); }
/** Convert an item from a list into a class using the classes constructor. * * REFACTOR: Can't this just be from collection? * REFACTOR * * @param fieldsAccessor how we are going to access the fields (by field, by property, combination) * @param argList list if arguments * @param clazz the type of the object we are creating * @param <T> generics * @return the new object that we just created. */ public static <T> T fromList( FieldsAccessor fieldsAccessor, List<?> argList, Class<T> clazz ) { return new MapperComplex(fieldsAccessor, null, null, false).fromList(argList, clazz); }
/** * This could be refactored to use core.TypeType class and it would run faster. * Converts an object into a map * @param object the object that we want to convert * @param ignore the map * @return map map representation of the object */ public static Map<String, Object> toMap( final Object object, Set<String> ignore ) { return new MapperComplex(ignore).toMap(object); }
/** * fromMap converts a map into a java object. * @param map map to create the object from. * @param clazz the new instance type * @param excludeProperties the properties to exclude * @param <T> generic type capture * @return the new object */ @SuppressWarnings( "unchecked" ) public static <T> T fromMap( Map<String, Object> map, Class<T> clazz, String... excludeProperties ) { Set<String> ignoreProps = excludeProperties.length > 0 ? Sets.set(excludeProperties) : null; return new MapperComplex(FieldAccessMode.FIELD_THEN_PROPERTY.create( false ), ignoreProps, null, true).fromMap(map, clazz); }
switch (value.type()) { case MAP: objValue = fromValueMap( ( Map<String, Value> ) objValue, clazz ); break; case LIST: objValue = fromList((List<Object>) objValue, clazz); break; handleCollectionOfValues( newInstance, field, ( Collection<Value> ) objValue );
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) {
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);
/** * 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; }
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); } }
/** * This could be refactored to use core.TypeType class and it would run faster. * Converts an object into a map * @param object the object that we want to convert * @param ignore the map * @return map map representation of the object */ public static Map<String, Object> toMap( final Object object, Set<String> ignore ) { return new MapperComplex(ignore).toMap(object); }
/** * fromMap converts a map into a java object. * @param map map to create the object from. * @param clazz the new instance type * @param excludeProperties the properties to exclude * @param <T> generic type capture * @return the new object */ @SuppressWarnings( "unchecked" ) public static <T> T fromMap( Map<String, Object> map, Class<T> clazz, String... excludeProperties ) { Set<String> ignoreProps = excludeProperties.length > 0 ? Sets.set(excludeProperties) : null; return new MapperComplex(FieldAccessMode.FIELD_THEN_PROPERTY.create( false ), ignoreProps, null, true).fromMap(map, clazz); }
switch (value.type()) { case MAP: objValue = fromValueMap( ( Map<String, Value> ) objValue, clazz ); break; case LIST: objValue = fromList((List<Object>) objValue, clazz); break; handleCollectionOfValues( newInstance, field, ( Collection<Value> ) objValue );
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) {
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);
/** * 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; }