YAML::Tiny 不支持 JSON::XS::Boolean
当读取一些 JSON 数据结构,然后尝试使用 YAML::Tiny
来转储
它们时,我有时会收到错误
YAML::Tiny does not support JSON::XS::Boolean
,我理解为什么会出现这种情况(特别是 >YAML::Tiny
不支持布尔值,而 JSON
热衷于与其他标量明确区分开来),但是有没有快速的技巧可以将这些 JSON::XS: :Boolean
对象转换为普通对象0
和 1
只是为了快速转储到屏幕?
When reading some JSON data structures, and then trying to Dump
them using YAML::Tiny
, I sometimes get the error
YAML::Tiny does not support JSON::XS::Boolean
I understand why this is the case (in particular YAML::Tiny
does not support booleans, which JSON
is keen to clearly distinguish from other scalars), but is there a quick hack to turn those JSON::XS::Boolean
objects into plain 0
's and 1
's just for quick dump-to-the-screen purposes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
YAML::Tiny 不支持对象。不幸的是,它甚至没有一个选项来仅对所有对象进行字符串化,从而处理 JSON::XS::Boolean。
不过,您可以使用递归函数相当轻松地做到这一点:
该函数不会费心处理标量引用,因为 JSON 不会生成它们。它也不检查对象是否确实具有重载的字符串化。
Data::Rmap 对此效果不佳,因为它只会访问特定对象一次,无论它出现多少次。由于
JSON::XS::Boolean
对象是单例,这意味着它只会找到第一个true
和第一个false
。可以解决这个问题,但需要深入研究源代码来确定如何在其seen
哈希中生成密钥:我认为递归函数更清晰,并且不易受到
中的更改的影响>数据::Rmap
。YAML::Tiny doesn't support objects. Unfortunately, it doesn't even have an option to just stringify all objects, which would handle
JSON::XS::Boolean
.You can do that fairly easily with a recursive function, though:
This function doesn't bother processing scalar references, because JSON won't produce them. It also doesn't check whether an object actually has overloaded stringification.
Data::Rmap doesn't work well for this because it will only visit a particular object once, no matter how many times it appears. Since the
JSON::XS::Boolean
objects are singletons, that means it will only find the firsttrue
and the firstfalse
. It's possible to work around that, but it requires delving into the source code to determine how keys are generated in itsseen
hash:I think the recursive function is clearer, and it's not vulnerable to changes in
Data::Rmap
.