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
I found AssumeUtcDates property.... But if i want to change it i need to change directly in wwjsonserializer.prg ? or not ?
It's a property of the class, change it in the instantiated object.
loSer = CREATEOBJECT("wwJsonSerializer")
loSer.AssumeUtcDate = .T.
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 ---
I have similar problem when receiving data in json....i send this value in my json "p_dtin": "2022-01-01T23:00:00"
and when i look at loJson.p_dtin value in rest service i have {02-01-2022}
Exactly one hour after
Sounds that's the difference of your time zone. Date is in UTC and will convert back to local time when you deserialize.
+++ Rick ---
Ok, but how can i have the correct date in my test? I need to store it with correct value, not in UTC...
I don't understand your question. If you receive the data in JavaScript it'll be adjusted to local time by the JSON Serialization. If you use it in FoxPro wwJsonSerializer
will give you the local date.
You shouldn't look at the date in JSON because that's not what you'll get when the data deserializes (unless you're on Greenwich Mean Time)
+++ Rick ---
I send a date with a json, for example 12-31-2022 23:30 In my rest i receive 01-01-2023 and i store it my table... If i access the table from a BI directly without rest i have an uncorrect date. Not the same i sent at the beginning....
If that's the case your JSON client is not doing the right thing in deserializing the data.
Here's my point, to demonstrate, which is two-way conversion:
DO wwJsonSerializer
ltDate = DATETIME()
? ltDate
loSer = CREATEOBJECT("wwJsonSerializer")
lcJson = loSer.Serialize(ltDate)
? lcJson
dtDate = loSer.DeSerialize(lcJson)
? dtDate
Here's the output both in FoxPro, and also in .NET (to demonstrate that this is universal):
+++ Rick ---