Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Catalyst: Users

Noob hoping for help encoding mysql datetime fields for JSON

 

 

Catalyst users RSS feed   Index | Next | Previous | View Threaded


steve.seremeth at dealer

Mar 15, 2012, 2:38 PM

Post #1 of 4 (669 views)
Permalink
Noob hoping for help encoding mysql datetime fields for JSON

Hello -

I am semi new to Perl and Catalyst. I have walked through the Catalyst tutorial a couple times and have also picked up a couple books and done much googling that always seem to get me close but leave me hanging.

Have been looking at tons of docs for catalyst::controller::rest and catalyst::view::json amongst many others and can't find joy.

Anyway -- I think I would be on the right track if someone solved this basic inquiry:

Working from this example:
http://www.catalystframework.org/calendar/2009/22# An AJAX CRUD Interface with Catalyst and jQuery<http://www.catalystframework.org/calendar/2009/22#%20An%20AJAX%20CRUD%20Interface%20with%20Catalyst%20and%20jQuery>

which I have working fine, fwiw...

How would you expose the "created" column from the db (schema came from here: http://search.cpan.org/~bobtfish/Catalyst-Manual-5.9003/lib/Catalyst/Manual/Tutorial/04_BasicCRUD.pod<http://search.cpan.org/%7Ebobtfish/Catalyst-Manual-5.9003/lib/Catalyst/Manual/Tutorial/04_BasicCRUD.pod> and is, in this example, a sqlite TIMESTAMP col) in the JSON generated by the API controller's grid_POST method?

If I simply add the column to the API controller here:

<snip>
$data{rows} = [.
map { +{
id => $_->id,
cell => [
$_->id,
$_->title,
$_->rating,
$_->author_list,
$_->created,
]
} } $paged_rs->all
];
</snip>

The app throws this:

Content-Type application/json had a problem with your request.
***ERROR***
encountered object '2012-02-29T17:16:27', but neither allow_blessed enabled nor TO_JSON method available on it at /usr/local/share/perl/5.12.4/Catalyst/Action/Serialize/JSON.pm line 39.

And I realize I'm not serializing the timestamp appropriately (and how data with colons are bound to cause issues in JSON)... but this simple thing is what I haven't been able to solve.

Any guidance greatly appreciated.

Thanks -

Steve

P.S. For penance I will create some documentation of the working example and check it in somewhere useful or host a living doc on one of my websites to help others.

Steve Seremeth | Release Engineer
[Dealer.com]
steve.seremeth [at] dealer<mailto:{Message%20Sender}>
V : 877.327.8422 x 1391

FOLLOW US:
[Like Dealer.com]<http://www.facebook.com/DealerDotCom> [Follow Dealer.com] <https://twitter.com/#!/DealerDotCom> [Follow Dealer.com] <http://www.linkedin.com/company/dealer.com> [Dealer.com Channel] <http://www.youtube.com/user/DealerDotCom> [Dealer.com Blog] <http://www.dealer.com/blog.htm>


fobispo at isc

Mar 15, 2012, 2:43 PM

Post #2 of 4 (649 views)
Permalink
Re: Noob hoping for help encoding mysql datetime fields for JSON [In reply to]

try:


$_->created->ymd

created is most likely a DateTime object.

more info: perldoc DateTime

Francisco


On Mar 15, 2012, at 2:38 PM, Steve Seremeth wrote:

> Hello -
>
> I am semi new to Perl and Catalyst. I have walked through the Catalyst tutorial a couple times and have also picked up a couple books and done much googling that always seem to get me close but leave me hanging.
>
> Have been looking at tons of docs for catalyst::controller::rest and catalyst::view::json amongst many others and can't find joy.
>
> Anyway -- I think I would be on the right track if someone solved this basic inquiry:
>
> Working from this example:
> http://www.catalystframework.org/calendar/2009/22# An AJAX CRUD Interface with Catalyst and jQuery
>
> which I have working fine, fwiw...
>
> How would you expose the "created" column from the db (schema came from here: http://search.cpan.org/~bobtfish/Catalyst-Manual-5.9003/lib/Catalyst/Manual/Tutorial/04_BasicCRUD.pod and is, in this example, a sqlite TIMESTAMP col) in the JSON generated by the API controller's grid_POST method?
>
> If I simply add the column to the API controller here:
>
> <snip>
> $data{rows} = [.
> map { +{
> id => $_->id,
> cell => [.
> $_->id,
> $_->title,
> $_->rating,
> $_->author_list,
> $_->created,
> ]
> } } $paged_rs->all
> ];
> </snip>
>
> The app throws this:
>
> Content-Type application/json had a problem with your
> request.
>
> ***ERROR***
> encountered object '2012-02-29T17:16:27', but neither
> allow_blessed enabled nor TO_JSON method available on it at
> /usr/local/share/perl/5.12.4/Catalyst/Action/Serialize/JSON.pm
> line 39.
>
>
> And I realize I'm not serializing the timestamp appropriately (and how data with colons are bound to cause issues in JSON)... but this simple thing is what I haven't been able to solve.
>
> Any guidance greatly appreciated.
>
> Thanks -
>
> Steve
>
> P.S. For penance I will create some documentation of the working example and check it in somewhere useful or host a living doc on one of my websites to help others.
>
> Steve Seremeth | Release Engineer
>
> steve.seremeth [at] dealer
> V : 877.327.8422 x 1391
>
> FOLLOW US:
>
>
>
> _______________________________________________
> List: Catalyst [at] lists
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
> Dev site: http://dev.catalyst.perl.org/

Francisco Obispo
email: fobispo [at] isc
Phone: +1 650 423 1374 || INOC-DBA *3557* NOC
PGP KeyID = B38DB1BE


_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


bobtfish at bobtfish

Mar 16, 2012, 1:10 AM

Post #3 of 4 (652 views)
Permalink
Re: Noob hoping for help encoding mysql datetime fields for JSON [In reply to]

On 15 Mar 2012, at 21:38, Steve Seremeth wrote:
>
> If I simply add the column to the API controller here:
>
> <snip>
> $data{rows} = [.
> map { +{
> id => $_->id,
> cell => [.
> $_->id,
> $_->title,
> $_->rating,
> $_->author_list,
> $_->created,
> ]
> } } $paged_rs->all
> ];
> </snip>
>
> The app throws this:
>
> Content-Type application/json had a problem with your
> request.
>
> ***ERROR***
> encountered object '2012-02-29T17:16:27', but neither
> allow_blessed enabled nor TO_JSON method available on it at
> /usr/local/share/perl/5.12.4/Catalyst/Action/Serialize/JSON.pm
> line 39.
>
>
> And I realize I'm not serializing the timestamp appropriately (and how data with colons are bound to cause issues in JSON)... but this simple thing is what I haven't been able to solve.

The JSON encoder will handle escaping for you, so colons, quotes, backslashes or whatever aren't an issue.

What's happening here is that you have a 'DateTime' object, and the JSON encoder is puking on encoding it, as it's an object.

DateTime objects stringily by default, which is why you get: encountered object '2012-02-29T17:16:27'.. This error message is crap really - it doesn't make it clear that it's an object which has been stringified, and doesn't tell you which class is at fault!!

So, the simplest fix is:

> cell => [.
> $_->id,
> $_->title,
> $_->rating,
> $_->author_list,
> $_->created."",
> ]

Adding the ."" means that the object gets explicitly stringified, using whatever the default formatter is (giving you something ISO8601ish by default).

You can, of course, be a little more creative / flexible with the formatting if you want or need to be - checkout the DateTime::Format docs :)

Cheers
t0m


_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


steve.seremeth at dealer

Mar 19, 2012, 8:17 PM

Post #4 of 4 (641 views)
Permalink
Re: Noob hoping for help encoding mysql datetime fields for JSON [In reply to]

´╗┐You guys were right - it was a DateTime object - thank you very much for
clearing this up.

Steve

On 03/15/2012 05:43 PM, Francisco Obispo wrote:
> try:
>
>
> $_->created->ymd
>
> created is most likely a DateTime object.
>
> more info: perldoc DateTime
>
> Francisco
>
>
> On Mar 15, 2012, at 2:38 PM, Steve Seremeth wrote:
>
>> Hello -
>>
>> I am semi new to Perl and Catalyst. I have walked through the Catalyst tutorial a couple times and have also picked up a couple books and done much googling that always seem to get me close but leave me hanging.
>>
>> Have been looking at tons of docs for catalyst::controller::rest and catalyst::view::json amongst many others and can't find joy.
>>
>> Anyway -- I think I would be on the right track if someone solved this basic inquiry:
>>
>> Working from this example:
>> http://www.catalystframework.org/calendar/2009/22# An AJAX CRUD Interface with Catalyst and jQuery
>>
>> which I have working fine, fwiw...
>>
>> How would you expose the "created" column from the db (schema came from here: http://search.cpan.org/~bobtfish/Catalyst-Manual-5.9003/lib/Catalyst/Manual/Tutorial/04_BasicCRUD.pod and is, in this example, a sqlite TIMESTAMP col) in the JSON generated by the API controller's grid_POST method?
>>
>> If I simply add the column to the API controller here:
>>
>> <snip>
>> $data{rows} = [.
>> map { +{
>> id => $_->id,
>> cell => [.
>> $_->id,
>> $_->title,
>> $_->rating,
>> $_->author_list,
>> $_->created,
>> ]
>> } } $paged_rs->all
>> ];
>> </snip>
>>
>> The app throws this:
>>
>> Content-Type application/json had a problem with your
>> request.
>>
>> ***ERROR***
>> encountered object '2012-02-29T17:16:27', but neither
>> allow_blessed enabled nor TO_JSON method available on it at
>> /usr/local/share/perl/5.12.4/Catalyst/Action/Serialize/JSON.pm
>> line 39.
>>
>>
>> And I realize I'm not serializing the timestamp appropriately (and how data with colons are bound to cause issues in JSON)... but this simple thing is what I haven't been able to solve.
>>
>> Any guidance greatly appreciated.
>>
>> Thanks -
>>
>> Steve
>>
>> P.S. For penance I will create some documentation of the working example and check it in somewhere useful or host a living doc on one of my websites to help others.
>>
>> Steve Seremeth | Release Engineer
>>
>> steve.seremeth [at] dealer
>> V : 877.327.8422 x 1391
>>
>> FOLLOW US:
>>
>>
>>
>>


Steve Seremeth | Release Engineer
V: 877.327.8422 x 1391
mailto:Steve.Seremeth [at] dealer | www.dealer.com


_______________________________________________
>> List: Catalyst [at] lists
>> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
>> Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
>> Dev site: http://dev.catalyst.perl.org/
> Francisco Obispo
> email: fobispo [at] isc
> Phone: +1 650 423 1374 || INOC-DBA *3557* NOC
> PGP KeyID = B38DB1BE
>
>
> _______________________________________________
> List: Catalyst [at] lists
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
> Dev site: http://dev.catalyst.perl.org/


_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/

Catalyst users RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.