Web Connection
wwJSONserializer and empty dates
Gravatar is a globally recognized avatar based on your email address. wwJSONserializer and empty dates
  Stein Goering
  All
  May 10, 2021 @ 01:55pm

It appears that if I convert a VFP record that contains empty date fields, the serializer simply uses 1-Jan-1970 as the default date:

"cobegdate": "1970-01-01T06:00:00Z"

This isn't an issue for now as I'm currently only working with business records that date back to the 1990's at the earliest, so if I see that 1970 date I know it's just a placeholder. But what if I need to deal with personal profiles where that day could be someone's actual birthdate. Is there a way to configure wwJSONserializer to use a different fall back date? (e.g. 1-Jan-1900)

--stein

Gravatar is a globally recognized avatar based on your email address. re: wwJSONserializer and empty dates
  Rick Strahl
  Stein Goering
  May 10, 2021 @ 06:29pm

The problem isn't anything in Web Connection, but JSON's standard (based on JavaScript). JavaScript's date type defaults to 1/1/1970 for the default 0 date. It's the basis for the number of ticks since that make up a time value in JavaScript. Ticks can be negative for dates before, but the default is this funky date.

You can serialize the following perfectly fine:

CLEAR
DO wwJsonSerializer

loSer = CREATEOBJECT("wwJsonSerializer")

loObj = CREATEOBJECT("EMPTY")
ADDPROPERTY(loObj, "timestamp", {^1900-01-01 12:00})
? loObj.TimeStamp

lcJson = loSer.Serialize(loObj)

* {"timestamp": "1900-01-01T20:00:00Z"}
? lcJson  

loObj2 = loSer.Deserialize(lcJson)
? loObj2.timestamp

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: wwJSONserializer and empty dates
  Stein Goering
  Rick Strahl
  May 11, 2021 @ 07:41pm

But to take advantage of that when working against VFP data, I'd need to somehow assign the 1900 value to all empty date fields, right? (That's essentially what we do when clients convert from VFP to SQL data...)

--stein

Gravatar is a globally recognized avatar based on your email address. re: wwJSONserializer and empty dates
  Rick Strahl
  Stein Goering
  May 11, 2021 @ 08:50pm

Huh? No. If you have empty dates they will serialize as using the default JS 1970 date. When the date is returned it won't be empty and then you might have to explicitly check for the the empty date and convert it back to {^} in FoxPro.

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: wwJSONserializer and empty dates
  Stein Goering
  Rick Strahl
  May 14, 2021 @ 10:00pm

But my issue is I don't want to default to the JS date since it's possible that 1-1-1970 could be a valid database entry. 1-1-1900, OTOH, would be pretty safe to map back and forth to empty VFP dates.

FWIW, I made a subclass of wwJsonSerializer which appears to accomplish what I want..

*************************************************************
DEFINE CLASS awJSONSerializer AS wwJSONSerializer
*************************************************************

*** Custom zero date flag s.g.
lZeroDate1900 = .F.

**************************************************
FUNCTION WriteDate(lvValue)
LOCAL loNow

IF VARTYPE(lvValue) = "D"
  lvValue = DTOT(lvValue)
ENDIF  
IF EMPTY(lvValue)
  lvValue = IIF(This.lZeroDate1900,{^1900-1-1 :},{^1970-1-1 :})
ENDIF

IF ISNULL(this.oBridge)
  THIS.oBridge = GetwwDotnetBridge()
ENDIF
this.cOutput = this.cOutput + THIS.oBridge.oDotnetBridge.ToJsonUtcDate(lvValue, this.AssumeUtcDates)

ENDFUNC
© 1996-2021