FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Json.Stringify()
Posts: 408
Joined: Sun Nov 06, 2005 03:55 PM
Json.Stringify()
Posted: Fri Dec 11, 2015 06:36 PM

How can this be performed in xHarbour, I need this to help send parameters to json methods.

Thanks,

Byron ...

Thanks,

Byron Hopp

Matrix Computer Services
Posts: 1335
Joined: Fri Jun 13, 2008 11:04 AM
Re: Json.Stringify()
Posted: Sat Dec 12, 2015 04:22 AM
I am not sure whether I understood your requirement correctly. As far as I know Json.Stringify() turns an object into a JSON text

Please try and see whether you are getting the output equivalent to Json.Stringify using Harbour's hb_jsonEncode()
A Sample code
Code (fw): Select all Collapse
#include "Fivewin.ch"
Function Main()
    aHash := hash()
    ahash["ItemName"] = "Apple"
    ahash["Quantity"] = 500

    cJson := hb_jsonEncode(ahash,.T.) // .T. adds Line Feed to the output string, .F. without Linefeed 
    MsgInfo(cJson)

    MsgInfo( hb_jsonEncode(.T.,.F.) )  //  Output true
   

Return NIL


Regards
Anser
Posts: 408
Joined: Sun Nov 06, 2005 03:55 PM
Re: Json.Stringify()
Posted: Sat Dec 12, 2015 08:03 PM

In your example:

aHash := hash()
ahash["ItemName"] = "Apple"
ahash["Quantity"] = 500

cJson := hb_jsonEncode(ahash,.T.) // .T. adds Line Feed to the output

Does cJson == (not sure about linefeeds)

{"ItemName": "Apple","Quantity": 500}

or

{
"ItemName": "Apple",
"Quantity": 500
}

If this is true then I could probably write my own JsonStringify(), and JsonParse() but it would need to include more complex hash arrays.

btw, I don't have hb_JsonEncode(), or hb_JsonDecode() available in my version of xHarbour. I Downloaded the newest demo but there is no reference of these functions in the documentation. I sent an e-mail to the software company representing xHarbour and asked if these functions are included in the newest version and they indicated that they don't answer technical questions, and referred me to this news group. I really feel that this is a pre-sales question and not the focus of this group, but I appreciate the assistance in this group as always.

hLogin := GetEmptyHash()
cLogin := ""
hLogin["email" ] := "bhopp@matrixcomputer.com"
hLogin["password"] := "TheWord"
cLogin := JsonStringify( hLogin )

cLogin is: {"email": "bhopp@matrixcomputer.com","password": "TheWord"}

Function JsonStringify( hJson )
Local nI := 0
Local cJson := '{'

For nI := 1 to Len( hJson )
    cJson += '"' + HGetKeyAt( hJson,nI ) + '": "' + HGetValueAt( hJson,nI) + '"' + IIF( nI < Len( hJson ),",","" )
Next

cJson += '}'

Return cJson

Thanks,

Byron ...

Thanks,

Byron Hopp

Matrix Computer Services
Posts: 1335
Joined: Fri Jun 13, 2008 11:04 AM
Re: Json.Stringify()
Posted: Mon Dec 14, 2015 09:35 AM
byron.hopp wrote:In your example:

aHash := hash()
ahash["ItemName"] = "Apple"
ahash["Quantity"] = 500

cJson := hb_jsonEncode(ahash,.T.) // .T. adds Line Feed to the output

Does cJson == (not sure about linefeeds)

{"ItemName": "Apple","Quantity": 500}

or

{
"ItemName": "Apple",
"Quantity": 500
}.


Code (fw): Select all Collapse
#include "Fivewin.ch"
Function Main()
    aHash := hash()
    ahash["ItemName"] = "Apple"
    ahash["Quantity"] = 500

    cJson := hb_jsonEncode(ahash,.T.)
    MsgInfo(cJson)
    /* Output with Parameter .T. ie hb_jsonEncode(ahash,.T.)
       {
         "ItemName": "Apple",
         "Quantity": 500
       }
    */
    cJson := hb_jsonEncode(ahash,.F.)
    MsgInfo(cJson)    
    /* Output with Parameter .F. ie hb_jsonEncode(ahash,.F.)

       {"ItemName": "Apple","Quantity": 500} 
    */    
Return NIL



byron.hopp wrote:If this is true then I could probably write my own JsonStringify(), and JsonParse() but it would need to include more complex hash arrays.

btw, I don't have hb_JsonEncode(), or hb_JsonDecode() available in my version of xHarbour. I Downloaded the newest demo but there is no reference of these functions in the documentation. I sent an e-mail to the software company representing xHarbour and asked if these functions are included in the newest version and they indicated that they don't answer technical questions, and referred me to this news group. I really feel that this is a pre-sales question and not the focus of this group, but I appreciate the assistance in this group as always.

hLogin := GetEmptyHash()
cLogin := ""
hLogin["email" ] := "bhopp@matrixcomputer.com"
hLogin["password"] := "TheWord"
cLogin := JsonStringify( hLogin )

cLogin is: {"email": "bhopp@matrixcomputer.com","password": "TheWord"}

Function JsonStringify( hJson )
Local nI := 0
Local cJson := '{'

For nI := 1 to Len( hJson )
cJson += '"' + HGetKeyAt( hJson,nI ) + '": "' + HGetValueAt( hJson,nI) + '"' + IIF( nI < Len( hJson ),",","" )
Next

cJson += '}'
Return cJson

Thanks,

Byron ...


I believe that our friend EMG has already replied to you in another post about the availability of the functions hb_jsonEncode() and hb_jsonDecode() in xHarbour. As per him they are part of the standard xHarbour RTL.
viewtopic.php?f=3&t=31710

Regards
Anser

Continue the discussion