Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Newtonsoft.Json customize date serialization

I am using Newtonsoft.Json for serializing my dates from C# to JSON. What I want to do is have the json serializer use the current culture for formatting dates into string.

Here is what I am getting by my Json serializer:

JsonConvert.SerializeObject(DateTime.Now);

result is:

"2016-07-08T17:10:17.6722753-05:00"

But what I prefer is:

"08/07/2016 17:10:57"

Because my current culture is brazil and I want my dates to be displayed the above way.

Is it possible to Globally (for any date that may be serialized) tell the json serializer in Newtonsoft.Json to use as if it is doing the date.ToString() (because ToString respects the culture in System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat and gives the right format accordingly)

like image 830
Helen Araya Avatar asked Jul 08 '16 22:07

Helen Araya


People also ask

How do I set date format in JSON?

JSON does not have a built-in type for date/time values. The general consensus is to store the date/time value as a string in ISO 8601 format.

How do I format a Microsoft JSON date C#?

You can use this to get a date from JSON: var date = eval(jsonDate. replace(/\/Date\((\d+)\)\//gi, "new Date($1)")); And then you can use a JavaScript Date Format script (1.2 KB when minified and gzipped) to display it as you want.

How is date represented in JSON?

To represent dates in JavaScript, JSON uses ISO 8601 string format to encode dates as a string. Dates are encoded as ISO 8601 strings and then treated just like a regular string when the JSON is serialized and deserialized.

What is Jsonconvert SerializeObject?

SerializeObject Method (Object, Type, JsonSerializerSettings) Serializes the specified object to a JSON string using a type, formatting and JsonSerializerSettings. Namespace: Newtonsoft.Json.


2 Answers

You'll want to set JsonSerializerSettings.DateFormatString to your desired format.

var jsonSettings = new JsonSerializerSettings();
jsonSettings.DateFormatString = "dd/MM/yyyy hh:mm:ss";

string json = JsonConvert.SerializeObject(someObject, jsonSettings);

After that, you can either pass the settings object in each time you use the serializer, or follow the steps in the answer referenced by dbc. Although, you don't mention where this is running (ASP.NET, desktop, UWP, etc), so how you set it globally may differ.

like image 106
Devin Goble Avatar answered Oct 22 '22 06:10

Devin Goble


Yes you can use a Converter in the JsonSerializer settings.

public class SpecialDateTimeConverter : DateTimeConverterBase
    {
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            throw new NotImplementedException();
        }

        public override void WriteJson(JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
        {
             writer.WriteValue(((DateTime)value).ToString("dd/MM/yyyy hh:mm:ss"));
        }
    }

    string convertedDateTime = JsonConvert.SerializeObject(DateTime.Now, Formatting.Indented, new SpecialDateTimeConverter());
like image 1
Lockdowne Avatar answered Oct 22 '22 06:10

Lockdowne