如何在跨语言对象序列化中处理 ruby 符号
我目前正在开发一个项目,我需要将对象从 ruby 传输到 python 并再次传输回来,显然序列化是可行的方法。我研究过像 yaml 这样的东西,但决定编写自己的东西,因为我不想在分发时处理库的依赖关系等。我在此处写下了这种序列化格式的工作原理。
我的问题是,由于这种格式旨在在 ruby 和 python 之间跨语言工作, 我应该如何序列化 ruby 的符号?我不知道有哪个对象在 python 中以相同的方式工作。包含符号的转储应该失败吗?我应该将其序列化为字符串吗?什么是最好的?
I'm currently working on a project where I need to transfer objects from ruby to python and back again, obviously serialization is the way to go. I've looked at things like yaml but decided to write my own as I didn't want to deal with the dependencies of the libraries and such when it came time to distribute. I've wrote up how this serialization format works here.
my question is that as this format is intended to work cross language between ruby and python,
how should I serialize ruby's symbols? I'm not aware of a object that works the same way in python. should a dump containing a symbol fail? should I just serialize it as a string? what would be best?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这不是要看你的项目需要什么吗?如果符号很重要,您将需要某种方法来处理它们。
我不是 Ruby 程序员,但从我刚刚读到的内容来看,我认为将它们转换为字符串可能是最简单的。标准 Python 解释器将为相同的短字符串重用内存,这似乎是建议使用符号的一个关键原因。
编辑:如果它需要为其他程序员工作,来回传递值不应该改变它们。因此,您要么必须正确处理符号,要么立即抛出错误。用 Python 来说应该很简单:
Doesn't that depend on what your project needs? If symbols are important, you'll need some way to deal with them.
I'm not a Ruby programmer, but from what I've just read, I think converting them to strings is probably easiest. The standard Python interpreter will reuse memory for identical short strings, which seems to be a key reason suggested for using symbols.
EDIT: If it needs to work for other programmers, passing values back and forth shouldn't change them. So you either have to handle symbols properly, or throw an error straight away. It should be simple enough in Python:
您不使用 JSON 或 XML 等标准数据交换格式有什么原因吗?它们似乎被无数的应用程序、服务和程序员所接受。
Any reason you're not using a standard data interchange format like JSON or XML? They seem to be acceptable to countless applications, services, and programmers.
如果符号是绊脚石,那么您有三种选择:不允许它们、将它们即时转换为字符串或找出一种方法使它们在其他语言中通用和/或无害。
If symbols are a stumbling block then you have three choices, don't allow them, convert them to strings on the fly or figure out a way to make them universal and/or innocuous in other languages.