# Quick json question

**URL:** <https://www.ruby-forum.com/t/quick-json-question/173992>\
**Category:** Rails\
**Created:** [August 19, 2009, 4:36am UTC](https://www.ruby-forum.com/t/quick-json-question/173992 "2009-08-19T04:36:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![twaters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@twaters](https://www.ruby-forum.com/u/twaters)\
**Post date:** [August 19, 2009, 4:36am UTC](https://www.ruby-forum.com/t/quick-json-question/173992/1 "2009-08-19T04:36:59Z")

</div>

How does one format a time when serializing an active record object?  
Server side somehow or leave it up to client side javascript?

-tim

---

<div class="post-metadata">

**Author:** ![twaters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@twaters](https://www.ruby-forum.com/u/twaters)\
**Post date:** [August 19, 2009, 3:19pm UTC](https://www.ruby-forum.com/t/quick-json-question/173992/2 "2009-08-19T15:19:10Z")

</div>

Tim W. wrote:

> How does one format a time when serializing an active record object?

I’m not sure I quite follow your question, but I do know that the  
to\_json method as implemented in Rails uses the same date and time  
formats as does to\_xml.

Example:  
“2009-08-19T13:03:07Z”

> Server side somehow or leave it up to client side javascript?

In the case of to\_json the serialization of date and time gets formatted  
as a string on the server-side. I assume if you were to use that data in  
JavaScript (client-side) you would need to parse the string date/time  
format to a JavaScript Date object. I’m sure you could find a JS code  
snippet to handle that.

---

<div class="post-metadata">

**Author:** ![twaters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@twaters](https://www.ruby-forum.com/u/twaters)\
**Post date:** [August 19, 2009, 9:30pm UTC](https://www.ruby-forum.com/t/quick-json-question/173992/3 "2009-08-19T21:30:52Z")

</div>

Yeah… I was basically wonder what the best practice / preferred  
method for this was.

When using ajax and json to update a page with a new object that  
includes a date… (like created\_at) and you want that date to be only  
month and day… Do you parse the date on the server side and send over  
as string (Is that possible with to\_json?) or do you send over the  
default ISO format and do the conversion on the client side in  
javascript.

I’d like to do it on the server side, but then the only way I can see  
is a complete hack around by adding another method to the ActiveRecord  
class.

def created\_at\_formatted  
self.created\_at.to\_S(‘md’)  
end

object.to\_json( :methods =\> [:created\_at\_formatted] )

-tim

On Aug 19, 9:19 am, Robert W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![twaters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@twaters](https://www.ruby-forum.com/u/twaters)\
**Post date:** [August 19, 2009, 9:46pm UTC](https://www.ruby-forum.com/t/quick-json-question/173992/4 "2009-08-19T21:46:29Z")

</div>

Tim W. wrote:

> I’d like to do it on the server side, but then the only way I can see  
> is a complete hack around by adding another method to the ActiveRecord  
> class.
> 
> def created\_at\_formatted  
> self.created\_at.to\_S(‘md’)  
> end
> 
> object.to\_json( :methods =\> [:created\_at\_formatted] )

Model objects should not be responsible for formatting data for  
presentation. This responsibility should be implemented in view objects  
(or view helper objects such as formatter objects).

---

<div class="post-metadata">

**Author:** ![twaters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@twaters](https://www.ruby-forum.com/u/twaters)\
**Post date:** [August 20, 2009, 4:43am UTC](https://www.ruby-forum.com/t/quick-json-question/173992/5 "2009-08-20T04:43:28Z")

</div>

That is why I mentioned it was a hack…

I take it there is no way to do it via the to\_json method which is the  
view at this point?

Thanks…  
-tim

On Aug 19, 3:46 pm, Robert W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![twaters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@twaters](https://www.ruby-forum.com/u/twaters)\
**Post date:** [August 20, 2009, 4:02pm UTC](https://www.ruby-forum.com/t/quick-json-question/173992/6 "2009-08-20T16:02:04Z")

</div>

Tim W. wrote:

> That is why I mentioned it was a hack…
> 
> I take it there is no way to do it via the to\_json method which is the  
> view at this point?

Given that JSON offers no data type for storing date and time  
information, a choice must be made. Rails core team, I believe smartly,  
decided to borrow the string representation of time from XML, which does  
define standard date and time representations.

I would not call this a view. In the same way as databases store  
representations of the data, so does the serialized string of JSON or  
XML.

What I was getting at in my previous reply was that regardless of how  
the data is actually stored in a data model (whether that be in model  
objects, a database or a serialized string) it is still the  
responsibility of the view layer in MVC to format the data for  
presentation to the user.

Even though the serialized date may be stored as “2009-08-19T13:03:07Z”  
you probably would not want to present that to the end user. They would  
probably prefer something based on their own locale. In the US that  
might be: “August 19, 2009 9:00 AM” or in Europe maybe something like  
“19 August 2009 09:00.” If I’m not mistaken you can use I18n  
(Internationalization) in Rails to format dates based on the user’s  
locale.

Given that JSON (or XML) are not what you present to end users, you  
would store the date and time in a way meaningful to your model. Only  
when you present that data to a human do you run the data through  
formatters for presentation.
