对于我的项目,我需要存储有关协议的信息(发送的数据(最有可能是整数)及其发送的顺序)以及可能采用如下格式的信息:
'ID' '字符串' '附加整数数据'
这些信息将由 Java 程序读取并存储在内存中进行处理,但我不知道存储这些数据的最合理的格式是什么?
编辑:这里有一些额外的信息:
1)我将在游戏服务器中使用这些数据。
2)由于它是游戏服务器,速度不是主要考虑的问题,因为这些数据主要在启动期间被读取和利用,这不应该经常发生。
3)但是,我希望将内存消耗保持在最低限度。
4)第二个数据“示例”将用作“字典”来查找特定游戏内物品的名称、其统计数据和其他整数数据(因此可能会变得非常大,与包含协议信息的第一个数据不同,其中每个文件只会记录小的协议片段,例如登录协议)。
5)是的,我希望数据是“人类可编辑的”。
编辑 2:这是我所做的选择:
JSON - 用于协议描述
CSV - 对于字典
For my project, I need to store info about protocols (the data sent (most likely integers) and in the order it's sent) and info that might be formatted something like this:
'ID' 'STRING' 'ADDITIONAL INTEGER DATA'
This info will be read by a Java program and stored in memory for processing, but I don't know what would be the most sensible format to store this data in?
EDIT: Here's some extra information:
1)I will be using this data in a game server.
2)Since it is a game server, speed is not the primary concern, since this data will primary be read and utilized during startup, which shouldn't occur very often.
3)Memory consumption I would like to keep at a minimum, however.
4)The second data "example" will be used as a "dictionary" to look up names of specific in-game items, their stats and other integer data (and therefore might become very large, unlike the first data containing the protocol information, where each file will only note small protocol bites, like a login protocol for instance).
5)And yes, I would like the data to be "human-editable".
EDIT 2: Here's the choices that I've made:
JSON - For the protocol descriptions
CSV - For the dictionaries
发布评论
评论(4)
有很多因素需要权衡——以下内容可能会帮助您弄清楚这一点:
1)速度/内存使用情况:如果数据需要非常快地加载或非常大,您可能需要考虑滚动您自己的二进制格式。
2) 可移植性/兼容性:与#1 相平衡的是您可能希望在其他地方使用数据,而程序不会读取自定义二进制格式。在这种情况下,最重要的可能是 CSV、dBase、XML 和我个人最喜欢的 JSON。
3) 简单性:像 CSV 这样的分隔格式很容易手动读取、写入和编辑。使用带有适当转义的双引号或选择不会出现在数据中的分隔符。
如果您可以发布有关您的情况以及这些因素的重要性的更多信息,我们也许能够为您提供进一步指导。
There are many factors that could come to weigh--here are things that might help you figure this out:
1) Speed/memory usage: If the data needs to load very quickly or is very large, you'll probably want to consider rolling your own binary format.
2) Portability/compatibility: Balanced against #1 is the consideration that you might want to use the data elsewhere, with programs that won't read a custom binary format. In this case, your heavy hitters are probably going to be CSV, dBase, XML, and my personal favorite, JSON.
3) Simplicity: Delimited formats like CSV are easy to read, write, and edit by hand. Either use double-quoting with proper escaping or choose a delimiter that will not appear in the data.
If you could post more info about your situation and how important these factors are, we might be able to guide you further.
XML、JSON 或 CSV ?
How about XML, JSON or CSV ?
我已经使用 XML 编写了类似的协议规范。 (可在此处获得。)
我认为这是一个很好的匹配,因为它捕获了指定的层次结构性质消息/网络包/字段等。字段的顺序定义良好等等。
我什至编写了一个代码生成器,它生成消息发送/接收类,其中包含 XSLT 中每种消息类型的方法。
我认为唯一的缺点是冗长。如果您的规范结构非常简单,我建议您使用一些简单的自制格式,并使用您选择的解析器生成器为其编写一个解析器。
I've written a similar protocol-specification using XML. (Available here.)
I think it is a good match, since it captures the hierarchal nature of specifying messages / network packages / fields etc. Order of fields are well defined and so on.
I even wrote a code-generator that generated the message sending / receiving classes with methods for each message type in XSLT.
The only drawback as I see it is the verbosity. If you have a really simple structure of the specification, I would suggest you use some simple home-brewed format and write a parser for it using a parser-generator of your choice.
除了此处其他人建议的格式(CSV、XML、JSON 等)之外,您还可以考虑将信息存储在 Java 属性文件中。 (请参阅
java.util.Properties
类。)代码已经为您准备好了,因此您只需弄清楚要使用的属性名称(或名称前缀)即可。Properties
类还提供以简单 XML 格式存储/加载属性的功能。In addition to the formats suggested by others here (CSV, XML, JSON, etc.) you might consider storing the info in a Java properties file. (See the
java.util.Properties
class.) The code is already there for you, so all you have to figure out is the properties names (or name prefixes) you want to use.The
Properties
class also provides for storing/loading properties in a simple XML format.