RABL的JSON输出不符合标准吗? 它可以?
所以我有一个非常简单的rabl视图,它将支持xml和json(curr.rabl)的输出:
collection @currencies => "currencies" attributes :code, :name
我的rabl配置:
Rabl.configure do |config| config.include_json_root = false config.include_child_root = false config.xml_options = { :skip_types => true, :camelize => :lower } config.include_xml_root = false end
rabl为xml和json提供的输出:
XML:
AFN
Afghan Afghani AFA
Afghanistan Afghani
JSON:
{ currencies: [ { code: "AFN", name: "Afghan Afghani" }, { code: "AFA", name: "Afghanistan Afghani" }]}
对我来说, 这是不正确的 。 根据我读过的内容,JSON应该如下所示:
{ currencies: {currency: [ { code: "AFN", name: "Afghan Afghani" }, { code: "AFA", name: "Afghanistan Afghani" } ] } }
如果我设置config.include_child_root(/include_json_root) = true
,那么我得到以下内容(仍然不正确)
{ currencies: [ { currency: { code: "AFN", name: "Afghan Afghani" } }, { currency: { code: "AFA", name: "Afghanistan Afghani" } } ] }
首先,我是否认为RABL正在输出错误的json? 第二,有没有办法让RABL为这个视图做我想做的事,以及可能有许多嵌套子对象的任何其他视图?
要明确这绝对不是标准 :
{ currencies: {currency: [ { code: "AFN", name: "Afghan Afghani" }, { code: "AFA", name: "Afghanistan Afghani" } ] } }
两种forms的RABL产品都是标准的。 一个没有根,一个没有。 上面这件事没有任何意义,我从未见过任何API行为。
我总是使用你上面的第二种forms:
{ currencies: [ { currency: { ... }, }, { currency: { ... } }] }
如果你真的想这样做:
{ currencies: { currency: [ { ... }, { ... }] } }
我认为你可以将include_json_root设置为false并使用自定义节点,但它可能会变得复杂。