我一直在努力寻找一个可以用于嵌套对象(特别是类)的模块,许多模块都很接近,但我不想将属性设置为 null 以跳过我不需要的值。
如果我能写一个 TO_JSON 方法来返回一个结构,然后由一个模块进行编组就好了。或者,只需在我们不想包含在最终 JSON 文档中的属性上使用 "json-skip"。
类似这样:
class Something {
has Str $.foo;
has Str $.bar is json-skip;
}
或class Something {
has Str $.foo;
has Str $.bar;
method TO_JSON {
return {
foo => $.foo
}
}
}
JSON::Tiny、JSON::Fast、JSON::Marshal等单独构造非常好用,有些甚至可以与嵌套结构一起使用,但目前我找不到排除属性的方法。
最终,我想做到类似于:
my $json-document = to-json(@nestedInstances);