1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
=head1 PURPOSE
Test C<jpath_map> exported function.
=head1 AUTHOR
Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
=head1 COPYRIGHT AND LICENCE
Copyright 2012-2013 Toby Inkster.
This module is tri-licensed. It is available under the X11 (a.k.a. MIT)
licence; you can also redistribute it and/or modify it under the same
terms as Perl itself.
=cut
use Test2::V0;
use JSON::Path -all;
use JSON::MaybeXS;
my $object = decode_json(<<'JSON');
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
},
"boolean": false
}
}
JSON
my $path1 = '$.store.book[*].title';
jpath_map { uc $_ } $object, '$.store.book[*].title';
is( [ jpath1( $object, $path1 ) ], [ map uc, 'Sayings of the Century' ], );
is(
[ jpath( $object, $path1 ) ],
[ map uc, 'Sayings of the Century', 'Sword of Honour', 'Moby Dick', 'The Lord of the Rings' ],
);
is( JSON::Path->new('$.store.book[*].author')->set( $object => 'Anon', 2 ), 2, );
is( [ jpath( $object, '$.store.book[*].author' ) ], [ 'Anon', 'Anon', 'Herman Melville', 'J. R. R. Tolkien' ], );
done_testing();
|