2

I would like to create a (json) file manually (with python) and load it then with cereal into my c++ application.

Saving and loading using ceral works fine. However, the polymorphic_ids in the json file are not clear to me.

Here a more clear example: This is the object, which is generated by the cereal framework:

{
    "array1": [
        {
            "key": 0,
            "value": {
                "ptr_wrapper": {
                    "id": 2147483649, //!-- ID1
                    "data": {
                        ...some float fields...
                    }
                }
            }
        }, 
       {
        "key": 1,
        "value": {
            "ptr_wrapper": {
                "id": 2147483650, //!-- This is previous ID+1 and so on...
                "data": {
                    ... some float fields...
                    }
                }
            }
        }
    ],
    "array2": [
        {
            "key": 0,
            "value": {
                "polymorphic_id": 2147483649, //!-- this is the very first ID from array 1.
                "polymorphic_name": "my_struct_name",
                "ptr_wrapper": {
                    "id": 2147483651, //this ID1+N Elements from array1
                    "data": {
                        ... also some float stuff...
                    }
                }
            }
        }
    ]
}

As I observe the number generation, the very first ID is increased. The second array uses the first ID as its polymorphic ID and further increases the numbers.

So is there some logic why these numbers have been used? Is it save to just use them all the time or will these change when I run my c++ importer on another machine?

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
mojovski
  • 581
  • 7
  • 21

1 Answers1

2

The ids in ptr_wrappers are generated by cereal to track the pointers it has already serialized when saving or loading. They are 32 bit unsigned integers that start at 1 and are incremented for each new pointer saved.

The most significant bit is set to 1 if it is the first time the pointer is encountered so that cereal can avoid saving data more than once. If the data has already been saved, cereal will look up the previously generated number and use that instead. Note that this looked-up number will not have the MSB set to 1.

See the functions registerSharedPointer in InputArchive and OutputArchive for more information.

Polymorphic ids are generated in an identical fashion using their own counter, see the registerPolymorphicType functions in the same file.

In general I would not recommend trying to hand-generate cereal JSON that deals with things like polymorphism or shared pointers since incorrectly generating these numbers will likely crash your program. However, if you can ensure that they are generated exactly as cereal does, it should work.

Azoth
  • 1,652
  • 16
  • 24