如何在跨语言对象序列化中处理 ruby​​ 符号

发布于 2024-10-07 09:45:18 字数 354 浏览 6 评论 0原文

我目前正在开发一个项目,我需要将对象从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暗藏城府 2024-10-14 09:45:18

这不是要看你的项目需要什么吗?如果符号很重要,您将需要某种方法来处理它们。

我不是 Ruby 程序员,但从我刚刚读到的内容来看,我认为将它们转换为字符串可能是最简单的。标准 Python 解释器将为相同的短字符串重用内存,这似乎是建议使用符号的一个关键原因。

编辑:如果它需要为其他程序员工作,来回传递值不应该改变它们。因此,您要么必须正确处理符号,要么立即抛出错误。用 Python 来说应该很简单:

class Symbol(str):
    pass

# In serialising code:
if isinstance(x, Symbol):
    serialise_as_symbol(x)

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:

class Symbol(str):
    pass

# In serialising code:
if isinstance(x, Symbol):
    serialise_as_symbol(x)
晚风撩人 2024-10-14 09:45:18

您不使用 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.

べ繥欢鉨o。 2024-10-14 09:45:18

如果符号是绊脚石,那么您有三种选择:不允许它们、将它们即时转换为字符串或找出一种方法使它们在其他语言中通用和/或无害。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文