替换 XML 以表示 C++ 中的数据结构
在工作中,我们有一个测试工具,用于向数据源发送查询。 该工具接受 XML 文件形式的输入。 只要我们试图表示的数据结构是一层深的,XML 文件就简单且易于解析。 但现在这些数据结构更加复杂,并且用 XML 表示它们变得非常混乱。 关于我可以用什么来表示数据结构而不是 XML 有什么想法吗?
示例:
之前:
class Foo {
int userId;
string name;
string address;
string eMail;
}
现在:
class Foo {
int userId,
string name,
vector<Location> loc,
map<string, string> attributes;
}
class Location {
Address addr; //class Address
vector<LocatedTime> lcTime; //class LocatedTime
Position ps; //class Position
}
...等等以具有任意数量的嵌套结构。
我倾向于 JSON,但对任何表示格式持开放态度。
At work, we have a testing tool that is used to send queries to a data source. The tool takes in input as XML files. The XML files were simple and easy to parse as long as the data structures we tried to represent were one layer deep. But now these data structures are more complex and representing them in XML is getting to be highly confusing. Any thoughts on what I can use to represent the data structures instead of XML?
Example:
Before:
class Foo {
int userId;
string name;
string address;
string eMail;
}
Now:
class Foo {
int userId,
string name,
vector<Location> loc,
map<string, string> attributes;
}
class Location {
Address addr; //class Address
vector<LocatedTime> lcTime; //class LocatedTime
Position ps; //class Position
}
... and so on to have any number of nested structures.
I was leaning towards JSON but am open to any representation formats.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否查看过协议缓冲区? 二进制序列化在处理时间和存储空间方面非常有效。 目前在 C++、Java 和 Python 中得到“适当”支持,并将有更多实现(来自第三方 - 例如我自己;我正在实现 C# 端口)。
Have you looked at Protocol Buffers? Binary serialization which is pretty efficient in processing time and storage space. Currently "properly" supported in C++, Java and Python, with more implementations coming (from third parties - such as myself; I'm implementing a C# port).
YAML 可能就是您要找的。
YAML may be what you're looking for.
您可以考虑使用 Lua(或其他脚本语言)。 您将获得良好的数据结构语法(大致与 JSON 相当),并具有编程语言的全部功能。 因此,您拥有变量(您可以逐个构建数据结构,象征性地声明重复值等)、循环(测试数据通常是重复的)、函数(将它们视为数据中样板结构的宏)。
对于此类用途,Lua 是一个特别有吸引力的候选者,因为它很小(为您的程序增加了 100-200K)并且具有非常优雅的 C 代码接口。
You might consider using Lua (or another scripting language). You get a nice data structure syntax (roughly on par with JSON), with the full power of a programming language. Thus you have variables (you can build up your data structures piece by piece, symbolically declare recurring values, etc.), loops (test data is often repetitive), functions (think of them as macros for boilerplate constructs in your data).
Lua is a particularly attractive candidate for this sort of use because it's small (adds 100-200K to your program) and has a pretty elegant interface to and from C code.