Home Help Center

serialize data into Freeze database

asecoaseco Member
Hi all,

I have the following problem. I would like to insert data into a Freeze connection
(i.e. a Berkeley Database) using a map. The type of data I would like to insert is an object (an instance of a class). I don't know what would be the best way to do that. I would like to serialize the object (my instance) in some way. If I compile slice2cpp with the --stream option I get marshaling functions to serialized my data but over input and outputstreams, and my question is how could I use it to insert data in my map. Is it possible to use an inputstream over a communicator defined with Freeze connection? by the same way, is it possible to get an outputstream over a Freeze connection to get back the data?
In case I'm wrong which solution you recommend me? should I code my own serialized functions without using streams?

Thanks in advanced ;)

Comments

  • mesmes CaliforniaAdministrators, ZeroC Staff Mark SpruiellOrganization: ZeroC, Inc.Project: Ice Developer ZeroC Staff
    Hi,

    I'm not sure why you would need to serialize the object yourself, as that is what Freeze is supposed to do for you. For example, if you generate a Freeze map using slice2freeze and specify the value type as your Slice class type, then you can insert and retrieve the object instances without needing to perform any marshaling in your application.

    If I'm misunderstanding your question, please provide more details.

    Take care,
    - Mark
  • asecoaseco Member
    Firstly, thank you for your answer Mes.
    In reference to my question, I didn't have in mind this possibility. It would be the solution for me, but I find now another problem. How can I get slice2freeze to understand an own class as a right value type ? I think Slice2freeze just only recognise some defined types and no others. Am I wrong? Should I code the value type as a generic Object (which is supported) and casting it afterwards? I have also tried to compile my own class, insert it as value type and I get errors (not a valid type).

    Thanks again!
  • bernardbernard Jupiter, FLAdministrators, ZeroC Staff Bernard NormierOrganization: ZeroC, Inc.Project: Ice ZeroC Staff
    The values stored in a Freeze dictionary can be any Slice type. Freeze marshals and unmarshals automatically all the Slice-defined data members; other (non-Slice) data members are not stored.

    If you want to store objects that are not defined in Slice, then you could define a dictionary key -> sequence<byte> and marshal/unmarshal yourself your objects.

    Cheers,
    Bernard
  • asecoaseco Member
    Hi Bernard,

    I have not understood at all what you have said. Excuse me :)

    Some questions:
    1- A class is not a Slice-defined data member? What I want to store in the dictionary value it's just only an instance of a class.

    2- In the case I'm wrong, and a class is not a slice defined data member, I tried to use a dictionary with the following line, and I got an error:
    slice2freeze --dict MyMap,string,sequence<byte> MyMap
    It is what I understood from your post. And so, marshaling my object into the sequence of bytes. But, from the first answer to the post of Mes I guess it must be easier than this, I guess it has to be possible to store an instance from a class.

    Thanks again to all!
    and excuse my ignorance :)
  • bernardbernard Jupiter, FLAdministrators, ZeroC Staff Bernard NormierOrganization: ZeroC, Inc.Project: Ice ZeroC Staff
    Ok, could you show the definition of this class?
    If it is defined in Slice, then storing it in Freeze is straightforward.

    > slice2freeze --dict MyMap,string,sequence<byte> MyMap
    you have to give a name to your sequence<byte>, for example:

    slice2freeze --ice -I%ICE_HOME%/slice --dict MyMap,string,Ice::ByteSeq MyMap %ICE_HOME%/slice/Ice/BuiltinSequences.ice

    Cheers,
    Bernard
  • asecoaseco Member
    this is how my simple class in slice looks like: (test.ice)

    module Demo {
    class Example {
    string id;
    int value;
    };
    };

    I get test.cpp and test.h , after compiling it, I try:
    slice2freeze --dict MyMap,string,Example MyMap,
    slice2freeze --dict MyMap,string,Demo::Example MyMap or
    slice2freeze -I. --dict MyMap,string,Example MyMap
    and always "'Example' is not a valid type" is returned.

    Apart from this, I got successfully a map with the value as Ice::ByteSeq, but I would prefer to insert a simple instance of 'Example'.

    Thank you for your help Bernard!
  • mesmes CaliforniaAdministrators, ZeroC Staff Mark SpruiellOrganization: ZeroC, Inc.Project: Ice Developer ZeroC Staff
    You must supply the Slice definitions for the Demo::Example type, so this is the command you should use:

    slice2freeze --dict MyMap,string,Demo::Example MyMap test.ice

    - Mark
  • asecoaseco Member
    Thanks a lot to ZeroC staff! :)
Sign In or Register to comment.