| 1 | #!/usr/bin/perl | ||||||||||
| 2 | |||||||||||
| 3 | package KiokuDB::Backend::Serialize::JSON; | ||||||||||
| 4 | 6 | ▶ | use Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 6 | ▶ | use IO::Handle; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 6 | ▶ | use namespace::clean -except => 'meta'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 6 | with qw( | |||||||||
| 11 | KiokuDB::Backend::Serialize | ||||||||||
| 12 | KiokuDB::Backend::Role::UnicodeSafe | ||||||||||
| 13 | KiokuDB::Backend::Serialize::JSPON | ||||||||||
| 14 | ); | ||||||||||
| 15 | |||||||||||
| 16 | 6 | has pretty => ( | |||||||||
| 17 | isa => "Bool", | ||||||||||
| 18 | is => "rw", | ||||||||||
| 19 | default => 0, | ||||||||||
| 20 | ); | ||||||||||
| 21 | |||||||||||
| 22 | 6 | has [qw(utf8 canonical)] => ( | |||||||||
| 23 | isa => "Bool", | ||||||||||
| 24 | is => "rw", | ||||||||||
| 25 | default => 1, | ||||||||||
| 26 | ); | ||||||||||
| 27 | |||||||||||
| 28 | 6 | has json => ( | |||||||||
| 29 | isa => "Object", | ||||||||||
| 30 | is => "rw", | ||||||||||
| 31 | lazy_build => 1, | ||||||||||
| 32 | handles => [qw(encode decode)], | ||||||||||
| 33 | ); | ||||||||||
| 34 | |||||||||||
| 35 | sub _build_json { | ||||||||||
| 36 | 17 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_build_json: called CC=3 SCAR=11.0
| |||||||||||
| 37 | |||||||||||
| 38 | 17 | my $json = JSON->new; | |||||||||
| 39 | |||||||||||
| 40 | 17 | foreach my $flag (qw(utf8 pretty canonical)) { | |||||||||
| 41 | 51 | ▶ | $json->$flag if $self->$flag; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | 17 | return $json; | |||||||||
| 45 | } | ||||||||||
| 46 | |||||||||||
| 47 | sub serialize { | ||||||||||
| 48 | 740 | ▶ | my ( $self, @args ) = @_; | ||||||||
|
Subroutine
serialize: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 49 | 740 | $self->encode( $self->collapse_jspon(@args) ); | |||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | sub deserialize { | ||||||||||
| 53 | 1747 | ▶ | my ( $self, $json, @args ) = @_; | ||||||||
|
Subroutine
deserialize: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 54 | 1747 | $self->expand_jspon( $self->decode($json), @args ); | |||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | sub serialize_to_stream { | ||||||||||
| 58 | 3 | ▶ | my ( $self, $fh, $entry ) = @_; | ||||||||
|
Subroutine
serialize_to_stream: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 59 | 3 | $fh->print( $self->serialize($entry) ); | |||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | sub deserialize_from_stream { | ||||||||||
| 63 | 2 | ▶ | my ( $self, $fh ) = @_; | ||||||||
|
Subroutine
deserialize_from_stream: called CC=4 SCAR=14.4
Pod
undocumented
| |||||||||||
| 64 | |||||||||||
| 65 | 2 | local $_; | |||||||||
| 66 | 2 | local $/ = \4096; | |||||||||
| 67 | |||||||||||
| 68 | 2 | my $json = $self->json; | |||||||||
| 69 | |||||||||||
| 70 | 2 | ▶ | while ( <$fh> ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 1 | ▶ | if ( my @docs = $json->incr_parse($_) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 1 | my @entries = map { $self->expand_jspon($_) } @docs; | |||||||||
| 73 | 1 | return @entries; | |||||||||
| 74 | } else { | ||||||||||
| 75 | 0 | ▶ | return if $fh->eof; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | } | ||||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | 1 | return; | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | 6 | __PACKAGE__ | |||||||||
| 83 | |||||||||||
| 84 | __END__ | ||||||||||
| 85 | |||||||||||
| 86 | =pod | ||||||||||
| 87 | |||||||||||
| 88 | =head1 NAME | ||||||||||
| 89 | |||||||||||
| 90 | KiokuDB::Backend::Serialize::JSON - Role to serialize entries to JSON strings | ||||||||||
| 91 | with L<KiokuDB::Backend::Serialize::JSPON> semantics | ||||||||||
| 92 | |||||||||||
| 93 | =head1 SYNOPSIS | ||||||||||
| 94 | |||||||||||
| 95 | with qw(KiokuDB::Backend::Serialize::JSON); | ||||||||||
| 96 | |||||||||||
| 97 | sub foo { | ||||||||||
| 98 | my ( $self, $entry ) = @_; | ||||||||||
| 99 | |||||||||||
| 100 | my $json_string = $self->serialize($entry); | ||||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | =head1 DESCRIPTION | ||||||||||
| 104 | |||||||||||
| 105 | This role provides additional convenience attributes and methods for backends | ||||||||||
| 106 | that encode entries to JSON strings, on top of | ||||||||||
| 107 | L<KiokuDB::Backend::Serialize::JSPON> which only restructures the data. | ||||||||||
| 108 | |||||||||||
| 109 | L<KiokuDB::Backend::Serialize::Delegate> is preferred to using this directly. | ||||||||||
| 110 | |||||||||||
| 111 | =head1 METHODS | ||||||||||
| 112 | |||||||||||
| 113 | =over 4 | ||||||||||
| 114 | |||||||||||
| 115 | =item serialize $entry | ||||||||||
| 116 | |||||||||||
| 117 | Returns a JSON string | ||||||||||
| 118 | |||||||||||
| 119 | =item deserialize $json_str | ||||||||||
| 120 | |||||||||||
| 121 | Returns a L<KiokuDB::Entry> | ||||||||||
| 122 | |||||||||||
| 123 | =back | ||||||||||
| 124 | |||||||||||
| 125 | =head1 ATTRIBUTES | ||||||||||
| 126 | |||||||||||
| 127 | =over 4 | ||||||||||
| 128 | |||||||||||
| 129 | =item json | ||||||||||
| 130 | |||||||||||
| 131 | The L<JSON> instance used to encode/decode the JSON. | ||||||||||
| 132 | |||||||||||
| 133 | =item pretty | ||||||||||
| 134 | |||||||||||
| 135 | Whether or not to pass the C<pretty> flag to the L<JSON> object after | ||||||||||
| 136 | construction. | ||||||||||
| 137 | |||||||||||
| 138 | =back | ||||||||||
| 139 | |||||||||||
| 140 | =cut | ||||||||||
| 141 | |||||||||||
| 142 | |||||||||||