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.

--
Jason Kohles, RHCA RHCDS RHCE
[EMAIL PROTECTED] - http://www.jasonkohles.com/
"A witty saying proves nothing."  -- Voltaire



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

Reply via email to