0

I'm wondering if anyone has seen this "issue" before and potential solutions on how to correct it?

I have a specific date format (MicrosoftDateFormat) that needs to be used when talking to an SAP Gateway service for DateTime fields.

This code:

string payload = JsonConvert.SerializeObject(myObject, new JsonSerializerSettings()
{
    DateFormatHandling = DateFormatHandling.MicrosoftDateFormat
});

serializes the DateTime fields into the required formats, e.g.:

"DocumentModifiedDate": "/Date(1533686400000)/"

But I then need to transform the JSON to a JToken object in order to pass it through the framework to my web service. When I transform to a JToken, the date is formatted back to the ATOM style approach:

var jot = Newtonsoft.Json.Linq.JToken.Parse(payload); 

Which results in:

"DocumentModifiedDate": "2018-08-08T00:00:00Z"

I've tried using the JToken.FromObject approach:

var jot = Newtonsoft.Json.Linq.JToken.FromObject(myObject, new JsonSerializer()
{
    DateFormatHandling = DateFormatHandling.MicrosoftDateFormat
});

But this doesn't seem to make use of the of serializer settings.

Hopefully someone has encountered this before and can assist.

If you can't assist here, then is there an easy way to perform that conversion in node.js because there is a chance to do it on the web server side before actually sending it off to SAP. I'm not wanting to add too much complexity to the web side of the equation so if I can do it on the app side, it'd be a lot more preferrable.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Skin
  • 9,085
  • 2
  • 13
  • 29
  • Related or duplicate: [Json.Net messes up timezones for DateTimeOffset when serializing](https://stackoverflow.com/q/44426207/3744182) plus [Creating JObject with JObject.FromObject ignores DateFormatString](https://stackoverflow.com/q/32770455/3744182). – dbc Aug 08 '18 at 05:23

1 Answers1

-1

I got the answer via a more roundabout method, which is probably correct, I'm not sure. bottom line is, it works.

Something like this ...

var payload = JsonConvert.SerializeObject(myObject, new JsonSerializerSettings()
{
    DateFormatHandling = DateFormatHandling.MicrosoftDateFormat
});

using (var sr = new StringReader(payload))
using (var jr = new JsonTextReader(sr) { DateParseHandling = DateParseHandling.None })
{
    var jot = JToken.ReadFrom(jr);
}
Skin
  • 9,085
  • 2
  • 13
  • 29