users@jersey.java.net

RESTful API with optional XML elements/JSON properties

From: Jaka Jančar <jaka_at_kubje.org>
Date: Fri, 6 Mar 2009 20:30:20 +0100

Hi!

I want to develop a RESTful API, which would differentiate between no
occurance of an XML element/undefined JSON property and a xsi:nil
element/null property.

Why? Take for example a PUT request to /users/15:

<user>
   <comapnyId xsi:nil="true"/>
</user>

This would remove the user from a company, while leaving everything
else unchanged. Similarly:

<user>
   <password>foo</password>
</user>

would change the user's password, while leaving the company unchanged.

I would also use this in the opposite direction, with GET requests,
when requesting different "views" (subsets) of a resource: no
<company> element would mean that the company was simply not retrieved
and the full representation must be fetched, while a nil element would
mean that there is no company set for the user.

First of all, does anyone see any problems with this design?

Secondly, how would I ago about implementing this?

In Javascript there's a difference between no object property ("if
(prop in obj)" or "if (obj.prop === undefined)") and a null property
("if (obj.prop === null"). Similarly in PHP and others.

In Java I guess I would need another field, for example xxxDefined for
each field. Or are there any smarter alternatives?

Has anyone done anything similar yet?

Regards,
  Jaka