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)); }
private static Type FindBaseClass(Type type) { while (type != null && type != typeof(object)) { JsonCustomConverterAttribute attr = (JsonCustomConverterAttribute)Attribute.GetCustomAttribute(type, typeof(JsonCustomConverterAttribute), false); if (attr != null && attr.ConverterType == typeof(TypedConverterFactory)) { return(type); } type = type.BaseType; } throw new Exception($"{nameof(TypedConverterFactory)} was not found somewhere in the inheritence chain of {type.Name}."); }
internal static Type GetConverterType(MemberInfo memberInfo, out object[] converterParameters) { converterParameters = null; JsonCustomConverterAttribute att = memberInfo.GetCustomAttribute <JsonCustomConverterAttribute>(true); if (att == null) { return(null); } if (att.ConverterType == null) { throw new Exception($"{nameof(ConverterType)} must not be null in {nameof(JsonCustomConverterAttribute)} for member {memberInfo.Name} of type {memberInfo.ReflectedType}."); } converterParameters = att.GetParameters(); return(att.ConverterType); }
internal static Type GetConverterType(Type type, out object[] converterParameters) { converterParameters = null; JsonCustomConverterAttribute att = type.GetCustomAttribute <JsonCustomConverterAttribute>(true); if (att == null) { return(null); } if (att.ConverterType == null) { throw new Exception($"{nameof(ConverterType)} must not be null in {nameof(JsonCustomConverterAttribute)} for type {type.Name}."); } converterParameters = att.GetParameters(); return(att.ConverterType); }
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) }); }