开发者

Document format

I'm making a game with my friend. Now t开发者_如何学Gohere is a question about levels. First version was built with XML. There is a few reasons I don't like it: too much data and there were some problems with parser we used. We are using Boost so I decided to look at json-parser.

The structure is much smaller and optimized. What do you think, isn't this too radical to change from powerful xml to simple json? Would it be enough of json-structure power to organize big game-level with a lot of extra-data and attributes?


I use JSON a lot in webapps written in python - there are really good decoders for both XMLand JSON. There really isn't a lot of difference except XML takes a little more space, but is more readable. JSON looks almost like a Python dictionary already, so that makes sense. Probably the main reason why JSON is more common on the web is that JSON-RPC is pretty good whereas XML-RPC is poorly thought of and SOAP is too heavy.

So it depends if you can find a good JSON decoder or not, which is fast enough for your needs. I don't know boost or work in C/C++ very often.


I think JSON is a better fit for data-oriented use cases (like yours, I assume) than XML. XML is good as markup language; or when one has to mix and match multiple vocabularies. I don't find XML more powerful for such use cases; and there are no reasons why you could not map any data you want to a JSON structure. With XML much of power comes from processing tools and XML vocabularies, not the core markup language which is reasonably simple in itself.

Chances are that both XML and JSON would actually work for you, so perhaps just see which has better libraries for your development style.


If you are already using a reader that puts it into a DOM object I don't think it is worth the hassle to switch from XML to JSON unless you end up changing your data format during the process. Especially if you are using an editor to create your program's data because you will have much better tools editing XML documents than JSON. More than likely the parse time for these DOM objects aren't going to be much different between the implementations but your JSON instance DOM objects probably will take up less space if that is a concern. You will be able to store whatever you need in either XML or JSON, so that shouldn't be a worry. As a level description language I would use XML because I could define a schema and it is easier to edit by hand if need be. Also it sounds like you may want the ability to create these levels effeciently as data is being read and disregarding all the extra instance data if it isn't needed as soon as possible. An XML SAX or pull parser would be ideal for that.

Now that my brief answer is over, I'll ramble a bit more with information that may help you better decide for yourself.

How do you want to access the data? Do you want to simply read it all in and have a big object to access or do you only need to look at the data once to create objects based on that information? Do you have any concerns about performance during parsing and how quickly you can respond to new information being read in? Is the format of your data likely to change a lot? Do you want to provide a standard in which this information can be shared outside of your application? What programming language am I using? Some languages have really great tools for JSON vs. XML depending on the language.

Given the only choice JSON or XML I would decide based on the use cases relevant to the application.

I would use XML if I need to really tweak performance in my parser and have a very structured format that can be described and shared and is unlikely to change often. I could use schema to describe the data structure requirements and that would allow other people to easily support my data files. I would also use XML if my information is large and I want a chance to parse the data in chunks and store them directly into objects which will end up with a smaller footprint than using a generic DOM model and parser. I would use SAX push reader or a stream reader that allows me to pull if I needed to respond to information as it was read in. There are many good libraries to do this quickly and easily for XML. If I needed to edit the data often by hand or print out for debug purposes an XML document is already in a good format for this.

I would use JSON if I need to save instance document space and I want a DOM type object. If I had two applications just trying to communicate data such as Python and Java I would use JSON because it can read these into instance objects with setters and getters without a complicated framwework, even though this could make things slow. If my need is to only serialize and transfer data objects JSON would be my choice over XML if those are my only options.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜