FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour HASH to File and File to HASH
Posts: 6983
Joined: Fri Oct 07, 2005 07:07 PM
HASH to File and File to HASH
Posted: Thu Nov 24, 2022 07:21 AM

Hello friends,

Is there a Harbour function to save a HASH to a file and then read it back in.

Best regards,

Otto

Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: HASH to File and File to HASH
Posted: Thu Nov 24, 2022 07:30 AM

Dear Otto,

good morning

You have to convert the hash into a JSON string using hb_jsonEncode( hTheHash ) --> cJson then you can save it to disk using hb_memoWrit()

to recover the hash from the file you use hb_memoRead() and then call to hb_jsonDecode( cJson, @hTheHash ), simply declare local hTheHash before

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 6983
Joined: Fri Oct 07, 2005 07:07 PM
Re: HASH to File and File to HASH
Posted: Thu Nov 24, 2022 07:32 AM
Dear Antonio,
now as I have written down my question, I remembered.
Thank you so much and have a nice day.
Best regards,
Otto
Code (fw): Select all Collapse
 local hPost
//memowrit( "c:\www\htdocs\booking_new1\room.txt", hb_jsonencode( hPairs ) )
 
hb_jsondecode( memoread( "c:\www\htdocs\booking_new1\room.txt" ), @hPost )
Posts: 6983
Joined: Fri Oct 07, 2005 07:07 PM
Re: HASH to File and File to HASH
Posted: Thu Nov 24, 2022 07:37 AM

Dear Antonio,

This is real time support from you.

Thank you very much.

There is no better forum and programming community than this.

Best regards,

Otto

Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: HASH to File and File to HASH
Posted: Thu Nov 24, 2022 08:07 AM
Dear Otto,

thank you

We all together make these forums as good as they are :-)
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: HASH to File and File to HASH
Posted: Thu Nov 24, 2022 08:44 AM
Otto wrote:Dear Antonio,
This is real time support from you.
Thank you very much.
There is no better forum and programming community than this.
I totally agree!
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: HASH to File and File to HASH
Posted: Sat Nov 26, 2022 12:51 PM
I prefer
Code (fw): Select all Collapse
HB_MEMOWRIT( cFile, HB_ValToExp( hHash ) )
// Reading back
hHash := &( MEMOREAD( cFile ) )
There is one problem with HASH --> JSON --> HASH.
Any date values in the original hash are converted to character stings "yyyymmdd". Unless we scan the hash and convert all such values back to date type, the hash can not be used in date calculations.

Please try this simple test to prove the point.
Code (fw): Select all Collapse
#include "fivewin.ch"

function Main()

   local hHash1, hHash2, cJson, cHash

   SET DATE GERMAN
   SET CENTURY ON

   hHash1 := { "date" => Date() }
   cJson  := hb_jsonEncode( hHash1 )
   hb_jsonDecode( cJson, @hHash2 )
   ? hHash1[ "date" ], ValType( hHash1[ "date" ] ), ;
     hHash2[ "date" ], ValType( hHash2[ "date" ] )

   hHash1   := { "date" => Date() }
   cHash    := hb_ValToExp( hHash1 )
   hHash2   := &cHash
   ? hHash1[ "date" ], ValType( hHash1[ "date" ] ), ;
     hHash2[ "date" ], ValType( hHash2[ "date" ] )

return nil
Regards



G. N. Rao.

Hyderabad, India

Continue the discussion