我有一些类通过HTTP发送到API,我需要导出到带有所有属性(包括nils)的json。
我有这样一门课:
class Customer
JSON.mapping(
id: UInt32 | Nil,
name: String | Nil,
email: String | Nil,
token: String
)
def initialize @token
end
end当我创建一个Customer实例并导出到json时,我将检索意想不到的结果。
c = Customer.new "FULANITO_DE_COPAS"
puts c.to_json
# Outputs
{"token":"FULANITO_DE_COPAS"}
# I expect
{"id":null,"name":null,"email":null,"token":"FULANITO_DE_COPAS"}如何强制to_json函数完全导出porperties类?
发布于 2017-09-13 11:33:10
class Customer
JSON.mapping(
id: {type: UInt32?, emit_null: true},
name: {type: String?, emit_null: true},
email: {type: String?, emit_null: true},
token: String
)
def initialize(@token)
end
end
c = Customer.new "FULANITO_DE_COPAS"
c.to_json #=> {"id":null,"name":null,"email":null,"token":"FULANITO_DE_COPAS"}https://stackoverflow.com/questions/46195766
复制相似问题