1JSON::Tokenize(3) User Contributed Perl Documentation JSON::Tokenize(3)
2
3
4
6 JSON::Tokenize - tokenize a string containing JSON
7
9 use JSON::Tokenize ':all';
10 my $input = '{"tuttie":["fruity", true, 100]}';
11 my $token = tokenize_json ($input);
12 print_tokens ($token, 0);
13
14 sub print_tokens
15 {
16 my ($token, $depth) = @_;
17 while ($token) {
18 my $start = tokenize_start ($token);
19 my $end = tokenize_end ($token);
20 my $type = tokenize_type ($token);
21 print " " x $depth;
22 my $value = substr ($input, $start, $end - $start);
23 print ">>$value<< has type $type\n";
24 my $child = tokenize_child ($token);
25 if ($child) {
26 print_tokens ($child, $depth+1);
27 }
28 my $next = tokenize_next ($token);
29 $token = $next;
30 }
31 }
32
33 This outputs
34
35 >>{"tuttie":["fruity", true, 100]}<< has type object
36 >>"tuttie"<< has type string
37 >>:<< has type colon
38 >>["fruity", true, 100]<< has type array
39 >>"fruity"<< has type string
40 >>,<< has type comma
41 >>true<< has type literal
42 >>,<< has type comma
43 >>100<< has type number
44
46 This documents version 0.55 of JSON::Tokenize corresponding to git
47 commit 739c1e12e85756c703242229caef685615ba77ca
48 <https://github.com/benkasminbullock/JSON-
49 Parse/commit/739c1e12e85756c703242229caef685615ba77ca> released on Tue
50 Oct 24 07:11:47 2017 +0900.
51
53 This is a module for tokenizing a JSON string. It breaks the string
54 into individual tokens without creating any Perl structures. Thus it
55 can be used for tasks such as picking out or searching through parts of
56 a large JSON structure without storing each part of the entire
57 structure as individual Perl variables in memory.
58
59 This module is an experimental part of JSON::Parse and its interface is
60 likely to change. The tokenizing functions are currently written in a
61 very primitive way.
62
64 tokenize_json
65 my $token = tokenize_json ($json);
66
67 tokenize_next
68 my $next = tokenize_next ($token);
69
70 Walk the tree of tokens.
71
72 tokenize_child
73 my $child = tokenize_child ($child);
74
75 Walk the tree of tokens.
76
77 tokenize_start
78 my $start = tokenize_start ($token);
79
80 Get the start of the token as a byte offset from the start of the
81 string. Note this is a byte offset not a character offset.
82
83 tokenize_end
84 my $end = tokenize_end ($token);
85
86 Get the end of the token as a byte offset from the start of the string.
87 Note this is a byte offset not a character offset.
88
89 tokenize_type
90 my $type = tokenize_type ($token);
91
92 Get the type of the token as a string. The possible return values are
93
94 "invalid",
95 "initial state",
96 "string",
97 "number",
98 "literal",
99 "object",
100 "array",
101 "unicode escape"
102
103 tokenize_text
104 my $text = tokenize_text ($json, $token);
105
106 Given a token $token from this parsing and the JSON in $json, return
107 the text which corresponds to the token. This is a convenience function
108 written in Perl which uses "tokenize_start" and "tokenize_end" and
109 "substr" to get the string from $json.
110
112 Ben Bullock, <bkb@cpan.org>
113
115 This package and associated files are copyright (C) 2016-2017 Ben
116 Bullock.
117
118 You can use, copy, modify and redistribute this package and associated
119 files under the Perl Artistic Licence or the GNU General Public
120 Licence.
121
122
123
124perl v5.28.0 2017-10-24 JSON::Tokenize(3)