File Coverage

blib/lib/Data/Transit.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package Data::Transit;
2 4     4   54990 use strict;
  4         9  
  4         126  
3 4     4   15 use warnings;
  4         4  
  4         89  
4 4     4   12 no warnings 'uninitialized';
  4         6  
  4         92  
5              
6 4     4   16 use Carp qw(confess);
  4         5  
  4         255  
7 4     4   1073 use Data::Transit::Reader::JSON;
  0            
  0            
8             use Data::Transit::Reader::MessagePack;
9             use Data::Transit::Reader::JSONVerbose;
10             use Data::Transit::Writer::JSON;
11             use Data::Transit::Writer::JSONVerbose;
12             use Data::Transit::Writer::MessagePack;
13              
14             =head1 NAME
15              
16             Data::Transit - Perl implementation of the transit format
17              
18             =head1 VERSION
19              
20             Version 0.8.04
21              
22             =cut
23              
24             our $VERSION = '0.8.04';
25              
26             =head1 SYNOPSIS
27              
28             use Data::Transit;
29              
30             my $writer = Data::Transit::writer($fh, 'json');
31             $writer->write($value);
32              
33             my $reader = Data::Transit::reader('json');
34             my $val = $reader->read($value);
35              
36             For example:
37              
38             use Data::Transit;
39              
40             my $output;
41             open my ($output_fh), '>>', \$output;
42             my $writer = Data::Transit::writer($fh, 'json');
43             $writer->write(["abc", 12345]);
44              
45             my $reader = Data::Transit::reader('json');
46             my $vals = $reader->read($output);
47              
48             Instead of json, you may also provide json-verbose and message-pack;
49              
50             =head1 Type Mappings
51              
52             Perl converts a lot of different types into basic strings, and keys in maps have to be strings. As a result, the only way to fully avoid key collisions is to have some sort of naming scheme, but this violates the spirit of Transit. Put another way, we're accepting the possibility of collisions in exchange for something that maps more closely to idiomatic perl.
53              
54             In an effort to keep the dependencies of this library to a minimum, any types that correspond to something outside of perls core modules has been excluded. If demand becomes high enough, I will write a separate package to extend heavily into CPAN types.
55              
56             =head2 Custom Types
57              
58             Custom types are registered at when the write/read handler is created:
59              
60             package Point;
61              
62             sub new {
63             my ($class, $x, $y) = @_;
64             return bless {x => $x, y => $y}, $class;
65             }
66              
67             package PointWriteHandler;
68              
69             sub new {
70             my ($class, $verbose) = @_;
71             return bless {verbose => $verbose}, $class;
72             }
73              
74             sub tag {
75             return 'point';
76             }
77              
78             sub rep {
79             my ($self, $p) = @_;
80             return [$p->{x},$p->{y}] if $self->{verbose};
81             return "$p->{x},$p->{y}";
82             }
83              
84             sub stringRep {
85             return undef;
86             }
87              
88             sub getVerboseHandler {
89             return __PACKAGE__->new(1);
90             }
91              
92             package PointReadHandler;
93              
94             sub new {
95             my ($class, $verbose) = @_;
96             return bless {
97             verbose => $verbose,
98             }, $class;
99             }
100              
101             sub fromRep {
102             my ($self, $rep) = @_;
103             return Point->new(@$rep) if $self->{verbose};
104             return Point->new(split /,/,$rep);
105             }
106              
107             sub getVerboseHandler {
108             return __PACKAGE__->new(1);
109             }
110              
111             package main;
112              
113             my $point = Point->new(2,3);
114              
115             my $output;
116             open my ($output_fh), '>>', \$output;
117             Data::Transit::writer("json", $output_fh, handlers => {
118             Point => PointWriteHandler->new(),
119             })->write($point);
120              
121             my $result = Data::Transit::reader("json", handlers => {
122             point => PointReadHandler->new(),
123             })->read($output);
124              
125             is_deeply($point, $result);# true
126              
127             =cut
128              
129             sub reader {
130             my ($format, %args) = @_;
131             return Data::Transit::Reader::JSON->new(%args) if $format eq 'json';
132             return Data::Transit::Reader::JSONVerbose->new(%args) if $format eq 'json-verbose';
133             return Data::Transit::Reader::MessagePack->new(%args) if $format eq 'message-pack';
134             confess "unknown reader format: $format";
135             }
136              
137             sub writer {
138             my ($format, $output, %args) = @_;
139             return Data::Transit::Writer::JSON->new($output, %args) if $format eq 'json';
140             return Data::Transit::Writer::JSONVerbose->new($output, %args) if $format eq 'json-verbose';
141             return Data::Transit::Writer::MessagePack->new($output, %args) if $format eq 'message-pack';
142             confess "unknown writer format: $format";
143             }
144              
145             =head1 AUTHOR
146              
147             Colin Williams, C<< >>
148              
149             =head1 SUPPORT
150              
151             You can find documentation for this module with the perldoc command.
152              
153             perldoc Data::Transit
154              
155              
156             You can also look for information at:
157              
158             =over 4
159              
160             =item * RT: CPAN's request tracker (report bugs here)
161              
162             L
163              
164             =item * AnnoCPAN: Annotated CPAN documentation
165              
166             L
167              
168             =item * CPAN Ratings
169              
170             L
171              
172             =item * Search CPAN
173              
174             L
175              
176             =back
177              
178             =head1 LICENSE AND COPYRIGHT
179              
180             Copyright 2015 Colin Williams.
181              
182             This program is free software; you can redistribute it and/or modify it
183             under the terms of the the Artistic License (2.0). You may obtain a
184             copy of the full license at:
185              
186             L
187              
188             Any use, modification, and distribution of the Standard or Modified
189             Versions is governed by this Artistic License. By using, modifying or
190             distributing the Package, you accept this license. Do not use, modify,
191             or distribute the Package, if you do not accept this license.
192              
193             If your Modified Version has been derived from a Modified Version made
194             by someone other than you, you are nevertheless required to ensure that
195             your Modified Version complies with the requirements of this license.
196              
197             This license does not grant you the right to use any trademark, service
198             mark, tradename, or logo of the Copyright Holder.
199              
200             This license includes the non-exclusive, worldwide, free-of-charge
201             patent license to make, have made, use, offer to sell, sell, import and
202             otherwise transfer the Package with respect to any patent claims
203             licensable by the Copyright Holder that are necessarily infringed by the
204             Package. If you institute patent litigation (including a cross-claim or
205             counterclaim) against any party alleging that the Package constitutes
206             direct or contributory patent infringement, then this Artistic License
207             to you shall terminate on the date that such litigation is filed.
208              
209             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
210             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
211             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
212             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
213             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
214             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
215             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
216             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
217              
218              
219             =cut
220              
221              
222             1;