Ruby on Rails JSON.parse(object.to_json) doesn't return the original object -


it seems object this:

#<course id: 1, name: "targeting triple negative breast cancer", description: nil, disclosure: nil, created_at: "2010-11-11 14:43:31", updated_at: "2010-11-11 14:43:31", picture: nil, course_reference_id: nil, authors: "lisa a. carey, m.d.", volumn_number: nil, publication_date: nil, expiration_date: nil, credits: 1, featured: false> 

i should able json.parse(course.to_json), when try this, instead of returning original object, equivalent of course.new (an object nil attributes).

when course.to_json get:

"{\"attributes\":{\"name\":\"targeting triple negative breast cancer\",\"expiration_date\":null,\"created_at\":\"2010-11-11 14:43:31\",\"featured\":\"0\",\"publication_date\":null,\"updated_at\":\"2010-11-11 14:43:31\",\"id\":\"1\",\"disclosure\":null,\"volumn_number\":null,\"credits\":\"1\",\"authors\":\"lisa a. carey, m.d.\",\"course_reference_id\":null,\"description\":null,\"picture\":null},\"json_class\":\"course\",\"attributes_cache\":{}}" 

but when json.parse(course.to_json) get:

#<course id: nil, name: nil, description: nil, disclosure: nil, created_at: nil, updated_at: nil, picture: nil, course_reference_id: nil, authors: nil, volumn_number: nil, publication_date: nil, expiration_date: nil, credits: nil, featured: false> 

i using rails 2.3.5 , same result other objects.

to serialize/deserialize object use #to_json , #from_json. don't try parse json string directly.

json = course.new(...).to_json course.new.from_json(json) 

also note should avoid calling json.parse directly. use activesupport::json bridge instead.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -