-5

How I can serialize/flatten the following C++ structure into byte array or anything else without using any external library.

struct node                                                 
{
    string splitOn;                                         
    string label;                                           
    bool isLeaf;                                            
    vector<string> childrenValues;                          
    vector<node*> children;                                 
};
Kumar Roshan Mehta
  • 3,078
  • 2
  • 27
  • 50
  • 2
    Possible duplicate of [Is it possible to serialize and deserialize a class in C++?](https://stackoverflow.com/questions/234724/is-it-possible-to-serialize-and-deserialize-a-class-in-c) –  Mar 20 '18 at 09:39
  • Why do you post some unused `typedef`s? – mch Mar 20 '18 at 09:41
  • If you don't have a `` header your installation is incomplete and you need to reinstall your compiler and standard library to make it complete. If you instead mean some header file with an `.h` suffix, then your compiler and library is to old to be relevant (as no standard library header file have had that suffix since C++ was standardized 20 years ago). – Some programmer dude Mar 20 '18 at 09:42
  • 3
    You need to write the code yourself. – Jabberwocky Mar 20 '18 at 09:42
  • Good serialization, even for only a single structure like yours, is not trivial. Quite the opposite. Don't implement it yourself, unless you want to spend countless of days and hours on fixing it. Get a third-party library instead, and spend only a couple of hours on making it work. – Some programmer dude Mar 20 '18 at 10:16

2 Answers2

3

There is no such header as part of C++, .h names are deprecated and no longer used. Do you mean<sstream> or <iostream>? If you are not using serailization library, you have to write it yourself. E.g. define serialization operators. There is no language-defined serialization like in java and if you should account for version changes, you have to design a new or use an existing representation format.

Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42
  • So cast each member of the struct to char array? – Kumar Roshan Mehta Mar 20 '18 at 10:19
  • 1
    if those members are containers, you can't cast (see my comment above). The inner representation of non-standard-layout, non-trivial classes are implementation and compilation mode dependant, trying to store copy and reuse those representations leads to undefined behavior. You have to extract data from collections with changeable size (it stored separately in heap anyway, not inside class). – Swift - Friday Pie Mar 20 '18 at 10:40
-4

You can use memcpy to copy bit by bit information from your structure node into a character array frame like this:

struct node obj;
char frame[1024];
memcpy ( frame, &obj, sizeof(struct node) );

You can then send the frame serially bit by bit and recover the structure later like this:

memcpy ( &obj2, frame, sizeof(struct node) );

However, I have not tested this code. You may need to cast to a void * the first and second arguments.

Syed Waris
  • 1,056
  • 1
  • 11
  • 16
  • Will it work with struct containing vectors? – Kumar Roshan Mehta Mar 20 '18 at 10:03
  • 2
    that would not work and is an UB. memcpy is usable only on classes that are trivially constructable, copyable and destructable (almost a POD). Your class is not, because its fields aren't. From implementation side of view, this will never copy data stored in resources of fields – Swift - Friday Pie Mar 20 '18 at 10:03