Where can I find the Perl module to convert Perl data structure to JavaScript?

Where can I find the Perl module to convert Perl data structure to JavaScript?

eg. this is my code (Mason):

% # convert our @cti data structure into a javascript one var cti = [ % foreach my $cti_category (@cti) { { label: "<% $cti_category->{'label'} %>", value: "<% $cti_category->{'value'} %>", children: [ % foreach my $cti_type (@{$cti_category->{'children'}}) { { label: "<% $cti_type->{'label'} %>", value: "<% $cti_type->{'value'} %>", }, % } ] }, % } ]; 

Is there a module for this?

+4
source share
4 answers

JSON stands for JavaScript Object Notation whose format you are looking for.

Unfortunately, none of the modules you are looking for are in the Perl core, but they are available in CPAN, as a quick search will show.

I would recommend setting JSON :: Any as a wrapper, as well as JSON :: XS (if you have a C compiler) or one of JSON and JSON :: Syck if you don't. JSON :: Any provides an interface class on top of several other JSON modules (you can choose or let choose from installed), which do not depend on which module you use. Thus, if your code needs to be ported elsewhere and, say, the target computer can install JSON :: XS when you cannot, you get a performance boost without any additional code.

 use JSON::Any; my $j = JSON::Any->new; $json = $j->objToJson($perl_data); 

Same.

+15
source

Check out JSON or JSON :: XS .

To develop a little more, JSON is "JavaScript Object Naming", and the two modules above convert perl data structures to this format.

+7
source

JSON !

This module converts Perl data structures to JSON and vice versa, using either JSON :: XS or JSON :: PP.

0
source

The JSON module converts data structures - mostly to / from a JSON serializer.

0
source

Source: https://habr.com/ru/post/1277177/


All Articles