Ejemplo n.º 1
0
        public IConverter Create(Type type, object[] converterParameters)
        {
            // if a custom converter has been specified using the CustomConverterAttribute,
            //  we use this converter instead.
            Type customConverterType = JsonCustomConverterAttribute.GetConverterType(type, out object[] parametersAtType);

            if (customConverterType == null)
            {
                throw new Exception($"The {nameof(CustomConverterFactory)} cannot be used for type {type}, because the {nameof(JsonCustomConverterAttribute)} is not defined for this type.");
            }

            return(ConverterFactoryHelper.CreateConverter(customConverterType, type, converterParameters ?? parametersAtType));
        }
Ejemplo n.º 2
0
        private static IConverter CreateConverter(Type type, object[] converterParameters)
        {
            int factoryStartIndex = FACTORIES.Count - 1;

            // walk the factories from last to first
            for (int i = factoryStartIndex; i >= 0; i--)
            {
                if (FACTORIES[i].CanConvert(type))
                {
                    return(ConverterFactoryHelper.CreateConverter(FACTORIES[i], type, converterParameters));
                }
            }

            throw new Exception($"Unsupported model type '{type}'.");
        }
Ejemplo n.º 3
0
        public IConverter Create(Type type, object[] converterParameters)
        {
            if (!type.IsArray)
            {
                throw new ArgumentException($"Type '{type}' was expected to be an array.");
            }

            Type       elementType          = type.GetElementType();
            Type       elementConverterType = ConverterFactoryHelper.GetConverterParameter <Type>(typeof(ArrayConverterFactory), converterParameters, 0, 0, 1);
            IConverter elementConverter     = ConverterFactoryHelper.CreateConverter(elementConverterType, elementType, null);

            Type listConverterType = typeof(ArrayConverter <>).MakeGenericType(elementType);

            return((IConverter)Activator.CreateInstance(listConverterType, elementConverter));
        }
Ejemplo n.º 4
0
        public IConverter Create(Type type, object[] converterParameters)
        {
            Type genericTypeDef = type.GetGenericTypeDefinition();

            if (genericTypeDef != typeof(List <>) && genericTypeDef != typeof(IList <>))
            {
                throw new ArgumentException($"Type '{type}' was expected to be a generic List<> or IList<>.");
            }

            Type       elementType          = type.GetGenericArguments()[0];
            Type       elementConverterType = ConverterFactoryHelper.GetConverterParameter <Type>(typeof(ListConverterFactory), converterParameters, 0, 0, 1);
            IConverter elementConverter     = ConverterFactoryHelper.CreateConverter(elementConverterType, elementType, null);

            Type listConverterType = typeof(ListConverter <>).MakeGenericType(elementType);

            return((IConverter)Activator.CreateInstance(listConverterType, new object[] { elementConverter }));
        }
Ejemplo n.º 5
0
        private void AddProperty(MemberInfo memberInfo, Func <object, object> getter, Action <object, object> setter, Type propertyType)
        {
            // check, if a custom converter has been assigned
            Type       converterType = JsonCustomConverterAttribute.GetConverterType(memberInfo, out object[] converterParameters);
            IConverter converter     = ConverterFactoryHelper.CreateConverter(converterType, propertyType, converterParameters);

            // check, if a custom property name should be used
            JsonPropertyAttribute jsonProperty = memberInfo.GetCustomAttribute <JsonPropertyAttribute>(true);

            mProps.Add(memberInfo.Name, new PropInfo
            {
                Name                 = jsonProperty?.Name ?? memberInfo.Name,
                Setter               = setter,
                Getter               = getter,
                Converter            = converter,
                DefaultValue         = GetDefaultValue(propertyType),
                SuppressDefaultValue = SuppressDefaultValue(memberInfo),
                EmitNullValue        = EmitNullValue(memberInfo)
            });
        }
Ejemplo n.º 6
0
        public IConverter Create(Type type, object[] parameters)
        {
            Type genericTypeDef = type.GetGenericTypeDefinition();

            if (genericTypeDef != typeof(Dictionary <,>) && genericTypeDef != typeof(IDictionary <,>))
            {
                throw new ArgumentException($"Type '{type}' was expected to be a generic Dictionary<,> or IDictionary<,>.");
            }

            Type[] genericArguments = type.GetGenericArguments();
            if (genericArguments[0] != typeof(string))
            {
                throw new ArgumentException($"Type '{type}' was expected to have string keys.");
            }

            Type elementType = genericArguments[1];

            Type elementConverterType = ConverterFactoryHelper.GetConverterParameter <Type>(typeof(DictionaryConverterFactory), parameters, 0, 0, 1);
            Func <IConverter> elementConverterFactory = () => ConverterFactoryHelper.CreateConverter(elementConverterType, elementType, null);

            Type dictConverterType = typeof(DictionaryConverter <>).MakeGenericType(elementType);

            return((IConverter)Activator.CreateInstance(dictConverterType, elementConverterFactory));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Creates a converter for the given type. This converter factory
        /// takes one optional parameter for the name of the type property.
        /// This defaults to 'Type'.
        /// </summary>
        /// <param name="type">The type for which to create the converter.</param>
        /// <param name="parameters">An optional set of converter-specific parameters
        /// for the new converter.</param>
        /// <returns>An instance of <see cref="IConverter"/> which is capable
        /// of converting objects of the given type.</returns>
        public IConverter Create(Type type, object[] parameters)
        {
            string typeProperty = ConverterFactoryHelper.GetConverterParameter <string>(typeof(TypedConverterFactory), parameters, 0, 0, 1);

            return(new TypedConverter(type, typeProperty ?? "Type"));
        }