| 1 | package autobox::JSON; | ||
| 2 | 2 | $autobox::JSON::VERSION = '0.0006'; | |
| 3 | 2 | ▶ | use 5.008; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 2 | ▶ | use parent 'autobox'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 2 | ▶ | sub import { |
|
Subroutine
import: called CC=1 SCAR=0.0
| |||
| 10 | 2 | my ($class) = @_; | |
| 11 | |||
| 12 | 2 | $class->SUPER::import( | |
| 13 | HASH => 'autobox::JSON::Ref', | ||
| 14 | ARRAY => 'autobox::JSON::Ref', | ||
| 15 | STRING => 'autobox::JSON::String', | ||
| 16 | ); | ||
| 17 | } | ||
| 18 | |||
| 19 | =head1 NAME | ||
| 20 | |||
| 21 | autobox::JSON - bringing JSON functions to autobox | ||
| 22 | |||
| 23 | =head1 VERSION | ||
| 24 | |||
| 25 | version 0.0006 | ||
| 26 | |||
| 27 | =head1 SYNOPSIS | ||
| 28 | |||
| 29 | use autobox::JSON; | ||
| 30 | |||
| 31 | say {name => 'Jim', age => 34}->encode_json; | ||
| 32 | # {"name":"Jim","age":46} | ||
| 33 | |||
| 34 | my $person = '{"name":"Jim","age":46}'->decode_json | ||
| 35 | # {name => 'Jim', age => 34} | ||
| 36 | |||
| 37 | my $serialized_person = $person->encode_json; | ||
| 38 | # {"name":"Jim","age":46} | ||
| 39 | |||
| 40 | # works on arrays too | ||
| 41 | [1, 2, 3, 4, 5]->encode_json; | ||
| 42 | |||
| 43 | =head1 METHODS | ||
| 44 | |||
| 45 | =head2 encode_json | ||
| 46 | |||
| 47 | This method behaves the same as L<JSON/encode_json>. | ||
| 48 | |||
| 49 | =head2 encode_json_pretty | ||
| 50 | |||
| 51 | This method is identical to L</encode_json>, except that it also "prettifies" | ||
| 52 | the output, making it easier for us mortals to read. This is useful | ||
| 53 | especially when dumping a JSON structure to something reasonable for, say, | ||
| 54 | debug or other purposes. | ||
| 55 | |||
| 56 | It is functionally the same as: | ||
| 57 | |||
| 58 | JSON->new->utf8->canonical->pretty->encode($ref) | ||
| 59 | |||
| 60 | =head2 decode_json | ||
| 61 | |||
| 62 | This method behaves the same as L<JSON/decode_json>. | ||
| 63 | |||
| 64 | =head1 DEPRECIATED METHODS | ||
| 65 | |||
| 66 | =head2 to_json (depreciated) | ||
| 67 | |||
| 68 | This method behaves the same as L<JSON/to_json>. | ||
| 69 | |||
| 70 | This method is depreciated because the JSON documentation itself | ||
| 71 | prefers encode_json. | ||
| 72 | |||
| 73 | =head2 from_json (depreciated) | ||
| 74 | |||
| 75 | This method behaves the same as L<JSON/from_json>. | ||
| 76 | |||
| 77 | This method is depreciated as the JSON documentation itself | ||
| 78 | prefers decode_json. | ||
| 79 | |||
| 80 | =head1 SEE ALSO | ||
| 81 | |||
| 82 | C<autobox> C<JSON> C<autobox::Core> | ||
| 83 | |||
| 84 | =head1 AUTHOR | ||
| 85 | |||
| 86 | Robin Edwards, E<lt>robin.ge@gmail.comE<gt> | ||
| 87 | |||
| 88 | L<http://github.com/robinedwards/> | ||
| 89 | |||
| 90 | =head1 COPYRIGHT AND LICENSE | ||
| 91 | |||
| 92 | Copyright (C) 2011 by Robin Edwards | ||
| 93 | |||
| 94 | This library is free software; you can redistribute it and/or modify | ||
| 95 | it under the same terms as Perl itself, either Perl version 5.12.3 or, | ||
| 96 | at your option, any later version of Perl 5 you may have available. | ||
| 97 | |||
| 98 | =cut | ||
| 99 | |||
| 100 | package autobox::JSON::String; | ||
| 101 | 2 | $autobox::JSON::String::VERSION = '0.0006'; | |
| 102 | 2 | require JSON; | |
| 103 | 3 | ▶ | sub from_json { JSON::from_json(shift); } |
|
Subroutine
from_json: called CC=1 SCAR=0.0
| |||
| 104 | 3 | ▶ | sub decode_json { JSON::decode_json(shift); } |
|
Subroutine
decode_json: called CC=1 SCAR=0.0
| |||
| 105 | |||
| 106 | package autobox::JSON::Ref; | ||
| 107 | 2 | $autobox::JSON::Ref::VERSION = '0.0006'; | |
| 108 | 2 | require JSON; | |
| 109 | 3 | ▶ | sub to_json { JSON::to_json(shift); } |
|
Subroutine
to_json: called CC=1 SCAR=0.0
| |||
| 110 | 3 | ▶ | sub encode_json { JSON::encode_json(shift); } |
|
Subroutine
encode_json: called CC=1 SCAR=0.0
| |||
| 111 | 1 | ▶ | sub encode_json_pretty { JSON->new->utf8->canonical->pretty->encode(shift); } |
|
Subroutine
encode_json_pretty: called CC=1 SCAR=0.0
| |||
| 112 | |||
| 113 | 2 | 1; | |