nested array -> serialized to string -> nested array

237 views
Skip to first unread message

Francesco Perillo

unread,
Nov 20, 2013, 5:21:01 AM11/20/13
to harbou...@googlegroups.com

I have a nested array I'd like to save in a memo field. The array contains char, numeric and date fields.. nested to 2/3 levels.

So my idea was to serialize it, store in a memo field and unserialize it. Is there a pair of functions to do this ?

Thanks
Francesco

Francesco Perillo

unread,
Nov 20, 2013, 5:28:48 AM11/20/13
to harbou...@googlegroups.com

json would also be ok, but as far as I remember, it doesn't store variable types...

Jayadev U

unread,
Nov 20, 2013, 7:10:07 AM11/20/13
to harbou...@googlegroups.com

Hi,

 

Please see the following example:

 

/***

* PrintArray(aR, cTitle)

* *

*  aR[]     - -Array to print

*  cTitle   - -The heading

*

*  Recursive routine to print an array

*/

 

FUNCTION PrintArray(aR, cTitle)

 

 

LOCAL i := 0, cHeading

cTitle := iif(cTitle # NIL,cTitle,"a")

  // Step through every array element. If the element is

  // itself an array, recurse to print the subarray,

  // just print the element name and subscript.

  AEval(aR, {|elem|  ;

         i++,  ;

         cHeading := cTitle + "[" + Ltrim(Str(i)) + "]", ;

              Iif(ValType(elem) = "A",        ;

                  PrintArray(elem, cHeading), ;

                  iif(valtype(elem) == "B",Qout(cHeading,"{||...}"),Qout(cHeading, elem))) ;

            })

 

RETURN NIL

--
--
You received this message because you are subscribed to the Google
Groups "Harbour Users" group.
Unsubscribe: harbour-user...@googlegroups.com
Web: http://groups.google.com/group/harbour-users
 
---
You received this message because you are subscribed to the Google Groups "Harbour Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to harbour-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Przemyslaw Czerpak

unread,
Nov 20, 2013, 8:22:35 AM11/20/13
to harbou...@googlegroups.com
On Wed, 20 Nov 2013, Francesco Perillo wrote:

Hi,

> I have a nested array I'd like to save in a memo field. The array contains
> char, numeric and date fields.. nested to 2/3 levels.

Harbour can store string, array, numeric, logical and date values in FPT/SMT
memo files directly without any external serialization.
Format is compatible with SIx3 and Flex (it's also used by few other xBase
compatible systems).
Objects are stored and later read as arrays.
Hash arrays, function symbols, pointer items and code blocks are unsupported
in this format.

> So my idea was to serialize it, store in a memo field and unserialize it.
> Is there a pair of functions to do this ?

Harbour has functions:
HB_SERIALIZE( <xData>, [ <lNumSizes> = .F. ], ;
[ <cCdpIn> = HVMCP ], [ <cCdpOut> = HVMCP ] ) -> cData
HB_DESERIALIZE( <cData>, ;
[ <cCdpIn> = HVMCP ], [ <cCdpOut> = HVMCP ] ) -> xData

They can serialize hash arrays, function symbols (warning: static function
references are restored as public ones).
Objects are stored in raw form as array of instance variables with class
name and then restored. It means that they should be restored by programs
using exactly the same class definition.
Pointer items items are stored in raw form as simple address so GC pointer
items lost their functionality after deserialization).
Codeblocks are not supported.
Complex items with cyclic references are detected and correctly serialized
and later restored with original structure.

There is also function:
HB_VALTOEXP( <xData>, [ <lRaw> = .F. ] ) -> <cData>
which converts <xData> value to expression accepted by macro compiler.
It supports all types except codeblock and pointer items. Cyclic references
are properly detected, serialized and then restored.
<lRaw> parameter controls how objects should be serialized: in raw form
as array of instance variables (like in HB_SERIALIZE()) or as set of pairs
with instance variable name and value. The second form allows to deserialize
objects in programs with modified class definition.

best regards,
Przemek

Francesco Perillo

unread,
Nov 20, 2013, 8:52:07 AM11/20/13
to harbou...@googlegroups.com

Thank you for your answer. In the meanwhile the nested array has been transformed into an array of hashes of which one item is an array of bidimensional arrays...

And the array is a member of an object....

I need to study which way to go, but I think that serializing is the best thing.

Reply all
Reply to author
Forward
Message has been deleted
0 new messages