Web Connection
Problem with date values in REST response.
Gravatar is a globally recognized avatar based on your email address. Problem with date values in REST response.
  Michele
  All
  Apr 20, 2022 @ 07:41am

My REST app makes a query and the result is this but when i receive data in json format on client side i have this result where date values are different from those in my cursor.... One day less.... Why ? Thanks

Gravatar is a globally recognized avatar based on your email address. re: Problem with date values in REST response.
  Michele
  Michele
  Apr 20, 2022 @ 08:40am

I found AssumeUtcDates property.... But if i want to change it i need to change directly in wwjsonserializer.prg ? or not ?

Gravatar is a globally recognized avatar based on your email address. re: Problem with date values in REST response.
  Tore Bleken
  Michele
  Apr 20, 2022 @ 08:52am

It's a property of the class, change it in the instantiated object.

loSer = CREATEOBJECT("wwJsonSerializer")
loSer.AssumeUtcDate = .T.
Gravatar is a globally recognized avatar based on your email address. re: Problem with date values in REST response.
  Rick Strahl
  Michele
  Apr 20, 2022 @ 01:21pm

You really should not mess with the date format in the JSON. By default the date will be in UTC format and will get adjusted when you deserialize. IOW, if you do two way serialization the date in and out will be the same.

DO wwJsonSerializer

ldDate = DATETIME()
? ldDate       && 04/20/2022 10:15:45 AM

loSer = CREATEOBJECT("wwJsonSerializer")
lcJson = loSer.Serialize(DATETIME())
? lcJson       && "2022-04-20T20:15:45Z"

ldDate2 = loSer.Deserialize(lcJson)
? ldDate2     && 04/20/2022 10:15:45 AM

ASSERT(ldDate2 == ldDate)

You don't want the date to be in local date format in the JSON. because then every client will have to explicitly know that the data is in some specific date format and has to be calculate the date offset if dates are deserialized in a different time zone.

Not recommended.

The only reason you should use the lAssumeUtcDates flag is to deserialize JSON that has dates that are not UTC in the first place (which should be rare).

+++ Rick ---

© 1996-2022