YAML vs JSON vs XML?

Started by
21 comments, last by Ravyne 10 years, 8 months ago
...
There's quite a bit of art to designing and XML schema, but good designs use elements and properties together in a way that reduces redundancy and encourages correct use. You can define your format using a DTD and validate such files before they touch your game or content pipleline.
...

+1 but use XSD instead of the deprecated DTD for validation purposes wink.png

As far as I can tell, many systems are moving towards json for config etc. I can't speak for the rest, but I personally switched from xml (or xml-plists actually) to json because editing (and finding errors) is so much easier in json. Not to mention that json can be much more compact while still retaining readability.

My vote's for json.

Well, in my experience it's alot easier to spot formatting errors in XML than in json. If your texteditor did not spot it already, just use XSD-validation, it tells you exactly where the error is. Finding errors in data content is even harder in json.

Advertisement

I think at the very least you could abbreviate that format -- remember that XML is really meant to be a semantic format -- I might do something like the following:


<item id="1" name="sword" category="weapon">
  <description> Just a regular sword.</description>
  <components>
    <wieldable />
  </components>
</item>

I think you could abbreviate that a bit further, by removing the <components> tag, since it is kind of redundant. Any child elements of "item" could be seen as components of the item, and the "description" is one of them.

Personally, I try to put everything in tags and arguments too, since I find it easier to write the parser for with tinyxml.

So I'd write

<description text="Just a regular sword."/>

That way, you also avoid the end tags, and get the format a little bit more brief.

So this:

<item id="1" name="sword" category="weapon">
<description text="Just a regular sword."/>
<wieldable/>
</item>

or something...

That's one problem with xml, so many ways to do it.

But I like it.

As far as JSON is concerned, if you're not using Javascript, there isn't much point. The reason that JSON is so nice in web development is that it is instantly recognized by the browser's JS interpreter as an object so there's no additional parsing necessary. This is only true in Javascript, though. Any other language requires a custom library to interpret JSON.

This is becoming increasingly less true. Many of the NoSQL databases for example expect and return JSON. More and more web services are moving from XML to JSON as well. There are increasingly parsers for JSON in more and more languages, often many times lighter than XML. Frankly, if you aren't having to describe/discover the data's type, XML is overkill.

Since you are working in Java ( or C++ is another option here ), I would also consider checking out ProtocolBuffers. Ultra light weight and better, can actually generate java code to create a Builder for the type you define.

As for the verbosity of XML, it is fairly simple to use zlib to compress the XML. In the case of network transfer, is may be quite worth to spend the extra CPU-cycles and send a XMLZ-file.

You then have the tooling of XML in a fairly (albeit not the most) compact format.

this works pretty good IME.

though, granted, it is possible to get the speed/compression tradeoff a little better with a specialized binary serialization, but this is a bit more work (IOW: may involve working with entropy-coded bitstreams, ...).

a minor issue of XML+Deflate, is that it isn't by itself ideally suited for transmitting a stream of messages, so generally needs to be wrapped in some sort of container. in a typical example, this will be a tag-value, followed by a length, followed by the compressed data.

in some cases, it may also make sense to "escape code" the data, such that a tag may never appear in data. this may allow things like resynchronizing with a data stream.

as for data representations:

I also use S-Expressions to some extent as well, though these are not as widely popular.

another option is basically creating a byte-oriented data serialization, and then feeding this through zlib / deflate.

I've had very positive experiences with Protocol Buffers. They are fast, compact and have text and binary representation (even convertible from one form into the other using just a small tool). They are just so little work and versatile that I am using them for lots if different things: Network protocols, configuration files, save games, ...

I have found few libraries that made my life as much easier as protobuf did.

Protocol buffers looks really nice for anything that isn't a tree. (or am I missing something?)

I guess XML is at its best when you have to describe a tree, but I will definitely look into protocol buffers some more for things that isn't, and sending stuff

Basically all I need this to do is the following:

-Have "Item" class and "ItemLoader" class

-ItemLoader reads the data file and creates an instance of item for each item in the file

-Item has very limited fields/methods(Id/description/name)

-ItemLoader reads the data file and adds all components to the item(ex. Consumable, wieldable, weapon, shield, etc.)

-Easy to manipulate via API(Going to be editing and creating items from a GUI)

Perhaps just wrap it one level higher, so you can easily switch it out depending on the job at hand.

At work, we tend to swap between JSON and XML weekly ;)

http://tinyurl.com/shewonyay - Thanks so much for those who voted on my GF's Competition Cosplay Entry for Cosplayzine. She won! I owe you all beers :)

Mutiny - Open-source C++ Unity re-implementation.
Defile of Eden 2 - FreeBSD and OpenBSD binaries of our latest game.

Protocol buffers looks really nice for anything that isn't a tree. (or am I missing something?)

I guess XML is at its best when you have to describe a tree, but I will definitely look into protocol buffers some more for things that isn't, and sending stuff


message X { 
    optional X x = 1;
    optional X y = 2;
}

This works just fine. Trees!


<?xml version="1.0"?>
<item id="1">
    <name>sword</name>
    <components>
        <component wieldable="true" />
    </components>
</item>

Is this bad?

Beginner in Game Development?  Read here. And read here.

 

This topic is closed to new replies.

Advertisement