[Catalyst] need JSON converter which supports unquoted strings
Moritz Onken
onken at houseofdesign.de
Mon Nov 19 15:14:50 GMT 2007
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.
I want a clean solution which allows me to pass a string ref which
doesn't get quoted...
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.
>
More information about the Catalyst
mailing list