64900

How to make JSON.NET deserialize to Microsoft Date Time?

Using JsonSerializerSettings, I am able to consume/serialize the Microsoft DateTime format, but DateTimes seem continue to deserialize using the ISO 8601 standard

JsonSerializerSettings = new JsonSerializerSettings { DateFormatHandling = DateFormatHandling.MicrosoftDateFormat }

I'm trying to write a custom JsonConverter to handle the deserialization but am having trouble with the escape characters.

writer.WriteValue ("\\/Date(" + x + ")\\/");

is writing as

"\\/Date(1446145200000)\\/"

anyone ever tackled this before? Been searching around wayy to much without finding a good solution

Answer1:

You shouldn't need a custom converter here. Using the DateFormatHandling = DateFormatHandling.MicrosoftDateFormat setting is the correct approach and works both for serialization and deserialization. Make sure you are passing the settings to the SerializeObject and DeserializeObject methods as appropriate. If you don't pass the settings, it will use the default format, which is ISO 8601.

using System; using Newtonsoft.Json; public class Program { public static void Main() { Foo foo = new Foo { Date = new DateTime(2015,10,29,19,0,0) }; JsonSerializerSettings settings = new JsonSerializerSettings { DateFormatHandling = DateFormatHandling.MicrosoftDateFormat, Formatting = Formatting.Indented }; string json = JsonConvert.SerializeObject(foo, settings); Console.WriteLine(json); foo = JsonConvert.DeserializeObject<Foo>(json, settings); Console.WriteLine(foo.Date.ToString()); } } class Foo { public DateTime Date { get; set; } }

Output:

{ "Date": "\/Date(1446145200000+0000)\/" } 10/29/2015 7:00:00 PM

Fiddle: https://dotnetfiddle.net/hnJIO4

Answer2:

It looks like your input date has been serialized as ticks. In your custom converter can you deserialize the JSON value and use it to initialize a DateTime object?

Recommend

  • Recursive calls to Firestore
  • Write a Google Apps Script to send e-mails to users who submitted a form 10 days ago and meet other
  • Angular 5 | ReactiveForm with ControlValueAccessor | onChange is not triggered
  • Android: Having Difficulty passing an Object from one activity to another using Parcel
  • Serialize a custom map with Jackson
  • How to create vertical TitledBorder in JPanel (javax swing)
  • CoreBlueTooth delegate didWriteValueForCharacteristic called with a value null
  • Spring batch error tolerance
  • How to send image as base64 string in JSON using HTTP POST in Android?
  • networkstream “cannot access a disposed object” when using newly created networkstream
  • Complicated COUNT query in MySQL
  • Fluent NHibernate automap list of strings with nvarchar(max)
  • Get UILabel out of UIButton
  • Certain Arabic text gets incorrectly shown while other Arabic text gets showed normally?
  • custom string delimiters stringtemplate-4
  • How to turn (A, B, C) into (AB, AC, BC) with Pig?
  • Shouldn't else be indented in the below code
  • Validate jQuery plugin, field not required
  • git add error : “fatal : malloc, out of memory”
  • Azure table store snapshot/backup capability
  • UWP/C# - Issue with AQS and USB Devices
  • Combining two different ActiveRecord collections into one
  • How to get current document uri in XSLT?
  • SharedPreferences or SQLite Database?
  • Stop Bash Script if Hive Fails
  • one Local Olampyad Questions on Informatic in 2011
  • Why querying a date BC is changed to AD in Java?
  • Row Count Is Returning the incorrect number using RaptureXML
  • Extracting HTML between tags
  • Illegal mix of collations for operation for date/time comparison
  • Different response to non-authenticated users and AJAX calls
  • C# - Serializing and deserializing static member
  • javaw.exe and eclipse startup problems
  • Finding past revisions of files in StarTeam w/ .NET SDK / C#
  • Incrementing object id automatically JS constructor (static method and variable)
  • When should I choose bucket sort over other sorting algorithms?
  • Release, debug version and Authorization Google?
  • Do create extension work in single-user mode in postgres?
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Hits per day in Google Big Query