Json.Stringify()

Json.Stringify()

Postby byron.hopp » Fri Dec 11, 2015 6: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
byron.hopp
 
Posts: 382
Joined: Sun Nov 06, 2005 3:55 pm
Location: Southern California, USA

Re: Json.Stringify()

Postby anserkk » Sat Dec 12, 2015 4: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: Select all  Expand view
#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
User avatar
anserkk
 
Posts: 1332
Joined: Fri Jun 13, 2008 11:04 am
Location: Kochi, India

Re: Json.Stringify()

Postby byron.hopp » Sat Dec 12, 2015 8: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
byron.hopp
 
Posts: 382
Joined: Sun Nov 06, 2005 3:55 pm
Location: Southern California, USA

Re: Json.Stringify()

Postby anserkk » Mon Dec 14, 2015 9: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: Select all  Expand view
#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
User avatar
anserkk
 
Posts: 1332
Joined: Fri Jun 13, 2008 11:04 am
Location: Kochi, India


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 103 guests