나는 다음과 같은 JSON이 있습니다중첩 된 JSON (유효성 검사) 루비 클래스에
class Task
include ActiveModel::Model
include ActiveModel::Serializers::JSON
validates ..., presence: true
...
end
class Product
include ActiveModel::Model
include ActiveModel::Serializers::JSON
validates ..., presence: true
...
end
class Customer
include ActiveModel::Model
include ActiveModel::Serializers::JSON
validates ..., presence: true
...
end
는 내가하고 싶은 것은 JSON을을에는 직렬화입니다 : 유효성 검사기를 포함하여 해당 루비 클래스와
{
"ordernumber":"216300001000",
"datecreated":"2016-11-08T14:23:06.631Z",
"shippingmethod":"Delivery",
...
"customer":{
"firstname":"Victoria",
"lastname":"Validator"
},
"products":[
{
"sku":"ABC1",
"price":"9.99"
},
...
]
}
을 Ruby 클래스. 문제는 Task 클래스가 올바르게 초기화된다는 것입니다. 그러나 고객 및 제품과 같은 중첩 클래스는 해시로 남아 있습니다. (A 작업 한 고객 및 여러 제품을 가지고)
예 : 나는이 사용 ActiveModel하고 중첩 된 JSON의 유효성을 검사 할 어떻게
json = %Q{{ "ordernumber":"216300001000", "datecreated":"2016-11-08T14:23:06.631Z", "shippingmethod":"Delivery", "customer":{ "firstname":"Victoria", "lastname":"Validator" }, "products":[ { "sku":"ABC1", "price":"9.99" } ] }}
task = Task.new()
task.from_json(json)
task.class
# => Task
task.products[0].class
# => Hash
? (나는 레일즈를 사용하지 않고있다.)
[중첩 속성] (http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html)을 사용 하시겠습니까? – tadman