1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
# NAME
JSON::Pointer - A Perl implementation of JSON Pointer (RFC6901)
# VERSION
This document describes JSON::Pointer version 0.07.
# SYNOPSIS
use JSON::Pointer;
my $obj = {
foo => 1,
bar => [ { qux => "hello" }, 3 ],
baz => { boo => [ 1, 3, 5, 7 ] }
};
JSON::Pointer->get($obj, "/foo"); ### $obj->{foo}
JSON::Pointer->get($obj, "/bar/0"); ### $obj->{bar}[0]
JSON::Pointer->get($obj, "/bar/0/qux"); ### $obj->{bar}[0]{qux}
JSON::Pointer->get($obj, "/bar/1"); ### $obj->{bar}[1]
JSON::Pointer->get($obj, "/baz/boo/2"); ### $obj->{baz}{boo}[2]
# DESCRIPTION
This library is implemented JSON Pointer ([http://tools.ietf.org/html/rfc6901](http://tools.ietf.org/html/rfc6901)) and
some useful operator from JSON Patch ([http://tools.ietf.org/html/rfc6902](http://tools.ietf.org/html/rfc6902)).
JSON Pointer is available to identify a specified value in JSON document, and it is simillar to XPath.
Please read the both of specifications for details.
# METHODS
## get($document :HashRef/ArrayRef/Scalar, $pointer :Str, $strict :Int) :Scalar
- $document :HashRef/ArrayRef/Scalar
Target perl data structure that is able to be presented by JSON format.
- $pointer :Str
JSON Pointer string to identify specified value in the document.
- $strict :Int
Strict mode. When this value equals true value, this method may throw exception on error.
When this value equals false value, this method return undef value on error.
Get specified value identified by _$pointer_ from _$document_.
For example,
use JSON::Pointer;
print JSON::Pointer->get({ foo => 1, bar => { "qux" => "hello" } }, "/bar/qux"); ### hello
## get\_relative($document :HashRef/ArrayRef/Scalar, $current\_pointer :Str, $relative\_pointer :Str, $strict :Int) :Scalar
**This method is highly EXPERIMENTAL**. Because this method depends on [http://tools.ietf.org/html/draft-luff-relative-json-pointer-00](http://tools.ietf.org/html/draft-luff-relative-json-pointer-00) draft spec.
- $document :HashRef/ArrayRef/Scalar
Target perl data structure that is able to be presented by JSON format.
- $current\_pointer : Str
JSON Pointer string to identify specified current position in the document.
- $relative\_pointer : Str
JSON Relative Pointer string to identify specified value from current position in the document
- $strict :Int
Strict mode. When this value equals true value, this method may throw exception on error.
When this value equals false value, this method return undef value on error.
## contains($document :HashRef/ArrayRef/Scalar, $pointer :Str) :Int
- $document :HashRef/ArrayRef/Scalar
Target perl data structure that is able to present by JSON format.
- $pointer :Str
JSON Pointer string to identify specified value in the document.
Return which the target location identified by _$pointer_ exists or not in the _$document_.
use JSON::Pointer;
my $document = { foo => 1 };
if (JSON::Pointer->contains($document, "/foo")) {
print "/foo exists";
}
## add($document :HashRef/ArrayRef/Scalar, $pointer :Str, $value :HashRef/ArrayRef/Scalar) :HashRef/ArrayRef/Scalar
- $document :HashRef/ArrayRef/Scalar
Target perl data structure that is able to be presented by JSON format.
- $pointer :Str
JSON Pointer string to identify specified value in the document.
- $value :HashRef/ArrayRef/Scalar
The perl data structure that is able to be presented by JSON format.
Add specified _$value_ on target location identified by _$pointer_ in the _$document_.
For example,
use JSON::Pointer;
my $document = +{ foo => 1, };
my $value = +{ qux => "hello" };
my $patched_document = JSON::Pointer->add($document, "/bar", $value);
print $patched_document->{bar}{qux}; ### hello
## remove($document, $pointer) :Array/Scalar
- $document :HashRef/ArrayRef/Scalar
Target perl data structure that is able to be presented by JSON format.
- $pointer :Str
JSON Pointer string to identify specified value in the document.
Remove target location identified by _$pointer_ in the _$document_.
use JSON::Pointer;
my $document = { foo => 1 };
my $patched_document = JSON::Pointer->remove($document, "/foo");
unless (exists $patched_document->{foo}) {
print "removed /foo";
}
This method is contextial return value. When the return value of _wantarray_ equals true,
return _$patched\_document_ and _$removed\_value_, or not return _$patched\_document_ only.
## replace($document :HashRef/ArrayRef/Scalar, $pointer :Str, $value :HashRef/ArrayRef/Scalar) :Array/HashRef/ArrayRef/Scalar
- $document :HashRef/ArrayRef/Scalar
Target perl data structure that is able to be presented by JSON format.
- $pointer :Str
JSON Pointer string to identify specified value in the document.
- $value :HashRef/ArrayRef/Scalar
The perl data structure that is able to be presented by JSON format.
Replace the value of target location specified by _$pointer_ to the _$value_ in the _$document_.
use JSON::Pointer;
my $document = { foo => 1 };
my $patched_document = JSON::Pointer->replace($document, "/foo", 2);
print $patched_document->{foo}; ## 2
This method is contextial return value. When the return value of _wantarray_ equals true,
return _$patched\_document_ and _$replaced\_value_, or not return _$patched\_document_ only.
## set($document :HashRef/ArrayRef/Scalar, $pointer :Str, $value :HashRef/ArrayRef/Scalar) :Array/HashRef/ArrayRef/Scalar
This method is alias of replace method.
## copy($document :HashRef/ArrayRef/Scalar, $from\_pointer :Str, $to\_pointer :Str) :HashRef/ArrayRef/Scalar
- $document :HashRef/ArrayRef/Scalar
Target perl data structure that is able to be presented by JSON format.
- $from\_pointer :Str
JSON Pointer string to identify specified value in the document.
- $to\_pointer :Str
JSON Pointer string to identify specified value in the document.
Copy the value identified by _$from\_pointer_ to target location identified by _$to\_pointer_.
For example,
use JSON::Pointer;
my $document = +{ foo => [ { qux => "hello" } ], bar => [ 1 ] };
my $patched_document = JSON::Pointer->copy($document, "/foo/0/qux", "/bar/-");
print $patched_document->{bar}[1]; ## hello
Note that "-" notation means next of last element in the array.
In this example, "-" means 1.
## move($document :HashRef/ArrayRef/Scalar, $from\_pointer :Str, $to\_pointer :Str) :HashRef/ArrayRef/Scalar
- $document :HashRef/ArrayRef/Scalar
Target perl data structure that is able to be presented by JSON format.
- $from\_pointer :Str
JSON Pointer string to identify specified value in the document.
- $to\_pointer :Str
JSON Pointer string to identify specified value in the document.
Move the value identified by _$from\_pointer_ to target location identified by _$to\_pointer_.
For example,
use JSON;
use JSON::Pointer;
my $document = +{ foo => [ { qux => "hello" } ], bar => [ 1 ] };
my $patched_document = JSON::Pointer->move($document, "/foo/0/qux", "/bar/-");
print encode_json($patched_document); ## {"bar":[1,"hello"],"foo":[{}]}
## test($document :HashRef/ArrayRef/Scalar, $pointer :Str, $value :HashRef/ArrayRef/Scalar) :Int
- $document :HashRef/ArrayRef/Scalar
Target perl data structure that is able to be presented by JSON format.
- $pointer :Str
JSON Pointer string to identify specified value in the document.
- $value :HashRef/ArrayRef/Scalar
The perl data structure that is able to be presented by JSON format.
Return which the value identified by _$pointer_ equals _$value_ or not in the _$document_.
This method distinguish type of each values.
use JSON::Pointer;
my $document = { foo => 1 };
print JSON::Pointer->test($document, "/foo", 1); ### 1
print JSON::Pointer->test($document, "/foo", "1"); ### 0
## traverse($document, $pointer, $opts) : JSON::Pointer::Context
This method is used as internal implementation only.
# DEPENDENCIES
Perl 5.8.1 or later.
# BUGS
All complex software has bugs lurking in it, and this module is no
exception. If you find a bug please either email me, or add the bug
to cpan-RT.
# SEE ALSO
- [perl](https://metacpan.org/pod/perl)
- [Mojo::JSON::Pointer](https://metacpan.org/pod/Mojo::JSON::Pointer)
Many codes in this module is inspired by the module.
- [http://tools.ietf.org/html/rfc6901](http://tools.ietf.org/html/rfc6901)
- [http://tools.ietf.org/html/rfc6902](http://tools.ietf.org/html/rfc6902)
- [http://tools.ietf.org/html/draft-luff-relative-json-pointer-00](http://tools.ietf.org/html/draft-luff-relative-json-pointer-00)
# AUTHOR
Toru Yamaguchi <zigorou at cpan.org>
# LICENSE AND COPYRIGHT
Copyright (c) 2013, Toru Yamaguchi. All rights reserved.
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
|