void Object::Serialize(Stream& stream)
{
stream.Serialize(m_field1);
stream.Serialize(m_field2);
// ...
m_complicatedSubObjectField.Serialize(stream); // Yes the order is reversed here. Think about it.
// ...
}
void Object::Serialize(Stream& stream)
{
stream.Serialize(m_arraySize);
if (stream.Direction() == StreamRead) {
m_array = new SubObject[m_arraySize];
}
stream.Serialize(m_array, m_arraySize*sizeof(m_array[0]));
}
template <T> void Serialize(T& data) { Serialize(data, sizeof(data)); }
int data;
BigLargeStruct moreData;
stream.Serialize(data); // => stream.Serialize(data, sizeof(data));
stream.Serailize(moreData); // => stream.Serialize(moreData, sizeof(moreData));
<OutpostHD_SaveGame version="0.01">
<properties planetname="Planet Name -- DUMMY" sitemap="maps/mars_04" sitemapname="Sitemap Name -- DUMMY" tset="tsets/mars.png" diggingdepth="4" />
<view_parameters currentdepth="0" viewlocation_x="183" viewlocation_y="30" />
<mines>
<mine x="33" y="121" age="0" depth="0" active="0" exhausted="0" yield="1" />
<mine x="126" y="76" age="0" depth="0" active="0" exhausted="0" yield="0" />
<mine x="273" y="124" age="0" depth="0" active="0" exhausted="0" yield="1" />
<mine x="10" y="55" age="0" depth="0" active="0" exhausted="0" yield="1" />
<mine x="255" y="49" age="0" depth="0" active="0" exhausted="0" yield="0" />
<mine x="113" y="21" age="0" depth="0" active="0" exhausted="0" yield="1" />
<mine x="283" y="101" age="0" depth="0" active="0" exhausted="0" yield="1" />
<mine x="270" y="103" age="0" depth="0" active="0" exhausted="0" yield="2" />
<mine x="123" y="32" age="0" depth="0" active="0" exhausted="0" yield="1" />
<mine x="76" y="60" age="0" depth="0" active="0" exhausted="0" yield="1" />
<mine x="105" y="36" age="0" depth="0" active="0" exhausted="0" yield="1" />
<mine x="79" y="48" age="0" depth="0" active="0" exhausted="0" yield="1" />
<mine x="260" y="95" age="0" depth="0" active="0" exhausted="0" yield="2" />
<mine x="81" y="110" age="0" depth="0" active="0" exhausted="0" yield="1" />
</mines>
<tiles>
<tile x="184" y="36" depth="0" index="0" />
<tile x="186" y="34" depth="0" index="0" />
<tile x="189" y="36" depth="0" index="0" />
<tile x="189" y="37" depth="0" index="0" />
<tile x="189" y="38" depth="0" index="0" />
</tiles>
<structures>
<structure x="185" y="35" depth="0" id="64185312" age="48" state="1" type="2" direction="1" />
<structure x="185" y="36" depth="0" id="64412400" age="0" state="0" type="13" direction="1" />
<structure x="186" y="37" depth="0" id="111595704" age="0" state="0" type="13" direction="1" />
<structure x="187" y="37" depth="0" id="111596032" age="48" state="1" type="11" direction="1">
<production common_metals_ore="196" common_minerals_ore="30" rare_metals_ore="8" rare_minerals_ore="16" common_metals="0" common_mminerals="0" rare_metals="0" rare_mminerals="0" energy="0" food="0" />
</structure>
<structure x="257" y="8" depth="0" id="111596360" age="35" state="2" type="8" direction="1">
<production common_metals_ore="0" common_minerals_ore="12" rare_metals_ore="8" rare_minerals_ore="8" common_metals="0" common_mminerals="0" rare_metals="0" rare_mminerals="0" energy="0" food="0" />
<storage common_metals_ore="130" common_minerals_ore="154" rare_metals_ore="96" rare_minerals_ore="120" common_metals="0" common_mminerals="0" rare_metals="0" rare_mminerals="0" energy="0" food="0" />
</structure>
<structure x="187" y="35" depth="0" id="111599096" age="48" state="1" type="0" direction="1" />
<structure x="186" y="36" depth="0" id="113860920" age="48" state="1" type="6" direction="1" />
<structure x="188" y="36" depth="0" id="114327688" age="25" state="1" type="12" direction="1" />
<structure x="186" y="35" depth="0" id="114353432" age="0" state="0" type="13" direction="1" />
<structure x="242" y="53" depth="0" id="114428832" age="41" state="1" type="8" direction="1">
<storage common_metals_ore="0" common_minerals_ore="0" rare_metals_ore="0" rare_minerals_ore="8" common_metals="0" common_mminerals="0" rare_metals="0" rare_mminerals="0" energy="0" food="0" />
</structure>
<structure x="187" y="36" depth="0" id="114429208" age="0" state="0" type="13" direction="1" />
</structures>
<factories>
<factory x="185" y="37" depth="0" id="114429536" age="48" state="1" type="3" direction="1" production_completed="0" production_type="0" />
</factories>
</OutpostHD_SaveGame>
enum StructureType
{
STRUCTURE_AGRIDOME,
STRUCTURE_MINE_FACILITY,
STRUCTURE_WHATEVER
};
enum StructureType
{
STRUCTURE_AGRIDOME,
STRUCTURE_ATMOSPHERE, // new enumeration
STRUCTURE_MINE_FACILITY,
STRUCTURE_WHATEVER
};
class BinaryStreamReader : public Serializer
class BinaryStreamWriter : public Serializer
class XmlStreamReader : public Serializer
class XmlStreamWriter : public Serializer
class JsonStreamReader : public Serializer
class JsonStreamWriter : public Serializer
Original XML file | 58 893 octets | 100,000% |
Zip compressed | 5 138 octets | 8,723% |
gZip compressed | 5 015 octets | 8,515% |
7Zip compressed | 4 063 octets | 6,899% |
2) I don't have to worry about portability issues. It's all just plain-text.OMG, don't even get me started on that one. What's the newline convention on your OS? Carriage Return + Line Feed? Only Carriage Return? Only Line Feed? Line Feed + Carriage Return? What about the encoding? ASCII? Latin-1? UTF-8? UTF-16?
Quote2) I don't have to worry about portability issues. It's all just plain-text.OMG, don't even get me started on that one. What's the newline convention on your OS? Carriage Return + Line Feed? Only Carriage Return? Only Line Feed? Line Feed + Carriage Return? What about the encoding? ASCII? Latin-1? UTF-8? UTF-16?
"Plain-text" is a lie!
I believe JSON would be smaller and faster to work with than XML, and still easy to understand and work with in a text editor. In some ways easier to read than XML because it's not so verbose and awful (subjective term). Natural indentation would make it easier to read, which is true regardless of it being JSON or XML.
Binary files would be the smallest and fastest to work with. Compression would probably make them all roughly equal in terms of size. It's the same data being stored in all cases.
In short, do what you want.
It would be probably 20% lighter in terms of pure character count. Maybe. Ultimately it doesn't really matter which 'format' I use, the only real comparison is between binary and non-binary.
One extra negative that isn't being mentioned about JSON is the requirement of needing JavaScript to use. Not everyone has JavaScript installed on their machine as its one of those languages that can be easily abused by online hackers and deliver a virus to you via a JavaScript supported program. Thus, if you used JSON, they may not be able to play your game period, as I think by utilizing JSON it would force the user to have JavaScript installed to run the program at all, much less try to modify the contents of the file.
While with XML, the requirements are much less. Something to consider.
Goof and Leeor_net,De nada
Thank you for posting the info on compressing saved data. I just implemented this in one of my projects using gzip and it dropped the save file size from around 750kb to 15kb! In my programming amateurishness, it didn't even occur to me that save data is easily compressed. Also, there is no noticeable delay in saving/loading with the compression/decompression added so big win.
Sorry for derailing the post a little. I'm happy to see all the progress on OutpostHD!
-Brett
One extra negative that isn't being mentioned about JSON is the requirement of needing JavaScript to use.
One extra negative that isn't being mentioned about JSON is the requirement of needing JavaScript to use.
Yes JavaScript is a scripting language, but all scripting languages REQUIRE an interpreter of some kind on the system to use it. If you don't have the interpreter you can't use JavaScript. When people don't install JavaScript on their machine, they are purposefully trying to avoid auto-activating scripts and the only surefire way to avoid them is to not have a JavaScript interpreter or any of the runtimes installed. Hope I've made things clear enough.
Also wonder which came first, Java or JavaScript.Both languages appeared May 23, 1995. The similarity in name is largely due to marketing reasons. The languages themselves are quite distinct. They both had strong early ties to web browsers.