Serialize classes using XML

T

titanandrews

Hi All,

There is a library for Java called XStream which will serialize
instances to XML. It's very easy to use and does not require changing
existing code. Basically, you just call 1 API method and bingo, you
have XML. I was wondering if there is something similar for C++. I have
already investigated the boost serialization library, but it requires
adding a serialize() function to your classes. Does anyone know of an
open source library that is similar to XStream, but for C++? Google did
not turn up anything.


many thanks!

Barry
 
V

Victor Bazarov

There is a library for Java called XStream which will serialize
instances to XML. It's very easy to use and does not require changing
existing code. Basically, you just call 1 API method and bingo, you
have XML. I was wondering if there is something similar for C++. [..]

Nope. C++ has no "reflection" API. Information about class members
and class layouts doesn't survive the compilation and linking unless
you specifically represent your objects by writing additional code to
generate your XML or make your objects out of XML.
 
P

Phlip

titanandrews said:
There is a library for Java called XStream which will serialize
instances to XML. It's very easy to use and does not require changing
existing code. Basically, you just call 1 API method and bingo, you
have XML. I was wondering if there is something similar for C++. I have
already investigated the boost serialization library, but it requires
adding a serialize() function to your classes. Does anyone know of an
open source library that is similar to XStream, but for C++? Google did
not turn up anything.

You are asking for "reflection", which C++ does not support.

Now ask yourself: What's the value of reflecting _everything_, when the only
part of your application that _should_ serialize is a tiny bit of data?

So, how could the incredible burden of adding serialize() to the few classes
that should persist be too much?
 
T

titanandrews

"Now ask yourself: What's the value of reflecting _everything_, when
the only
part of your application that _should_ serialize is a tiny bit of data?
"

Yep, you're right. The problem is I am dealing with legacy code, and I
didn't want to have to try and figure out exactly what needs to be
serialized and what doesn't. The class I need to serialize contains
many other classes, and these classes may be derived from other
classes, and on and on. So it could become a cumbersome task to
implement serialization. I need to investigate further. I just thought
that there could be any easier way to do this, but you are right, no
reflection in C++. That is what XStream relies on. Didn't think about
it. I guess I was thinking it was magic. ;)

Thanks for the replies!

-B
 
P

Phlip

titanandrews said:
Yep, you're right. The problem is I am dealing with legacy code, and I
didn't want to have to try and figure out exactly what needs to be
serialized and what doesn't. The class I need to serialize contains
many other classes, and these classes may be derived from other
classes, and on and on. So it could become a cumbersome task to
implement serialization. I need to investigate further.

Ask your customer, "When you turn the program off, and turn it on again,
what do you want to come back?"

The users want a specific experience. If hardcore serialization were
available, it would be the simplest way to get that experience. Without it,
learn more about what experience they need. It could be just the window
position and the current document.
 
P

Phlip

titanandrews said:
In this case, it's a diagram. So it's quite complicated.

Eeeek. You mean you inherited code that draws a complicated diagram in
memory, without saving it to a file?

Write a unit test that draws the simplest possible diagram in memory. Then
the test calls a Save("scratch.xml") method (with nothing inside it), and
checks that "scratch.xml" now contains the simplest possible representation
of that diagram.

Make the test pass by writing something inside Save(). Write the simplest
possible thing to get the test to pass.

Now write a unit test that loads a fixed XML file, such as
"load_test_1.xml", and draws the diagram in memory. The test will assert
that Load() generates this diagram in memory.

Now write a unit test draws the simplest possible diagram in memory, and
adds one little feature, like a single child item. The test will now assert
that "scratch.xml" contains a representation of this one little feature.
Make the test pass by upgrading Save().

Repeat for the Load method (copying the new "scratch.xml" to
"load_test_2.xml").

Keep going, for each feature, until you have just enough code in Save() and
Load() to save and load only the features in the diagram that are important.

This situation is much more accessible than barfing everything in the
diagram into an XML file. You won't know what you can change, or what's
important. Tests lock all that down.
 
T

titanandrews

No, it actually does save it to a binary file. So I suppose I could use
the saving mechanism currently in place to figure out everything that
needs serializing!

Great! Thanks for your input!

-Barry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top