Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Json source doesn't seem to support null values #508

Closed
ghost opened this issue Sep 6, 2016 · 1 comment
Closed

Json source doesn't seem to support null values #508

ghost opened this issue Sep 6, 2016 · 1 comment
Assignees

Comments

@ghost
Copy link

ghost commented Sep 6, 2016

When trying to use a json configuration source with a null value

{"MyTest": null }

and we are using Options to bind that to a class with a property as int?. An error "Failed to convert '' to type 'System.Int32'." occurs.

It appears that https://github.com/aspnet/Configuration/blob/dev/src/Microsoft.Extensions.Configuration.Json/JsonConfigurationFileParser.cs#L98 converts to an empty string rather than null. When the Options framework comes along, it see's the empty string and tries to convert it, which it wouldn't if it was null as per the json string.

Should the json source have been interpreted that as a null rather than empty string?

 System.InvalidOperationException: Failed to convert '' to type 'System.Int32'. ---> System.Exception:  is not a valid value for Int32. ---> System.IndexOutOfRangeException: Index was outside the bounds of the array.
    at System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
    --- End of inner exception stack trace ---
    at System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
    at Microsoft.Extensions.Configuration.ConfigurationBinder.ConvertValue(Type type, String value)
    --- End of inner exception stack trace ---
    at Microsoft.Extensions.Configuration.ConfigurationBinder.ConvertValue(Type type, String value)
    at Microsoft.Extensions.Configuration.ConfigurationBinder.BindProperty(PropertyInfo property, Object instance, IConfiguration config)
    at Microsoft.Extensions.Configuration.ConfigurationBinder.BindNonScalar(IConfiguration configuration, Object instance)
    at Microsoft.Extensions.Configuration.ConfigurationBinder.BindInstance(Type type, Object instance, IConfiguration config)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants