| 1 |
NAME |
| 2 |
JSON::Hyper - extract links from JSON via a schema |
| 3 |
|
| 4 |
SYNOPSIS |
| 5 |
my $hyper = JSON::Hyper->new($hyperschema); |
| 6 |
my $json = from_json( ... ); |
| 7 |
my @links = $hyper->find_links($json->[1]->{some}->{object}); |
| 8 |
foreach my $link (@links) |
| 9 |
{ |
| 10 |
printf("<%s> (%s)", $link->{href}, $link->{rel}); |
| 11 |
} |
| 12 |
|
| 13 |
DESCRIPTION |
| 14 |
The JSON Hyper Schema proposal defines hypertext navigation through data |
| 15 |
structures represented by JSON. |
| 16 |
|
| 17 |
Constructor |
| 18 |
"new($hyperschema)" |
| 19 |
Given a JSON (or equivalent Perl nested hashref/arrayref structure) |
| 20 |
Hyper Schema, returns a Perl object capable of interpreting that |
| 21 |
schema. |
| 22 |
|
| 23 |
If the schema is omitted, defaults to the JSON Referencing hyper |
| 24 |
schema (described at <http://json-schema.org/json-ref>) |
| 25 |
|
| 26 |
Methods |
| 27 |
"schema" |
| 28 |
Returns the original schema as a hashref/arrayref structure. |
| 29 |
|
| 30 |
"ua" |
| 31 |
Get/set the LWP::UserAgent instance used to retrieve things. |
| 32 |
|
| 33 |
"find_links($object, $base)" |
| 34 |
Given a JSON object (or equivalent Perl nested hashref/arrayref |
| 35 |
structure) and optionally a base URL for interpreting relative URI |
| 36 |
references, returns a list of links found on object node. Does not |
| 37 |
operate recursively. |
| 38 |
|
| 39 |
Each link is a JSON::Hyper::Link object. |
| 40 |
|
| 41 |
"get($uri)" |
| 42 |
Performs an HTTP request for the given URI and returns a list of |
| 43 |
Perl nested hashref/arrayref structures corresponding to the JSON |
| 44 |
response. The URI may contain a fragment identifier, which will be |
| 45 |
interpreted according to the schema's fragment resolution method. |
| 46 |
Fragment resolution methods supported include: |
| 47 |
|
| 48 |
* slash-delimited (default) |
| 49 |
|
| 50 |
* dot-delimited |
| 51 |
|
| 52 |
* jsonpath |
| 53 |
|
| 54 |
For example, assuming the hyper schema specifies slash-delimited |
| 55 |
fragments, the following: |
| 56 |
|
| 57 |
my $hyper = JSON::Hyper->new($hyperschema); |
| 58 |
my ($result) = $hyper->get('http://example.com/data.json#foo/bar/0'); |
| 59 |
|
| 60 |
Is roughly equivalent to: |
| 61 |
|
| 62 |
use JSON; |
| 63 |
use LWP::UserAgent; |
| 64 |
my $ua = LWP::UserAgent->new; |
| 65 |
my $response = $ua->get('http://example.com/data.json'); |
| 66 |
my $object = from_json($response->decoded_content); |
| 67 |
my $result = $object->{foo}{bar}[0]; |
| 68 |
|
| 69 |
Note, if called multiple times on the same URL will return not just |
| 70 |
equivalent objects, but the same object. |
| 71 |
|
| 72 |
So, why does this method return a list of results instead of just a |
| 73 |
single result? In most cases, there will be either 0 or 1 items on |
| 74 |
the list; however, JSONPath allows a path to match multiple nodes, |
| 75 |
so there will occasionally be more than one result. (In scalar |
| 76 |
context, this method just returns the first result anyway.) |
| 77 |
|
| 78 |
"resolve_fragment($object, $fragment)" |
| 79 |
Used by "get" to resolve the fragment part of a URL against an |
| 80 |
object. |
| 81 |
|
| 82 |
"process_includes($object, $base, $recurse)" |
| 83 |
Given an JSON object (or equivalent Perl nested hashref/arrayref |
| 84 |
structure) and optional base URL, crawls the object finding |
| 85 |
rel="full" links, dereferences them using "get" and replaces the |
| 86 |
appropriate nodes with the retrieved content. $recurse is a boolean. |
| 87 |
|
| 88 |
This has the effect of rel="full" behaving like inclusion does in |
| 89 |
various programming languages. |
| 90 |
|
| 91 |
This modifies the given object rather than creating a new object. |
| 92 |
|
| 93 |
Utilities |
| 94 |
"JSON::Hyper::json_ref()" |
| 95 |
Returns the JSON referencing hyperschema as a hashref. |
| 96 |
|
| 97 |
BUGS |
| 98 |
Please report any bugs to <http://rt.cpan.org/>. |
| 99 |
|
| 100 |
SEE ALSO |
| 101 |
JSON::Hyper::Link. |
| 102 |
|
| 103 |
Related modules: JSON::T, JSON::Path, JSON::GRDDL, JSON::Schema. |
| 104 |
|
| 105 |
<http://tools.ietf.org/html/draft-zyp-json-schema>. |
| 106 |
|
| 107 |
AUTHOR |
| 108 |
Toby Inkster <[email protected]>. |
| 109 |
|
| 110 |
COPYRIGHT AND LICENCE |
| 111 |
Copyright 2010-2012 Toby Inkster. |
| 112 |
|
| 113 |
This module is tri-licensed. It is available under the X11 (a.k.a. MIT) |
| 114 |
licence; you can also redistribute it and/or modify it under the same |
| 115 |
terms as Perl itself. |
| 116 |
|
| 117 |
a.k.a. "The MIT Licence" |
| 118 |
Permission is hereby granted, free of charge, to any person obtaining a |
| 119 |
copy of this software and associated documentation files (the |
| 120 |
"Software"), to deal in the Software without restriction, including |
| 121 |
without limitation the rights to use, copy, modify, merge, publish, |
| 122 |
distribute, sublicense, and/or sell copies of the Software, and to |
| 123 |
permit persons to whom the Software is furnished to do so, subject to |
| 124 |
the following conditions: |
| 125 |
|
| 126 |
The above copyright notice and this permission notice shall be included |
| 127 |
in all copies or substantial portions of the Software. |
| 128 |
|
| 129 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 130 |
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 131 |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 132 |
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 133 |
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 134 |
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 135 |
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 136 |
|