From: "Kris Zyp" <kris...@gmail.com>
Date: Fri, 5 Sep 2008 09:33:16 -0600
Local: Fri, Sep 5 2008 11:33 am
Subject: JSON Resources: A proposal for a RESTful JSON protocol
Below is my proposal for a RESTful JSON protocol, called JSON resources. This proposal is focused on defining the use of REST architecture for integration with JSON documents, and intends to leverage and build on existing standards, and on existing implementations (and my experience working on them). However, this proposal is not a reverse-engineer or documentation of any existing implementation. Where possible, this proposal uses implementation tested or existing standards based techniques; there are a number of mechanisms in here that are somewhat new, partly due to desires expressed by this group, or by applications of standards that simply have not yet been implemented due to lack of demand so far. This proposal is admittedly quite rough, but hopefully it can serve to put forth some ideas for a RESTful JSON protocol. I don't believe this proposal is mutually exclusive with John Cowan's proposal for JSON Web Collections. The JSON resources proposal is primarily concerned with semantics of RESTful communication and interaction with JSON data, and generally does not impose any format/structure on the JSON data, however I did include a section on defining a structure for collections that uses a top-level array that is partly based on Cowan's proposal, although this is rather peripheral to main focus of the proposal. Even beyond top-level objects, JSON Web Collections may also be useful as an additional content type as a JSON structure for inclusion of per-resource metadata. This may have value with metadata intensive application, and I think it should work properly with the JSON resources proposal. http://www.json.com/specifications/json-resources/ JSON Resources Proposal JSON resources does not specify a JSON format, any valid JSON structure can be used with JSON resources. JSON resources does recommend a structure for hyper-referencing objects, but this structure can still be used with any JSON format, and it is not required. The specification assumes the use of the HTTP protocol for RESTful interaction because of it's ubiquity and position as the reference implementation for REST. JSON resources can be used with any REST architectures, but alternate REST architecture bear the responsibility for defining the resource metadata and methods in their own equivalent terms. Resource Locator Determination JSON is hierarchical in nature. A top level JSON entity (object or array) may have objects that can be treated as resources as themselves. In order for these sub-entities to be properly treated as resources, a client must be able to determine the resource's locator path (URL). An object's URL is determined from the object's locator property, treated as a relative URL. To calculate an object's URL, the URL used to retrieve the JSON document is the base URL and the value of the locator property is the relative URL. Then, the absolute URL can be computed based on RFC 1808's specification (http://www.ietf.org/rfc/rfc1808.txt). A server can declare which property is the locator property for objects in an JSON document with a MIME parameter (MIME parameters are defined in http://tools.ietf.org/html/rfc2045#section-5). Alternate JSON sub-content types (like application/collections+json) may utilize a predefined JSON structure which indicates the locator property. When a MIME parameter is used to define the locator property. The MIME parameter name should be "locator" and the value should be the name of the locator property. For example if the content type of the resource was "application/json" (alternate subtypes are allowed), then the locator property can be declared by suffixing the content type, and the following JSON representation could be sent. If the a request was made to http://site.com/Person/, then the following response could be returned: Content-Type: application/json;locator=my_id[ {"my_id":"1","name":"John Doe"}If a request is made to http://site.com/Person/1 with the same headers as the initial response, the server should respond with the representation shown above. If the client wishes to modify the resource it may request the modification by sending a PUT method and the representation of the resource with modifications. PUT /Person/1 {"my_id":"1","name":"Jim Doe"}If the server permits the operation, the resource should be modified to match the provided representation. New properties that were not in the old version should be added, modified properties should be updated, and properties not listed in the old version should be deleted. The client may request the deletion of the resource with the DELETE method: DELETE /Person/1Resource Creation POST /Person/ {"name":"Josh Doe"}This indicates that the client requests a new object be created with a "name" property with the value of "Josh Doe". If successful, the server respond with a response with a status of 201 Created. The response must also include a "Location" header that indicates the location of the newly created resource. For example: Location: /Person/3If the server does not support creation of resources in this manner, or the operation was unauthorized, the server should respond with the appropriate HTTP response status. A new resource may alternately be created with the PUT method. With the approach, the client provides the location for which it desires the resource to be accessible from in the future. The desired resource representation should still be included as with a POST method creation except that the client should include the appropriate locator property corresponding to the new resource's location. For example: PUT /Person/josh {"my_id":"josh","name":"Josh Doe"}If the operation is successful, the server should respond with a 201 status. The server may include a "Location" header indicating where the resource will be accessible from in the future, however this is not necessary if the client's defined location is acceptable to the server. Hyper References {"my_id":"1","name":"John Doe","spouse":{"$ref":"2"}}This would indicate that the "spouse" property's value is the JSON resource located at http://site.com/Person/2. If both subresources http://site.com/Person/1 and http://site.com/Person/2 were both provided within a single collection: [ Property Path Based Referencing GET /Person/[ {"my_id":"1","name":"John Doe","spouse":{"$ref":"2"}, children:[{"my_id":"3","name":"Jill Doe"}]}, {"my_id":"2","name":"Jane Doe","spouse":{"$ref":"1"} children:{"$ref":"1#.children"}}]In this example, both "John Doe" and "Jane Doe" refer to the same "children" JSON array. Another example: {"my_id":"1","foo":[{"bar":3},{"$ref":"#.foo[0]"}]}In this example, the resource indicates that both the first and second entry in the array in the "foo" property, should be the same object (the object {"bar":3}). The grammar for a JSON reference (building on the grammar defined in RFC 2616): json-reference = "{" <"> "$ref" <"> ":" <"> [relativeURI | absoluteURI] ["#"] *property-segment <"> "}"property-segment = ("." token) | ("[" 1*DIGIT "]") | ("[" single-quoted-string "]")single-quoted-string = "'" *(<any TEXT except "'"> | single-quoted-pair) "'"single-quoted-pair = "\" CHARPartial Loading [ A client can request that the objects that are returned from the server only include certain properties using a Range header with "properties" range units. The "properties" range unit defines a set of properties with a comma delimited set of property names as tokens or quoted strings. For example, to request a collection of objects where each object only includes the "name" property: GET /Person/ HTTP/1.1Range: properties=nameIf the server supports the "properties" range unit, the server should return a response like the example above where each object is a reference object with the properties specified in the Range header. Permission Indication When a collection resource is requested, a "X-Sub-Allow" header may also be included that indicates the allowable operations on the sub-resources: For example, if a server responded to the request for /Person/ with headers: Allow: GET, HEAD, POST Note that a server should be prepared to handle any method (and possibly reject), as a client may not respect the Allow header. Permission Setting POST /Person/Allow: HEAD, GET...Paging The returned entity from a Range should be a textual section of the entire JSON array. Consequently, the entity should start with "[" only if the first item in the array is included. It should end with "]" only if the last item is included, otherwise it should end with a ",". This allows Range responses to be properly concatenated by non-JSON aware proxy servers. The response should also include a Content-Range header indicating the items returned and the total count using the format defined in RFC 2616. For example: GET /Person/ HTTP/1.1 Accept-Ranges: itemsPartial Object Updates PUT /Person/2 A server that can handle partial object updates using the "properties" range unit may indicate this capability with the Accept-Ranges header. For example to indicate support for "items" and "properties" range units: Accept-Ranges: items, propertiesTable and Schema Discovery { GET /Personresponse: { Non-HTTP REST REST Request Message content This property provides the data to be applied to the target path. This is the new object data for puts and posts REST Response Message JSON Format Property Definition This message format would be defined as "application/rest+json". Another related format type may define additional resource metadata for item in the collection (possibly "application/collection+json"). Rational You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||