| 1 | package JSON::T; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.010; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use utf8; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Class::Load qw[]; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use JSON qw[]; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Scalar::Util qw[]; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Object::AUTHORITY; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use overload '""' => \&_to_string; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | BEGIN | ||||||||||||||||||||||||||||||||
| 16 | { | ||||||||||||||||||||||||||||||||
| 17 | 1 | ▶ | $JSON::T::AUTHORITY = 'cpan:TOBYINK'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 18 | 1 | $JSON::T::VERSION = '0.103'; | |||||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | 1 | our ($JSLIB, @Implementations); | |||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | sub _load_lib | ||||||||||||||||||||||||||||||||
| 24 | { | ||||||||||||||||||||||||||||||||
| 25 | 1 | ▶ | unless ($JSLIB) | ||||||||||||||||||||||||||||||
|
Subroutine
_load_lib: called CC=2 SCAR=7.1
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 26 | { | ||||||||||||||||||||||||||||||||
| 27 | 1 | local $/ = undef; | |||||||||||||||||||||||||||||||
| 28 | 1 | $JSLIB = <DATA>; | |||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||
| 32 | BEGIN | ||||||||||||||||||||||||||||||||
| 33 | { | ||||||||||||||||||||||||||||||||
| 34 | 1 | ▶ | push @Implementations, qw/JSON::T::JE JSON::T::SpiderMonkey/; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||
| 37 | { | ||||||||||||||||||||||||||||||||
| 38 | 1 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 39 | sub DOES | ||||||||||||||||||||||||||||||||
| 40 | { | ||||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | my ($class, $role) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
DOES: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | return $role if $role eq 'XML::Saxon::XSLT2'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 43 | 0 | return $class->SUPER::DOES($role); | |||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | sub new | ||||||||||||||||||||||||||||||||
| 48 | 1 | { | |||||||||||||||||||||||||||||||
| 49 | 1 | ▶ | my ($class, $transformation_code, $transformation_name) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=21.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 50 | 1 | ▶ | $transformation_name ||= '_main'; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$transformation_name ||= "_main"
MC/DC0%0/1
$transformation_name ||= "_main"
$transformation_name
Truth table50%1/2
A
A= $transformation_name
| |||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | 1 | my $impl_class; | |||||||||||||||||||||||||||||||
| 53 | 1 | ▶ | if ($class eq __PACKAGE__) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 54 | { | ||||||||||||||||||||||||||||||||
| 55 | 1 | IMPL: for my $i (@Implementations) | |||||||||||||||||||||||||||||||
| 56 | { | ||||||||||||||||||||||||||||||||
| 57 | 1 | ▶ | if (Class::Load::is_class_loaded($i) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
Class::Load::is_class_loaded($i) or Class::Load::try_load_class($i)
MC/DC0%0/2
Class::Load::is_class_loaded($i) or Class::Load::try_load_class($i)
Class::Load::is_class_loaded($i)
Class::Load::try_load_class($i)
Truth table33%1/3
A or B
A= Class::Load::is_class_loaded($i)B= Class::Load::try_load_class($i)
| |||||||||||||||||||||||||||||||||
| 58 | or Class::Load::try_load_class($i)) | ||||||||||||||||||||||||||||||||
| 59 | { | ||||||||||||||||||||||||||||||||
| 60 | 1 | $impl_class = $i; | |||||||||||||||||||||||||||||||
| 61 | 1 | last IMPL; | |||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||
| 65 | # let Class::Load break the bad news | ||||||||||||||||||||||||||||||||
| 66 | 1 | ▶ | unless (defined $impl_class) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 67 | { | ||||||||||||||||||||||||||||||||
| 68 | 0 | $impl_class = $Implementations[0]; | |||||||||||||||||||||||||||||||
| 69 | 0 | Class::Load::load_class($impl_class); | |||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||
| 72 | else | ||||||||||||||||||||||||||||||||
| 73 | { | ||||||||||||||||||||||||||||||||
| 74 | 0 | $impl_class = $class; | |||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | 1 | my $self = bless { | |||||||||||||||||||||||||||||||
| 78 | code => $transformation_code , | ||||||||||||||||||||||||||||||||
| 79 | name => $transformation_name , | ||||||||||||||||||||||||||||||||
| 80 | messages => [], | ||||||||||||||||||||||||||||||||
| 81 | }, $impl_class; | ||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | 1 | $self->init; | |||||||||||||||||||||||||||||||
| 84 | 1 | $self->engine_eval($transformation_code); | |||||||||||||||||||||||||||||||
| 85 | 1 | return $self; | |||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | sub init | ||||||||||||||||||||||||||||||||
| 89 | { | ||||||||||||||||||||||||||||||||
| 90 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 91 | 1 | _load_lib; | |||||||||||||||||||||||||||||||
| 92 | 1 | $self->engine_eval($JSLIB); | |||||||||||||||||||||||||||||||
| 93 | 1 | return $self; | |||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||
| 96 | 0 | ▶ | sub engine_eval { die "must be implemented by subclass" } | ||||||||||||||||||||||||||||||
|
Subroutine
engine_eval: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 97 | 0 | ▶ | sub parameters { warn "not implemented by subclass" } # non-fatal | ||||||||||||||||||||||||||||||
|
Subroutine
parameters: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||
| 99 | sub _accept_return_value | ||||||||||||||||||||||||||||||||
| 100 | { | ||||||||||||||||||||||||||||||||
| 101 | 1 | ▶ | my ($self, $value) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_accept_return_value: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 102 | 1 | $self->{return_value} = $value; | |||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||
| 105 | sub _last_return_value | ||||||||||||||||||||||||||||||||
| 106 | { | ||||||||||||||||||||||||||||||||
| 107 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_last_return_value: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 108 | 1 | $self->{return_value}; | |||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||
| 111 | sub _to_string | ||||||||||||||||||||||||||||||||
| 112 | { | ||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_to_string: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 114 | 0 | return 'JsonT:#'.$self->{'name'}; | |||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||
| 117 | sub transform | ||||||||||||||||||||||||||||||||
| 118 | { | ||||||||||||||||||||||||||||||||
| 119 | 1 | ▶ | my ($self, $input) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
transform: called CC=5 SCAR=19.6
Pod
documented
| |||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||
| 121 | 1 | ▶ | if (Scalar::Util::blessed($input) and $input->isa('JSON::JOM::Node')) | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
builtin::blessed($input) and $input->isa("JSON::JOM::Node")
MC/DC0%0/2
builtin::blessed($input) and $input->isa("JSON::JOM::Node")
builtin::blessed($input)
$input->isa("JSON::JOM::Node")
Truth table33%1/3
A and B
A= builtin::blessed($input)B= $input->isa("JSON::JOM::Node")
| |||||||||||||||||||||||||||||||||
| 122 | { | ||||||||||||||||||||||||||||||||
| 123 | 0 | $input = JSON::to_json($input, {convert_blessed=>1}); | |||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||
| 125 | elsif (ref $input) | ||||||||||||||||||||||||||||||||
| 126 | { | ||||||||||||||||||||||||||||||||
| 127 | 0 | $input = JSON::to_json($input); | |||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||
| 130 | 1 | my $name = $self->{'name'}; | |||||||||||||||||||||||||||||||
| 131 | 1 | my $rv1 = $self->engine_eval("return_to_perl(JSON.transform($input, $name));"); | |||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||
| 133 | 1 | ▶ | return ($self->_last_return_value//'').''; # stringify | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->_last_return_value // ""
MC/DC0%0/1
$self->_last_return_value // ""
$self->_last_return_value
Truth table50%1/2
A
A= $self->_last_return_value
| |||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||
| 136 | sub transform_structure | ||||||||||||||||||||||||||||||||
| 137 | { | ||||||||||||||||||||||||||||||||
| 138 | 1 | ▶ | my ($self, $input, $debug) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
transform_structure: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 139 | 1 | my $output = $self->transform($input); | |||||||||||||||||||||||||||||||
| 140 | 1 | ▶ | eval 'use Test::More; Test::More::diag("\n${output}\n");' | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 141 | if $debug; | ||||||||||||||||||||||||||||||||
| 142 | 1 | return JSON::from_json($output); | |||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||
| 144 | 1 | *transform_document = \&transform_structure; | |||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||
| 146 | # none of this is useful, but provided for XML::Saxon::XSLT2 compat. | ||||||||||||||||||||||||||||||||
| 147 | sub messages | ||||||||||||||||||||||||||||||||
| 148 | { | ||||||||||||||||||||||||||||||||
| 149 | 0 | ▶ | return; | ||||||||||||||||||||||||||||||
|
Subroutine
messages: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||
| 151 | sub media_type | ||||||||||||||||||||||||||||||||
| 152 | { | ||||||||||||||||||||||||||||||||
| 153 | 0 | ▶ | my ($self, $default) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
media_type: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 154 | 0 | return $default; | |||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||
| 156 | 1 | *version = \&media_type; | |||||||||||||||||||||||||||||||
| 157 | 1 | *doctype_system = \&media_type; | |||||||||||||||||||||||||||||||
| 158 | 1 | *doctype_public = \&media_type; | |||||||||||||||||||||||||||||||
| 159 | 1 | *encoding = \&media_type; | |||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||
| 161 | 1 | 1; | |||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||
| 163 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||
| 165 | JSON::T - transform JSON using JsonT | ||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||
| 167 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||
| 169 | my $jsont = slurp('foo/bar.js'); | ||||||||||||||||||||||||||||||||
| 170 | my $input = slurp('foo/quux.json'); | ||||||||||||||||||||||||||||||||
| 171 | my $JSONT = JSON::T->new($jsont); | ||||||||||||||||||||||||||||||||
| 172 | print $JSONT->transform($input); | ||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||
| 174 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||
| 176 | This module implements JsonT, a language for transforming JSON-like | ||||||||||||||||||||||||||||||||
| 177 | structures, analogous to XSLT in the XML world. | ||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||
| 179 | JsonT is described at L<http://goessner.net/articles/jsont/>. JsonT is | ||||||||||||||||||||||||||||||||
| 180 | a profile of Javascript; so JsonT needs a Javascript engine to actually | ||||||||||||||||||||||||||||||||
| 181 | work. This module provides the engine-neutral stuff, while L<JSON::T::JE> | ||||||||||||||||||||||||||||||||
| 182 | and L<JSON::T::SpiderMonkey> provide the necessary glue to hook it up to | ||||||||||||||||||||||||||||||||
| 183 | a couple of Javascript engines. | ||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||
| 185 | JSON::T::JE uses the pure Perl Javascript implementation L<JE>. | ||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||
| 187 | JSON::T::SpiderMonkey uses L<JavaScript::SpiderMonkey> which in turn is | ||||||||||||||||||||||||||||||||
| 188 | backed by Mozilla's libjs C library. | ||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||
| 190 | This module tries to provide a similar API to L<XML::Saxon::XSLT2>. | ||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||
| 192 | =head2 Constructor | ||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||
| 194 | =over 4 | ||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||
| 196 | =item C<< new($code, $name) >> | ||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||
| 198 | Constructs a new JSON::T transformation. $code is the JsonT Javascript | ||||||||||||||||||||||||||||||||
| 199 | code. As a JsonT file can contain multiple (potentially unrelated) | ||||||||||||||||||||||||||||||||
| 200 | transformations, the name of the particular transformation you want to | ||||||||||||||||||||||||||||||||
| 201 | use should also be provided. If $name is omitted, then the name "_main" | ||||||||||||||||||||||||||||||||
| 202 | is assumed. | ||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||
| 204 | If you wish to use a particular Javascript implementation, you can | ||||||||||||||||||||||||||||||||
| 205 | use, for example: | ||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||
| 207 | JSON::T::SpiderMonkey->new($code, $name) | ||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||
| 209 | Otherwise | ||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||
| 211 | JSON::T->new($code, $name) | ||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||
| 213 | will try to pick a working implementation for you. | ||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||
| 215 | =back | ||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||
| 217 | =head2 Methods | ||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||
| 219 | =over 4 | ||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||
| 221 | =item C<< parameters(param1=>$arg1, param2=>$arg2, ...) >> | ||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||
| 223 | Sets global variables available to the Javascript code. All arguments | ||||||||||||||||||||||||||||||||
| 224 | are treated as strings. | ||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||
| 226 | =item C<< transform($input) >> | ||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||
| 228 | Run the transformation. The input may be a JSON string, a JSON::JOM::Node | ||||||||||||||||||||||||||||||||
| 229 | or a native Perl nested arrayref/hashref structure, in which case it will be | ||||||||||||||||||||||||||||||||
| 230 | stringified using the JSON module's to_json function. The output (return value) | ||||||||||||||||||||||||||||||||
| 231 | will be a string. | ||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||
| 233 | =item C<< transform_structure($input) >> | ||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||
| 235 | Like C<transform>, but attempts to parse the output as a JSON string and | ||||||||||||||||||||||||||||||||
| 236 | return a native Perl arrayref/hashref structure. This method will fail | ||||||||||||||||||||||||||||||||
| 237 | if the output is not a JSON string. | ||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||
| 239 | =item C<< DOES($role) >> | ||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||
| 241 | Like L<UNIVERSAL>'s DOES method, but returns true for: | ||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||
| 243 | JSON::T->DOES('XML::Saxon::XSLT2') | ||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||
| 245 | as an aid for polymorphism. | ||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||
| 247 | =back | ||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||
| 249 | The following methods also exist for compatibility with XML::Saxon::XSLT2, | ||||||||||||||||||||||||||||||||
| 250 | but are mostly useless: | ||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||
| 252 | =over | ||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||
| 254 | =item C<< transform_document >> | ||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||
| 256 | =item C<< messages >> | ||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||
| 258 | =item C<< media_type >> | ||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||
| 260 | =item C<< version >> | ||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||
| 262 | =item C<< doctype_system >> | ||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||
| 264 | =item C<< doctype_public >> | ||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||
| 266 | =item C<< encoding >> | ||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||
| 268 | =back | ||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||
| 270 | =head2 Javascript Execution Environment | ||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||
| 272 | JSON::T is a profile of Javascript, so is evaluated in an execution | ||||||||||||||||||||||||||||||||
| 273 | environment. As this is not a browser environment, many global objects | ||||||||||||||||||||||||||||||||
| 274 | familiar to browser Javascript developers are not available. (For example, | ||||||||||||||||||||||||||||||||
| 275 | C<window>, C<document>, C<navigator>, etc.) | ||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||
| 277 | A single global object called "JSON" is provided with methods | ||||||||||||||||||||||||||||||||
| 278 | C<stringify> and C<parse> compatible with the well-known json2.js | ||||||||||||||||||||||||||||||||
| 279 | library (L<http://www.JSON.org/json2.js>), and a method | ||||||||||||||||||||||||||||||||
| 280 | C<transform(obj,jsont)> that provides a Javascript JsonT | ||||||||||||||||||||||||||||||||
| 281 | implementation. | ||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||
| 283 | A function C<print_to_perl> is provided which prints to Perl's | ||||||||||||||||||||||||||||||||
| 284 | STDOUT stream. | ||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||
| 286 | =head1 SUBCLASSING | ||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||
| 288 | Two subclasses are provided: L<JSON::T::JE> and L<JSON::T::SpiderMonkey>, | ||||||||||||||||||||||||||||||||
| 289 | but if you need to hook L<JSON::T> up to another Javascript engine, it is | ||||||||||||||||||||||||||||||||
| 290 | relatively simple. Just create a Perl class which is a subclass of L<JSON::T>. | ||||||||||||||||||||||||||||||||
| 291 | This subclass must implement two required methods and should implement | ||||||||||||||||||||||||||||||||
| 292 | one optional method. | ||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||
| 294 | =over | ||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||
| 296 | =item C<< init >> | ||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||
| 298 | Will be passed a newly created object (let's call it C<< $self >>). It is | ||||||||||||||||||||||||||||||||
| 299 | expected to initialise a Javascript execution context for C<< $self >>, and | ||||||||||||||||||||||||||||||||
| 300 | define two Javascript functions: C<return_to_perl> (which acts as a shim | ||||||||||||||||||||||||||||||||
| 301 | to C<< $self->_accept_return_value() >>) and C<print_to_perl> (which acts | ||||||||||||||||||||||||||||||||
| 302 | as a shim to C<print>). It must then call C<< SUPER::init >>. | ||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||
| 304 | =item C<< engine_eval >> | ||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||
| 306 | Will be passed an object (C<< $self >>) and a Javascript string. Must evaluate | ||||||||||||||||||||||||||||||||
| 307 | the string in the object's Javascript execution context. | ||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||
| 309 | =item C<< parameters >> | ||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||
| 311 | This one is optional to implement it. If you don't implement it, then users | ||||||||||||||||||||||||||||||||
| 312 | will get a warning message if they try to call C<< parameters >> on your | ||||||||||||||||||||||||||||||||
| 313 | subclass. | ||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||
| 315 | Will be passed an object (C<< $self >>) and a hash of parameters, using the | ||||||||||||||||||||||||||||||||
| 316 | following format: | ||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||
| 318 | ( | ||||||||||||||||||||||||||||||||
| 319 | name1 => 'value1', | ||||||||||||||||||||||||||||||||
| 320 | name2 => [ type2 => 'value2' ], | ||||||||||||||||||||||||||||||||
| 321 | name3 => [ type3 => 'value3', hint => 'hint value' ], | ||||||||||||||||||||||||||||||||
| 322 | ) | ||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||
| 324 | This should have the effect of setting: | ||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||
| 326 | var name1 = 'value1'; | ||||||||||||||||||||||||||||||||
| 327 | var name2 = 'value2'; | ||||||||||||||||||||||||||||||||
| 328 | var name3 = 'value3'; | ||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||
| 330 | in the object's Javascript execution context. Parameter types and additional | ||||||||||||||||||||||||||||||||
| 331 | hints may be used to set the correct types in Javascript. | ||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||
| 333 | =back | ||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||
| 335 | You are unlikely to need to do anything else when subclassing. | ||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||
| 337 | If you wish C<< JSON::T->new >> to know about your subclass, then push | ||||||||||||||||||||||||||||||||
| 338 | its name onto C<< @JSON::T::Implementations >>. | ||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||
| 340 | =head1 BUGS | ||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||
| 342 | Please report any bugs to L<http://rt.cpan.org/>. | ||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||
| 344 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||
| 346 | Specification: L<http://goessner.net/articles/jsont/>. | ||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||
| 348 | Related modules: L<JSON>, L<JSON::Path>, L<JSON::GRDDL>, | ||||||||||||||||||||||||||||||||
| 349 | L<JSON::Hyper>, L<JSON::Schema>. | ||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||
| 351 | JOM version: L<JSON::JOM>, L<JSON::JOM::Plugins::JsonT>. | ||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||
| 353 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||
| 355 | Toby Inkster E<lt>tobyink@cpan.orgE<gt>. | ||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||
| 357 | This module is embeds Stefan Goessner's Javascript implementation of | ||||||||||||||||||||||||||||||||
| 358 | JsonT (version 0.9) to do the heavy lifting. | ||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||
| 360 | =head1 COPYRIGHT AND LICENCE | ||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||
| 362 | Copyright 2006 Stefan Goessner. | ||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||
| 364 | Copyright 2008-2011, 2013 Toby Inkster. | ||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||
| 366 | Licensed under the Lesser GPL: | ||||||||||||||||||||||||||||||||
| 367 | L<http://creativecommons.org/licenses/LGPL/2.1/>. | ||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||
| 369 | =head1 DISCLAIMER OF WARRANTIES | ||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||
| 371 | THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED | ||||||||||||||||||||||||||||||||
| 372 | WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF | ||||||||||||||||||||||||||||||||
| 373 | MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||
| 375 | =cut | ||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||
| 377 | # Here's the Javascript... | ||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||
| 379 | __DATA__ | ||||||||||||||||||||||||||||||||