| JSON::Pointer(3pm) | User Contributed Perl Documentation | JSON::Pointer(3pm) |
JSON::Pointer - A Perl implementation of JSON Pointer (RFC6901)
This document describes JSON::Pointer version 0.07.
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]
This library is implemented JSON Pointer (<http://tools.ietf.org/html/rfc6901>) and some useful operator from JSON Patch (<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.
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
This method is highly EXPERIMENTAL. Because this method depends on <http://tools.ietf.org/html/draft-luff-relative-json-pointer-00> draft spec.
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 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 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 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.
This method is alias of replace method.
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 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":[{}]}
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
This method is used as internal implementation only.
Perl 5.8.1 or later.
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.
Toru Yamaguchi <zigorou at cpan.org>
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.
| 2022-06-15 | perl v5.34.0 |