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 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)
            });
        }