文件格式
我正在和我的朋友一起做一个游戏。现在有一个关于等级的问题。第一个版本是使用 XML 构建的。我不喜欢它有几个原因:数据太多,而且我们使用的解析器存在一些问题。我们正在使用Boost,所以我决定看看 json 解析器。
结构更小并且更优化。你觉得呢,从强大的 xml 改成简单的 json 是不是太激进了? json 结构的能力是否足以组织具有大量额外数据和属性的大型游戏级别?
I'm making a game with my friend. Now there 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在用 python 编写的 web 应用程序中经常使用 JSON - XML 和 JSON 都有非常好的解码器。实际上并没有太大区别,只是 XML 占用更多空间,但更具可读性。 JSON 看起来几乎已经像一个 Python 字典了,所以这是有道理的。 JSON 在 Web 上更常见的主要原因可能是 JSON-RPC 非常好,而 XML-RPC 没有得到很好的考虑,而且 SOAP 太重了。
所以这取决于你是否能找到一个好的 JSON 解码器,它的速度足以满足你的需求。我不太了解 boost 或经常使用 C/C++ 工作。
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.
我认为 JSON 比 XML 更适合面向数据的用例(我认为像您的用例)。 XML 是很好的标记语言;或者当一个人必须混合和匹配多个词汇时。
对于此类用例,我认为 XML 没有更强大的功能。并且您没有理由无法将所需的任何数据映射到 JSON 结构。 XML 的大部分功能来自处理工具和 XML 词汇表,而不是本身相当简单的核心标记语言。
XML 和 JSON 很可能实际上都适合您,所以也许只需看看哪个有
更适合您的开发风格的库。
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.
如果您已经在使用将其放入 DOM 对象的阅读器,我认为不值得从 XML 切换到 JSON,除非您最终在此过程中更改数据格式。特别是如果您使用编辑器来创建程序数据,因为您将拥有比 JSON 更好的编辑 XML 文档的工具。这些 DOM 对象的解析时间很可能在实现之间不会有太大差异,但如果这是一个问题,您的 JSON 实例 DOM 对象可能会占用更少的空间。您将能够以 XML 或 JSON 形式存储您需要的任何内容,因此不必担心。作为关卡描述语言,我会使用 XML,因为我可以定义模式,并且如果需要的话,手动编辑会更容易。另外,听起来您可能希望能够在读取数据时有效地创建这些级别,并在不需要尽快需要时忽略所有额外的实例数据。 XML SAX 或拉解析器将是理想的选择。
现在我的简短回答已经结束,我将多说一些信息,以帮助您更好地自己做出决定。
您想如何访问数据?您是否想简单地读取所有内容并访问一个大对象,或者您只需要查看一次数据就可以根据该信息创建对象?您是否对解析过程中的性能以及对读入的新信息的响应速度有任何担忧?您的数据格式可能会发生很大变化吗?您是否想提供一个标准,使这些信息可以在您的应用程序之外共享?我使用什么编程语言?有些语言对于 JSON 和 XML 都有非常好的工具,具体取决于语言。
考虑到 JSON 或 XML 是唯一的选择,我将根据与应用程序相关的用例来决定。
如果我需要真正调整解析器的性能并且拥有可以描述和共享且不太可能经常更改的非常结构化的格式,我会使用 XML。我可以使用模式来描述数据结构要求,这将允许其他人轻松支持我的数据文件。如果我的信息很大,并且我希望有机会以块的形式解析数据并将它们直接存储到对象中,那么与使用通用 DOM 模型和解析器相比,最终的占用空间会更小,我也会使用 XML。如果我需要响应读入的信息,我会使用 SAX 推送阅读器或流阅读器,它允许我拉取信息。有许多优秀的库可以快速轻松地为 XML 完成此操作。如果我需要经常手动编辑数据或出于调试目的打印数据,XML 文档已经是适合此目的的格式。
如果我需要节省实例文档空间并且想要 DOM 类型对象,我会使用 JSON。如果我有两个应用程序只是尝试通信数据(例如 Python 和 Java),我会使用 JSON,因为它可以使用 setter 和 getter 将这些数据读入实例对象,而无需复杂的框架,尽管这可能会使速度变慢。如果我只需要序列化和传输数据对象,那么我会选择 JSON 而不是 XML(如果这是我唯一的选择)。
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.