source: main/trunk/greenstone2/perllib/cpan/JSON/PP5005.pm@ 24921

Last change on this file since 24921 was 24921, checked in by davidb, 12 years ago

Some of our perl CGI scripts use JSON, while included in the ActivePerl distribution, it does not appear to be a standard Perl module

  • Property svn:executable set to *
File size: 3.0 KB
Line 
1package JSON::PP5005;
2
3use 5.005;
4use strict;
5
6my @properties;
7
8$JSON::PP5005::VERSION = '1.10';
9
10BEGIN {
11
12 sub utf8::is_utf8 {
13 0; # It is considered that UTF8 flag off for Perl 5.005.
14 }
15
16 sub utf8::upgrade {
17 }
18
19 sub utf8::downgrade {
20 1; # must always return true.
21 }
22
23 sub utf8::encode {
24 }
25
26 sub utf8::decode {
27 }
28
29 *JSON::PP::JSON_PP_encode_ascii = \&_encode_ascii;
30 *JSON::PP::JSON_PP_encode_latin1 = \&_encode_latin1;
31 *JSON::PP::JSON_PP_decode_surrogates = \&_decode_surrogates;
32 *JSON::PP::JSON_PP_decode_unicode = \&_decode_unicode;
33
34 # missing in B module.
35 sub B::SVp_IOK () { 0x01000000; }
36 sub B::SVp_NOK () { 0x02000000; }
37 sub B::SVp_POK () { 0x04000000; }
38
39 $INC{'bytes.pm'} = 1; # dummy
40}
41
42
43
44sub _encode_ascii {
45 join('', map { $_ <= 127 ? chr($_) : sprintf('\u%04x', $_) } unpack('C*', $_[0]) );
46}
47
48
49sub _encode_latin1 {
50 join('', map { chr($_) } unpack('C*', $_[0]) );
51}
52
53
54sub _decode_surrogates { # from http://homepage1.nifty.com/nomenclator/unicode/ucs_utf.htm
55 my $uni = 0x10000 + (hex($_[0]) - 0xD800) * 0x400 + (hex($_[1]) - 0xDC00); # from perlunicode
56 my $bit = unpack('B32', pack('N', $uni));
57
58 if ( $bit =~ /^00000000000(...)(......)(......)(......)$/ ) {
59 my ($w, $x, $y, $z) = ($1, $2, $3, $4);
60 return pack('B*', sprintf('11110%s10%s10%s10%s', $w, $x, $y, $z));
61 }
62 else {
63 Carp::croak("Invalid surrogate pair");
64 }
65}
66
67
68sub _decode_unicode {
69 my ($u) = @_;
70 my ($utf8bit);
71
72 if ( $u =~ /^00([89a-f][0-9a-f])$/i ) { # 0x80-0xff
73 return pack( 'H2', $1 );
74 }
75
76 my $bit = unpack("B*", pack("H*", $u));
77
78 if ( $bit =~ /^00000(.....)(......)$/ ) {
79 $utf8bit = sprintf('110%s10%s', $1, $2);
80 }
81 elsif ( $bit =~ /^(....)(......)(......)$/ ) {
82 $utf8bit = sprintf('1110%s10%s10%s', $1, $2, $3);
83 }
84 else {
85 Carp::croak("Invalid escaped unicode");
86 }
87
88 return pack('B*', $utf8bit);
89}
90
91
92sub JSON::PP::incr_parse {
93 local $Carp::CarpLevel = 1;
94 ( $_[0]->{_incr_parser} ||= JSON::PP::IncrParser->new )->incr_parse( @_ );
95}
96
97
98sub JSON::PP::incr_text {
99 $_[0]->{_incr_parser} ||= JSON::PP::IncrParser->new;
100
101 if ( $_[0]->{_incr_parser}->{incr_parsing} ) {
102 Carp::croak("incr_text can not be called when the incremental parser already started parsing");
103 }
104
105 $_[0]->{_incr_parser}->{incr_text} = $_[1] if ( @_ > 1 );
106 $_[0]->{_incr_parser}->{incr_text};
107}
108
109
110sub JSON::PP::incr_skip {
111 ( $_[0]->{_incr_parser} ||= JSON::PP::IncrParser->new )->incr_skip;
112}
113
114
115sub JSON::PP::incr_reset {
116 ( $_[0]->{_incr_parser} ||= JSON::PP::IncrParser->new )->incr_reset;
117}
118
119
1201;
121__END__
122
123=pod
124
125=head1 NAME
126
127JSON::PP5005 - Helper module in using JSON::PP in Perl 5.005
128
129=head1 DESCRIPTION
130
131JSON::PP calls internally.
132
133=head1 AUTHOR
134
135Makamaka Hannyaharamitu, E<lt>makamaka[at]cpan.orgE<gt>
136
137
138=head1 COPYRIGHT AND LICENSE
139
140Copyright 2007-2010 by Makamaka Hannyaharamitu
141
142This library is free software; you can redistribute it and/or modify
143it under the same terms as Perl itself.
144
145=cut
146
Note: See TracBrowser for help on using the repository browser.