File Coverage

blib/lib/JSON/YAJL/Parser.pm
Criterion Covered Total %
statement 6 6 100.0
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 8 8 100.0


line stmt bran cond sub pod time code
1             package JSON::YAJL::Parser;
2 3     3   16 use strict;
  3         14  
  3         107  
3 3     3   15 use warnings;
  3         4  
  3         263  
4             our $VERSION = '0.10';
5              
6             require XSLoader;
7             XSLoader::load( 'JSON::YAJL::Parser', $VERSION );
8              
9             1;
10              
11             =head1 NAME
12              
13             JSON::YAJL::Parser - JSON parsing with YAJL
14              
15             =head1 SYNOPSIS
16              
17             use JSON::YAJL;
18             my $text;
19             my $parser = JSON::YAJL::Parser->new(
20             0, 0,
21             [ sub { $text .= "null\n" },
22             sub { $text .= "bool: @_\n" },
23             undef,
24             undef,
25             sub { $text .= "number: @_\n" },
26             sub { $text .= "string: @_\n" },
27             sub { $text .= "map_open\n" },
28             sub { $text .= "map_key: @_\n" },
29             sub { $text .= "map_close\n" },
30             sub { $text .= "array_open\n" },
31             sub { $text .= "array_close\n" },
32             ]
33             );
34             my $json
35             = '{"integer":123,"double":4,"number":3.141,"string":"a string","string2":"another string","null":null,"true":true,"false":false,"map":{"key":"value","array":[1,2,3]}}';
36             $parser->parse($json);
37             $parser->parse_complete();
38             # $text is now:
39             # map_open
40             # map_key: integer
41             # number: 123
42             # map_key: double
43             # number: 4
44             # map_key: number
45             # number: 3.141
46             # map_key: string
47             # string: a string
48             # map_key: string2
49             # string: another string
50             # map_key: null
51             # null
52             # map_key: true
53             # bool: 1
54             # map_key: false
55             # bool: 0
56             # map_key: map
57             # map_open
58             # map_key: key
59             # string: value
60             # map_key: array
61             # array_open
62             # number: 1
63             # number: 2
64             # number: 3
65             # array_close
66             # map_close
67             # map_close
68              
69             =head1 DESCRIPTION
70              
71             This module allows you to parse JSON with YAJL. This is quite a low-level
72             interface for parsing JSON.
73              
74             You pass callbacks which are called whenever a small token of JSON is parsed.
75              
76             This is a very early release to see how cross-platform the underlying code is.
77             The API may change in future.
78              
79             =head1 METHODS
80              
81             =head2 new
82              
83             The constructor. You pass in if JavaScript style comments will be allowed in
84             the JSON input (both slash star and slash slash) and if invalid UTF8 strings
85             should cause a parse error. You also pass in callbacks. The missing callbacks
86             below are for integer and double - as there is no difference in Perl they are
87             both sent through the number callback instead.
88              
89             my $parser = JSON::YAJL::Parser->new(
90             0, 0,
91             [ sub { $text .= "null\n" },
92             sub { $text .= "bool: @_\n" },
93             undef,
94             undef,
95             sub { $text .= "number: @_\n" },
96             sub { $text .= "string: @_\n" },
97             sub { $text .= "map_open\n" },
98             sub { $text .= "map_key: @_\n" },
99             sub { $text .= "map_close\n" },
100             sub { $text .= "array_open\n" },
101             sub { $text .= "array_close\n" },
102             ]
103             );
104              
105             =head2 parse
106              
107             Parses some JSON. You can call this multiple times:
108              
109             $parser->parse($json);
110              
111             =head2 parse_complete
112              
113             Parse any remaining buffered JSON. Since YAJL is a stream-based parser,
114             without an explicit end of input, yajl sometimes can't decide if content
115             at the end of the stream is valid or not. For example, if "1" has been
116             fed in, yajl can't know whether another digit is next or some character
117             that would terminate the integer token:
118              
119             $parser->parse_complete();
120              
121             =head1 AUTHOR
122              
123             Leon Brocard
124              
125             =head1 LICENSE
126              
127             This module is free software; you can redistribute it or modify it under the same terms as Perl itself.
128              
129             =head1 SEE ALSO
130              
131             L, L