Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Json Format for a TimeSpan that can be bound using Microsoft.Extensions.Configuration

In a project I need to configure some third party library via the Micorosoft.Extensions.Configuration.

The library gives an options class and I used the configurationSection.Bind(optionsClassInstance) method to bind the values.

It works well except the nested TimeSpan value. I can't figure out what the json structure of a timespan is so it could be bound.

There are no errors. The values from the json are simply not bound.

So far I just used "timespan": { "Days": 0, "Hours": 1, "Minutes": 0 }


Thanks to the answer I tested successfully the given values with the given results:

1.02:03:04.567 = 1 day, 2 hours, 3 minutes, 4 seconds, 567 milliseconds

1.02:03:04 = 1 day, 2 hours, 3 minutes, 4 seconds, 0 milliseconds

02:03:04 = 0 days, 2 hours, 3 minutes, 4 seconds, 0 milliseconds

03:04 = 0 days, 3 hours, 4 minutes, 0 seconds, 0 milliseconds

04 = 4 days, 0 hours, 0 minutes, 0 seconds, 0 milliseconds

like image 562
monty Avatar asked May 03 '18 13:05

monty


People also ask

Which configuration providers are derived from json configuration?

The following configuration providers derive from FileConfigurationProvider : JSON configuration provider. XML configuration provider. INI configuration provider.

What is Appsettings json?

The appsettings. json file is generally used to store the application configuration settings such as database connection strings, any application scope global variables, and much other information.

Where is Appsettings json?

appsettings. json is one of the several ways, in which we can provide the configuration values to ASP.NET core application. You will find this file in the root folder of our project. We can also create environment-specific files like appsettings.


1 Answers

Timespan format in .net core is D.HH:mm:nn (so "1.02:03:04" is 1 day, 2 hours, 3 mins, 4 seconds).

javascript wont be able to read that (we use a custom JsonConverter for timespan objects for that reason), but .Net can.

{"timespan":"1.02:03:04"}
like image 170
bri Avatar answered Oct 06 '22 03:10

bri