Example #1
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            try
            {
                switch (reader.TokenType)
                {
                case JsonToken.Null:
                    if (!TypeData.Get(objectType).IsNullable)
                    {
                        throw new JsonSerializationException($"Cannot convert null value to {objectType}.");
                    }
                    return(null);

                case JsonToken.String:
                    return(NonGenericEnums.Parse(objectType, (string)reader.Value, EnumFormat));

                case JsonToken.Integer:
                    return(NonGenericEnums.ToObject(objectType, reader.Value));

                default:
                    throw new JsonSerializationException($"Unexpected token {reader.TokenType} when parsing enum.");
                }
            }
            catch (Exception ex)
            {
                throw new JsonSerializationException($"Error converting value {reader.Value} to type '{objectType}'", ex);
            }
        }
Example #2
0
            protected override JsonConverter ResolveContractConverter(Type objectType)
            {
                var typeData = TypeData.Get(objectType);
                var typeInfo = typeData.TypeInfo;

                if (typeData.IsEnum || typeData.NonNullableValueTypeData?.IsEnum == true)
                {
                    var enumFormatAttribute = (typeData.NonNullableValueTypeData?.TypeInfo ?? typeInfo).GetCustomAttribute <EnumFormatAttribute>();
                    if (enumFormatAttribute != null)
                    {
                        return(new EnumJsonConverter(enumFormatAttribute.EnumFormat));
                    }
                    else
                    {
                        return(DefaultEnumConverter);
                    }
                }
                var jsonConverterAttribute = typeInfo.GetCustomAttribute <JsonConverterAttribute>(true);

                if (jsonConverterAttribute != null)
                {
                    var converterType = jsonConverterAttribute.ConverterType;
                    if (converterType == TypeData <PublicallySerializableConverter> .Type)
                    {
                        return(null);
                    }
                    if (typeInfo.IsGenericType && !typeInfo.IsGenericTypeDefinition && converterType.GetTypeInfo().IsGenericTypeDefinition)
                    {
                        return((JsonConverter)Activator.CreateInstance(jsonConverterAttribute.ConverterType.MakeGenericType(typeInfo.GenericTypeArguments), jsonConverterAttribute.ConverterParameters));
                    }
                }
                return(base.ResolveContractConverter(objectType));
            }
Example #3
0
            protected override JsonConverter ResolveContractConverter(Type objectType)
            {
                var typeData = TypeData.Get(objectType);
                var typeInfo = typeData.TypeInfo;

                if (typeInfo.IsGenericType && !typeInfo.IsGenericTypeDefinition)
                {
                    var genericTypeDefinition = typeInfo.GetGenericTypeDefinition();
                    if (genericTypeDefinition == TypeData.OpenDirtyListType || genericTypeDefinition == TypeData.OpenDirtyDictionaryType)
                    {
                        return(null);
                    }
                }
                return(base.ResolveContractConverter(objectType));
            }
Example #4
0
        public override bool CanConvert(Type objectType)
        {
            var data = TypeData.Get(objectType);

            return(data.IsEnum || data.NonNullableValueTypeData?.IsEnum == true);
        }