Alternative to JSON and XML?

Started by
29 comments, last by ApochPiQ 9 years, 4 months ago
Currently, in my game, I use XML for most of my data files. Not because I particularly like XML, but because the library I'm using (PugiXML) is particularly friendly. However, because the files are XML, they feel rather unwieldy to edit.

In the past I tried switching to JSON. Ignoring the fact that I couldn't find a C++ JSON parsing library that didn't spit out tons of compiler warnings, I found JSON to be just as annoying to use as XML. My primary interest in JSON was that it was a simpler format, and seemed to have less extraneous text required to be a valid and easily-readable file. However, after using it, I found that to not be the case.

  • The entire contents of the file has to be surrounded in curly braces. Not a big deal, but bothers me as it feels unnecessary. I don't care about it being parseable by Javascript, I just want it to hold some data from my game, that only my game will look at. I can't just dive in, create a new file, I first have to start with the curly braces to make it valid.
  • All variable names have to be surrounded by quotes. If the file is a decent size, pretty soon all you see is a big sea of quotes. They add noise and extra work to what should otherwise be a simple layout.
  • JSON is very strict about commas. If you create an array of items, every item must end with a comma, EXCEPT for the very last item, which must NOT end with a comma. It is very common fro me to get parse errors because I added or removed something from an array, and didn't notice that the commas were wrong.

I recently found this blog post which gives pretty much the same list, and shows an example of an alternative layout. I think the result he comes up with is pretty good. If there was a library to parse that format, I'd use it.

But I think it could be taken even further. Considering that this is for storing data for a game to use, I don't remotely care about any features that exist for the sake of Javascript or the web. All I need is a list of key-value pairs, where the value could be a single value or an array. Something like this (as a random example):


name "Test model" // Simple key-value pair
version 1.0 // Numeric values are allowed
geometry { // Value is an array
vertices {
0, 0, 0 // Array of values. No key given, so accessible by index position
1, 0, 0 // Another array of values, also accessible by index
1, 1, 0 // ...
0, 1, 0 // ...
}
indices 0, 1, 2, 2, 3, 0 // Value is an array. Array values are inline, so don't need curly braces.
}


To me (and I know this is personal opinion), this format looks great. Simple, no extra characters, easy to understand and write.

So, I guess I'm wondering:

  1. Why do most people seem to be using XML or JSON for their data files? In the context of writing a game, where you don't need to communicate with an external piece of software, it seems like overkill.
  2. Does there already exist a file format (and accompanying library) that is extremely simple, like what I've written here? If not, I may have to write it myself, but no sense rewriting something that already exists.
Advertisement
I've never liked XML, and I have many of the same objections to JSON that you do. I'm not aware of any library that does exactly as you are asking for, though. Personally, I like to use Lua for my game files and data. It's quite handy for data description, even if I don't use it as an embedded script but just to load and move data.
i use 2 formats for serialization and transfer. Binary and XML.
There is practically no reason to use human readable format if you transfer data.

i prefer XML because it is more flexible. I have event scripts defined in XML files, lots of CDATA sections etc...
JSON's strict syntax with commas and double quotes make it hard for me to write.
XML is primarily useful if you take advantage of the hundreds of tools that exist for working with XML. JSON is in a similar boat.

If you're editing XML by hand, you're doing it wrong. At the bare minimum, use an XML editing tool and a schema to validate your content for you.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Because I can serialize and deserialize json, XML, or binary in one line and move on to things that matter.

If you're editing XML by hand, you're doing it wrong. At the bare minimum, use an XML editing tool and a schema to validate your content for you.


But that's just it. I don't care about the schema, or using a special editor. I just want an easy to use, easy to read file format for storing structured data. Whether it is XML, or JSON, or Lua, or something else, doesn't really matter so long as it fits those two goals.

    Why do most people seem to be using XML or JSON for their data files? In the context of writing a game, where you don't need to communicate with an external piece of software, it seems like overkill.


XML and JSON are well understood formats with easy-to-use, mature parser implementations in many languages (often with some kind of support for at least XML built-into the language); when something ticks those boxes it's difficult to call it overkill. In the context of games you frequently are communicating with other bits of software, you could have a complex toolchain spanning a number of platforms with tools written in various languages.


    Does there already exist a file format (and accompanying library) that is extremely simple, like what I've written here? If not, I may have to write it myself, but no sense rewriting something that already exists.

[/quote]
Take a look at YAML, it's pretty much a format expressly intended to alleviate the problems you highlighted.
I never understood why anyone would call xml human readable. It's a complete mess and most of the parsers only implement half of it.
JSON is more readable to me.

But if you really need something only for yourself/inhouse I would alwqays choose ini or an ini-based format.
You can choose arbitrary section names and use them to build hierarchies (e.g. [Engine\Renderer]).
If you need lists or tables, just use a csv-string, or make up your own format.

Of course you shouldn't use GetPrivateProfileString for performance and portability reasons. Write your own parser or use something like SDL_Config.
The main advantage to XML and JASON is they're fully buzzword-compliant. There are plenty of books published on them to earn the authors money, not to mention websites to sell views, and it's easy to write filters for scanning resumes based on those keywords.

Another advantage is you can just grab an available library and integrate it into your project. Excepting that they tend to be some complex and do-everything that by the time you've bent the API to your will and got everything integrated into your project, you could have written and tested your own simple data loader twice over.

A small data loader is easy to write and write tests to validate. JFDI.

Stephen M. Webb
Professional Free Software Developer

might want to look at libconfig, which i discovered because of this topic, and on first glance it might be just what you're looking for.
http://www.hyperrealm.com/libconfig/
(check out the documentation link on their site)

devstropo.blogspot.com - Random stuff about my gamedev hobby

This topic is closed to new replies.

Advertisement