How should I bind a json response so that ActiveResource can generate a DateTime object from a date?

I have a question object with the created_at attribute. When I find the question, I will return created_at from the REST service, but Activeresource does not convert the date string to DateTime.

Question.find(1) /questions/1.json Question.find(1).created_at.class => String 

What format should the Json response be for ActiveResource to convert it to DateTime?

If this is not possible, what should I do?

+4
source share
2 answers

According to slide # 14 in this presentation, you can do this in config/initializers/active_resource.rb :

 ActiveSupport::JSON::Encoding.use_standard_json_time_format = true ActiveSupport.parse_json_times = true 
+7
source

JSON will always be a string, as it passes data as string objects. If you want to convert it to a date / time object, you need to parse it on the server side.

Time.parse should do the trick

http://ruby-doc.org/stdlib-2.0/libdoc/time/rdoc/Time.html#method-c-parse

0
source

Source: https://habr.com/ru/post/1493467/


All Articles