Skip Menu |

This queue is for tickets about the Data-Google-Visualization-DataTable CPAN distribution.

Report information
The Basics
Id: 67974
Status: resolved
Priority: 0/
Queue: Data-Google-Visualization-DataTable

People
Owner: Nobody in particular
Requestors: DRLARO [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Important
Broken in: 0.07
Fixed in: (no value)



Subject: Invalid JSON output when date objects are used
It the DataTable contains date or datetime objects invalid JSON is generated by Data::Google::Visualization::DataTable To see an example of this run the attached script. Expected Result: JSON parsed Actual Result: $ ./Google_data_table_date_test.pm Error parsing JSON: 'null' expected, at character offset 132 (before "new Date( 2010, 9, 1...") at ./Google_data_table_date_test.pm line 32. Note this JSON output is also rejected by http://www.jslint.com/
Subject: Google_data_table_date_test.pm
#!/usr/bin/perl -w use Data::Google::Visualization::DataTable; use DateTime; use JSON::XS; use strict; my $datatable = Data::Google::Visualization::DataTable->new(); $datatable->add_columns( { id => 'date', label => "A Date", type => 'date', p => {} }, { id => 'number', label => "Number", type => 'number' }, ); my $date = DateTime->new( year => 2010, month => 10, day => 16 ); $datatable->add_rows( { date => $date, number => 100, } ); my $output = $datatable->output_json(); #print "$output\n"; eval { my $json_ref = decode_json( $output ); }; if ( $@ ) { print "Error parsing JSON:\n$@ \n"; } else { print "JSON parsed\n"; }
I've attached a patch for this bug. It was only necessary to make a one line change to DataTable.pm. With the add_rows method change $json_date = 'new Date( ' . $json_date . ' )'; to $json_date = '"Date( ' . $json_date . ' )"'; The attached patch makes this simple change to fix this bug. Thanks, Dave On Thu May 05 18:01:19 2011, DRLARO wrote: Show quoted text
> It the DataTable contains date or datetime objects invalid JSON is > generated by Data::Google::Visualization::DataTable > > To see an example of this run the attached script. > > Expected Result: > JSON parsed > > Actual Result: > > $ ./Google_data_table_date_test.pm > Error parsing JSON: > 'null' expected, at character offset 132 (before "new Date( 2010, 9, > 1...") at ./Google_data_table_date_test.pm line 32. > > Note this JSON output is also rejected by http://www.jslint.com/ > >
Subject: DataTable.patch
--- DataTable.pm 2011-05-05 18:08:31.176712001 -0400 +++ DataTable_new.pm 2011-05-05 18:07:31.276712001 -0400 @@ -475,7 +475,7 @@ if ( $type eq 'timeofday' ) { $json_date = '[' . $json_date . ']'; } else { - $json_date = 'new Date( ' . $json_date . ' )'; + $json_date = '"Date( ' . $json_date . ' )"'; } my $placeholder = '%%%PLEHLDER%%%';
Hi David, This is expected behaviour, although perhaps the documentation didn't make it clear enough, and the method name is badly named. On that front, I've uploaded a new version which resolves both of those issues: https://github.com/sheriff/data-google-visualization-datatable-perl/commit/998728e7b586c588fad6019ff54608149ae3bc7a From that, concerning the issue you're having: +=head1 JSON vs Javascript + +Please note this module outputs Javascript, and not JSON. JSON is a subset of Javascript, +and Google's API requires a similar - but different - subset of Javascript. Specifically +some values need to be set to native Javascript objects, such as (and currently limited to) +the Date object. That means we output code like: + + {"v":new Date( 2011, 2, 21, 2, 6, 25 )} + +which is valid Javascript, but not valid JSON. + While your patch causes the module to output valid JSON, it breaks the Date functionality for the Google API itself, which is expecting native Date objects. Thanks for bringing this issue to my attention - hopefully the documentation patch and change of default method name rectify this. -P