hb_jsonencode / hb_jsondecode with date values

117 views
Skip to first unread message

Pere Cordonet

unread,
Sep 28, 2021, 9:07:29 AM9/28/21
to
Hello,
I have a problems with date values when i call hb_jsonencode and hb_jsondecode

After call hb_jsondecode date value is a number value.

Can anyone test this code?

Function Main()
Local cJson,xVar

SetMode( 24 , 80 )

SET DATE BRITISH
Set(_SET_DATEFORMAT,"DD/MM/YYYY")

cJson:= hb_jsonencode( {"DATE"=> Stod("20210401") } )

Hb_jsondecode( cJson , @xVar )
? xVar["DATE"]

Return

Thank you.

Mel Smith

unread,
Sep 28, 2021, 11:14:25 AM9/28/21
to
Hi Pereco:

When compiled with xHarbour under BCC 7.4 , the result is a numeric: 2459306 (Number of days since base date ???)
When compiled under Harbour under MInGW, the result is a Character string: "20210401"

-Mel Smith
Message has been deleted

Ella Stern

unread,
Sep 28, 2021, 4:04:19 PM9/28/21
to
In JavaScript and in JSON there is no "date" primitive data type - you need to convert your date value into string.

Pere Cordonet

unread,
Sep 29, 2021, 2:13:02 AM9/29/21
to
El dia dimarts, 28 de setembre de 2021 a les 22:04:19 UTC+2, Ella Stern va escriure:
Good morning,

I found the problem.
I'm compile with xhb.com.

In file "hbjson.c" at function "_hb_jsonEncode", i changed the order of " else if( HB_IS_DATE( pValue ) )" before of " else if( HB_IS_NUMERIC( pValue ) )" and work fine.

It's strange, but works.

Can you make this changes to repository?

Thank you
Pere.

Mel Smith

unread,
Sep 29, 2021, 11:22:44 AM9/29/21
to
Hi Pere Cordonet:
I confirm that the change you made to hbjson.c corrects the error-- so that the return is the character string "20210401" Good detective work !
However, I have no idea *why* it works ?? !!
Because in this particular section of the code, the Harbour version is identical to the original code in xHarbour. !
I don't understand how the (pValue) can be both NUMERIC *and* DATE at the same time.
What is happening here ???
-Mel Smith

Mel Smith

unread,
Sep 29, 2021, 3:15:58 PM9/29/21
to
Hi Luiz
Can you investigate this problem reported by Pere Cordonet ?
Thank you.
-Mel
Reply all
Reply to author
Forward
0 new messages