[Catalyst] need JSON converter which supports unquoted strings

Jason Kohles email at jasonkohles.com
Mon Nov 19 15:35:01 GMT 2007


On Nov 19, 2007, at 10:14 AM, Moritz Onken wrote:

> I tried this approach, too. But I don't like the XXX-Handler replace
> story :-)
>
> If you have a big function with a lot of braces and stuff your regex
> brakes.
>
True, but in that case I would put that function in a .js and then  
just call it from the kludged JSON.

> I want a clean solution which allows me to pass a string ref which
> doesn't get quoted...
>
Then what you want isn't JSON, which doesn't allow unquoted strings  
(except true and false).

>
> Am Montag, den 19.11.2007, 09:57 -0500 schrieb Jason Kohles:
>> On Nov 19, 2007, at 9:20 AM, Moritz Onken wrote:
>>
>>> Hi,
>>>
>>> sorry, a bit off topic, but I'm looking for a perl-object-to-JSON
>>> converter which supports unquoted strings.
>>> Modules like JSON::Any quote everything except true and false. But I
>>> need to pass unquoted strings to let JavaScript run some functions.
>>>
>>> Any suggestions?
>>>
>> Not sure if it will help in your application, but I ran into a  
>> similar
>> issue recently with trying to generate a javascript data structure to
>> define a cascading menu (which would get turned into menus for the
>> ExtJS toolbar object.)  One of the elements in the data structure
>> needed to be a javascript callback that should be run when the menu
>> option was selected.  I wanted to have an 'href' field in my objects
>> that defined the location the user should be redirected to when they
>> picked that menu item.  The way I handled it was to use JSON::XS  
>> which
>> lets you define a TO_JSON method for objects that are being
>> serialized, and then post-processed the output a little bit...
>>
>> sub TO_JSON {
>> 	my ( $self ) = @_;
>>
>> 	my %hash = %{ $self };
>>
>> 	# Here we wrap XXX-HANDLER() around the value that
>> 	# is in the handler field of the object
>> 	$hash{ 'handler' } = "XXX-HANDLER(".delete( $hash{ 'href' } ).")";
>>
>> 	return \%hash;
>> }
>>
>> sub render {
>> 	my ( $self ) = @_;
>>
>> 	my $json = JSON::XS->new->ascii->convert_blessed;
>> 	my $out = $json->encode( [ $self->children ] );
>> 	$out =~ s/"XXX-HANDLER\((.*?)\)/function()
>> {document.location.href='$1'}/g;
>>
>> 	return "var mainMenuItems = $out;";	
>> }
>>
>>
>> It's a little kludgy, but it works.  I'm probably going to write this
>> whole package up as a blog entry, if anybody is interested in it.   
>> The
>> reason for this was to allow controllers to register the menu items
>> for their actions, and have a cascading menu built automatically from
>> the controllers.
>>
>
>
> _______________________________________________
> List: Catalyst at lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/catalyst@lists.rawmode.org/
> Dev site: http://dev.catalyst.perl.org/
>

-- 
Jason Kohles, RHCA RHCDS RHCE
email at jasonkohles.com - http://www.jasonkohles.com/
"A witty saying proves nothing."  -- Voltaire





More information about the Catalyst mailing list