Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JsonConvert.DeserializeObject to en-US culture

Tags:

json

c#

json.net

I added custom JsonSerializerSettings to ensure the date is always de-serialized to en-US culture.

JsonConvert.DeserializeObject<T>(responseString, new JsonSerializerSettings()
                                { Culture = new System.Globalization.CultureInfo("en-US") });

However, This does not work for Hijri Calendar and date is still de-serialized in ar culture calendar.

Why? What am I missing here?

Were T

public partial class T
    {
    --------------------
        public Nullable<System.DateTime> EffectiveStartDate { get; set; }
        public Nullable<System.DateTime> EffectiveEndDate { get; set; }
        public Nullable<System.DateTime> SourceDate { get; set; }
    -----------------
    }

Json response { ..... , "effectiveStartDate":"2018-01-02T00:00:00", .... }

After deserialize

15/04/39 12:00:00

Whereas my expected is

02/01/2018

like image 498
immirza Avatar asked Jan 26 '26 04:01

immirza


1 Answers

I think you have misunderstood how the code will work. The deserialisation process will go something like this:

  1. Create an instance of T (let's call it x).

  2. Start to read the properties from the JSON, and find matching properties in T. Convert the JSON properties into C# variables, and add them to the correct property in x.

  3. When we get to the effectiveStartDate property, it will deserialise the value into a DateTime object. In order to parse the string in the JSON, it references the en-US culture setting which you provided, in case the string shows the date in US format (e.g. mm/dd/yyyy). However, the date is in ISO format, which is unambiguous and easily readable by a computer without being told what to expect. So in fact the culture setting doesn't matter at all in this case.

  4. Store the parsed date in a DateTime object. DateTime does not use a specific format internally - it just stores the date as a number. Formats are only used for serialising dates to strings, or showing them to humans.

Deserialisation is now complete and you have a regular C# object to use freely within your code.

Presumably you then try to read the x.effectiveStartDate value somewhere in your code, or via your debugger (you haven't shown us exactly what you're doing to view the date). Inevitably, since you want to view it, C# outputs the number stored within the DateTime object as a human-readable date string. Note that the deserialisation process has completed, and you are now back in the context of your application. Because of this, you see the date formatting according to the ar culture and its associated calendar style, because that's your application's culture. The en-US culture only applied to the deserialisation process, which has already finished.

If you want to display your date in a different format than the application culture, then you need to tell the application that explicitly. You can set a format via the toString method of DateTime, for instance, and/or temporarily change your culture settings via your code.

like image 73
ADyson Avatar answered Jan 28 '26 18:01

ADyson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!