C Object Serialization

P: n/a
Hi,
This is how I do it:
Derive every class from an ISerialize class.
This ISerialize class has one member accepting an IArchive i.e Serialize(
IArchive& Archive ).
The ISerialize class has a virtual function that returns it name (overridden
in the derived classes to return the class name). The name is used to
deserialize the correct class.
The IArchive function knows whether it is open for reading or writing.
The IArchive class has two virtual members read( char *, long length ) and a
similar write().
The IArchive also contains non virtual functions for all regular types like
string long int (use templates etc to reduce these to a few functions).
In addition this IArchive can serialize/deserialize ISerialize derived
classes by first storing there name and then calling the class'es Serialize
function passing itself. On deserialization it first reads the class name
(it knows this is a ISerialize class and not another type like long int etc.
because you pass a pointer or reference to it.)
Then it creates the object (lookup 'object factory' this is pretty standard
C++ way of creating objects by name or id) and calls the serialize member of
the object passing itself.
The Serialize member of a ISerialize derived typically (piece of my code)
void MCursor::MCursorInfo::Serialize( MArchive& Archive )
{
Archive.Serialize ( Event );
Archive.Serialize ( OffsetX );
Archive.Serialize ( OffsetY );
Archive.Serialize ( Animation );
}
Note1 that since the archive knows whether it is reading or writing it can
choose between the read or write function.
Note2 Because you can overload the read and write function you can create
derived function to store to memory disks, sockets etc easily. You only have
to overload the very simplistic read and write function.
Note3 Because (de)/serialization is done with one function it always is in
sync (no possibility to make a mismatch between read and write).
When this works extend your archive to keep track of written pointers to
ISerailize objects (so it only creates one object when it there are several
pointers around to one object on deserialization).
and add some stuff to automatically save STL vectors/sets/maps etc of
ISerialize etc
It might take some time to setup but ones you have it, it really works like
a charm :-) and loads of fun to see how easy it works then. I use it to save
my 2D game engines internal state (savegame) and load the same state back in
memory (deserialization).
Things to study:
Object factories
Lookup microsofts way of serialization on MSDN ( I stole the duplicate
pointer idea from them :-) )
Make sure your are reasonably aquainted with templates
Regards, Ron AF Greve
http://www.InformationSuperHighway.eu
'William' <Wi*******@gmail.comwrote in message
news:11**********************@d57g2000hsg.googlegr oups.com..
I'm looking for an example that would show how to serialize a c++
object at it's simplest w/o using any other api's. I have a class that
I want to serialize and then pass to my obj-c class so I can send it
over the wire.
I'm just looking for how to serialize it, then pack it back up on the
other end.
Any help much appreciated.

Active1 year, 5 months ago
  • What is the object serialization? A serialized object represents the type of data stored in the object, its information. Object serialization is a process of reading or writing an entire object from a file. This helps in saving session state information by servlets, for sending parameters for Remote Method Invocation (RMI) calls.
  • I'm writing some code to serialize some data to send it over the network. Currently, I use this primitive procedure: create a void. buffer apply any byte ordering operations such as the hton famil.
  • The Microsoft.NET Framework includes powerful objects that can serialize any object to XML. The System.Xml.Serialization namespace provides this capability. Follow these steps to create a console application that creates an object, and then serializes its state to XML: In Visual C#, create a new Console Application project.
  • Serialization is the process of converting complex objects into stream of bytes for storage. Deserialization is its reverse process, that is unpacking stream of bytes to their original form. The namespace which is used to read and write files is System.IO. For Serialization we are going to look at the System.Runtime.Serialization namespace.
  • Aug 28, 2019  First, we create an object of the Tutorial class. We then assign the value of '1' to ID and '.net' to the name property. We then use the formatter class which is used to serialize or convert the object to a binary format. The data in the file in serialization is done.
  • A lot of framework will serialize and deserialize NSDictionary/NSArray into JSON. But I still have to do the work to convert NSDictionary into Objective-C objects. To make things more complex, my Object A can have reference to an NSArray/NSDictionary of Object Bs.

C# Serialization. In C#, serialization is the process of converting object into byte stream so that it can be saved to memory, file or database. The reverse process of serialization is called deserialization. Serialization is internally used in remote applications. C# SerializableAttribute.

I'm writing some code to serialize some data to send it over the network. Currently, I use this primitive procedure:

  1. create a void* buffer
  2. apply any byte ordering operations such as the hton family on the data I want to send over the network
  3. use memcpy to copy the memory into the buffer
  4. send the memory over the network

The problem is that with various data structures (which often contain void* data so you don't know whether you need to care about byte ordering) the code becomes really bloated with serialization code that's very specific to each data structure and can't be reused at all.

What are some good serialization techniques for C that make this easier / less ugly?

-

Note: I'm bound to a specific protocol so I cannot freely choose how to serialize my data.

Object Serialization Java

ryystryyst
5,03115 gold badges58 silver badges95 bronze badges
Object

4 Answers

For each data structure, have a serialize_X function (where X is the struct name) which takes a pointer to an X and a pointer to an opaque buffer structure and calls the appropriate serializing functions. You should supply some primitives such as serialize_int which write to the buffer and update the output index.The primitives will have to call something like reserve_space(N) where N is the number of bytes that are required before writing any data. reserve_space() will realloc the void* buffer to make it at least as big as it's current size plus N bytes.To make this possible, the buffer structure will need to contain a pointer to the actual data, the index to write the next byte to (output index) and the size that is allocated for the data.With this system, all of your serialize_X functions should be pretty straightforward, for example:

And the framework code will be something like:

From this, it should be pretty simple to implement all of the serialize_() functions you need.

EDIT:For example:

Object

EDIT:Also note that my code has some potential bugs. The size of the buffer array is stored in a size_t but the index is an int (I'm not sure if size_t is considered a reasonable type for an index). Also, there is no provision for error handling and no function to free the Buffer after you're done so you'll have to do this yourself. I was just giving a demonstration of the basic architecture that I would use.

jstanleyjstanley

I suggest using a library.

As I was not happy with the existing ones, I created the Binn library to make our lives easier.

Here is an example of using it:

Bernardo RamosBernardo Ramos

I would say definitely don't try to implement serialization yourself. It's been done a zillion times and you should use an existing solution. e.g. protobufs: https://github.com/protobuf-c/protobuf-c

dex-translator is designed to do the convert job. Pes 2017 jar file download. Mirrors:.dex2jar contains following compment. It reads the dex instruction to dex-ir format, after some optimize, convert to ASM format. dex-reader is designed to read the Dalvik Executable (.dex/.odex) format. It has a light weight API similar with ASM.

It also has the advantage of being compatible with many other programming languages.

Assaf LavieAssaf Lavie
47.1k31 gold badges130 silver badges188 bronze badges

It would help if we knew what the protocol constraints are, but in general your options are really pretty limited. If the data are such that you can make a union of a byte array sizeof(struct) for each struct it might simplify things, but from your description it sounds like you have a more essential problem: if you're transferring pointers (you mention void * data) then those points are very unlikely to be valid on the receiving machine. Why would the data happen to appear at the same place in memory?

Object C Online

Charlie MartinCharlie Martin
94.9k21 gold badges171 silver badges245 bronze badges

Why Serialize Objects

Not the answer you're looking for? Browse other questions tagged cserialization or ask your own question.