+ Reply to Thread
Results 1 to 6 of 6

Thread: serialize and deserialize of C++ Objects.

  1. #1
    bala_muthu is offline Junior Member
    Join Date
    June 4th, 2008
    Posts
    5
    Downloads
    0
    Uploads
    0

    Default serialize and deserialize of C++ Objects.

    Hi,

    I have a situation where i have to serialize and deserialize a C++ Objects. Can any one Suggest how that can be done using Platforn Symphony Developer Edition 4.0.

    Rgds
    Bala

  2. #2
    Young's Avatar
    Young is offline Junior Member
    Join Date
    March 5th, 2008
    Location
    Toronto, Canada
    Posts
    58
    Blog Entries
    1
    Downloads
    7
    Uploads
    0

    Default

    Hi Bala,

    You have to manually write each data member of the C++ object into the stream in onSerialze() method of your Symphony messsage class. Similarly, populate the data by reading each data member of the object in onDeserialize() method. Make sure, the order of writing and reading matches, to avoid runtime serialization/deserialization error.
    A couple of online documents can help you.
    1.C++ tutorial . Go to [Synchronous Symphony C++ client tutorial] -> [Review the sample code, Input and output: declare the message object ]
    2.Development guide . Go to [Messages] section.

  3. #3
    bala_muthu is offline Junior Member
    Join Date
    June 4th, 2008
    Posts
    5
    Downloads
    0
    Uploads
    0

    Default serialize and deserialize of C++ Objects.

    Hi,

    We have tried this approach for passing structure in our previous work. But when we had a chat with one of your team member, he was saying that readbytes & writebytes can be used. So that i have raised this question in this forum to confirm on that.

    We did some sample by having pointer object. on serialize it was successful and while deserialize its throws the error.

    Rgds
    Bala

  4. #4
    Ajith's Avatar
    Ajith is offline Symphony DE Moderator
    Join Date
    February 28th, 2008
    Location
    Markham, Ontario
    Posts
    104
    Blog Entries
    2
    Downloads
    10
    Uploads
    0

    Default Deserialize

    Quote Originally Posted by bala_muthu View Post
    Hi,

    We have tried this approach for passing structure in our previous work. But when we had a chat with one of your team member, he was saying that readbytes & writebytes can be used. So that i have raised this question in this forum to confirm on that.

    We did some sample by having pointer object. on serialize it was successful and while deserialize its throws the error.

    Rgds
    Bala
    Hi Bala,

    I didn't try this, but you should do something like this;

    MyClass x(param1, param2);

    writeBytes((void *)x, sizeof(MyClass));

    -----

    MyClass *x;

    // allocate memory for the class
    void * buffer = malloc(sizeof(MyClass));

    // initialize class data
    readBytes(buffer, sizeof(MyClass));

    // Construct the class using the placement operator - constuctor should not do any initialization
    x = new (buffer) MyClass();

    delete x;
    Last edited by Ajith; June 4th, 2008 at 04:58 PM.

  5. #5
    bala_muthu is offline Junior Member
    Join Date
    June 4th, 2008
    Posts
    5
    Downloads
    0
    Uploads
    0

    Default

    Hi,

    Please excuse me for the delay

    When we tried the approach that you have specified, at the time of type casting the object to (void *) it gives error saying cannot covert.

    So we tried some other way and we were able to pass the object

    Example:

    MyClass x(param1, param2);
    writeBytes(&x, sizeof(MyClass));
    ---------
    readBytes(&x, sizeof(MyClass));

    This solved the issue of passing the object. When we tested with objects that have variables, array …

    But the problem we face now is that our real time object has some vector array. When we went with the approach that we have used both serialization and de-serialization happens without error. But the vector data were not populated , so the service go too ideal state.

    Do u have any suggestion on this?

    Rgds
    Bala

  6. #6
    Ajith's Avatar
    Ajith is offline Symphony DE Moderator
    Join Date
    February 28th, 2008
    Location
    Markham, Ontario
    Posts
    104
    Blog Entries
    2
    Downloads
    10
    Uploads
    0

    Default Marshalling Vectors

    Hi Bala,

    Yes, this line should be: writeBytes((void *)&x, sizeof(MyClass));

    The approach I mentioned is only useful if your class does not have any pointers. If your class has pointers, like a vector, your best approach is to serialize the size first and then the values individually.

    Serialize :
    int size
    value1
    value2
    ...

    Deserialize
    size and then loop on size, taking out values.

    - Ajith

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts