Change in JSON output for collections
"John P. Rouillard" <[email protected]>
| Newsgroups | gmane.comp.bug-tracking.roundup.devel |
|---|---|
| Message-ID | <[email protected]> |
Hi all:
I am working on some code that is going to drastically change the json
output form for collections. The current code supports pagination, but
doesn't provide 'prev' or 'next' links to allow the client to walk
through the pages. The current collection response is a simple array of
values:
{
"data": [
{
"link": "https://example.com/demo/rest/data/user/2",
"id": "2"
},
{
"link": "https://example.com/demo/rest/data/user/3",
"id": "3"
}
]
}
There is no place to put pagination and other data about the
collection. The code I am working on makes the following changes:
1) The array data is moved to the 'collection' property
2) a new @links dict that holds an array of link objects indexed by
relationship
3) a new @total_size property reports the number of items if you
were to get all the pages. (The current code also provides this
number in the X-Count-Total header.)
Each element of @links is an array of links objects because I can see
some cases where there may be multiple links with the same relation.
So using:
curl -s -u admin:admin -X GET --header "Accept: application/json" \
"https://example.com/demo/rest/data/user?page_index=2&page_size=2"
it produces output like this:
{
"data": {
"@total_size": 5,
"@links": {
"self": [
{
"uri": "https://rouilj.dynamic-dns.net/demo/rest/data/user?page_index=2&pretty=any&page_size=2",
"rel": "self"
}
],
"prev": [
{
"uri": "https://rouilj.dynamic-dns.net/demo/rest/data/user?page_index=1&pretty=any&page_size=2",
"rel": "prev"
}
],
"next": [
{
"uri": "https://rouilj.dynamic-dns.net/demo/rest/data/user?page_index=3&pretty=any&page_size=2",
"rel": "next"
}
]
},
"collection": [
{
"link": "https://rouilj.dynamic-dns.net/demo/rest/data/user/2",
"id": "2"
},
{
"link": "https://rouilj.dynamic-dns.net/demo/rest/data/user/3",
"id": "3"
}
]
}
}
If you are the last page of the collection, there is no "next" link.
If you are at the first page of the collection, there is no "prev"
link.
I did not add first or last links, they can be added later if this
looks acceptable.
Also I pushed a change a bit earlier to make pretty printing the
default. It can be turned off with ?pretty=false although with gzip
compression implemented, the difference is size is minimal and it
makes debugging/manually using the API much easier.
Quips, comments, evasions, questions or answers?
--
-- rouilj
John Rouillard
===========================================================================
My employers don't acknowledge my existence much less my opinions.