The same resource may return either XML or JSON depending upon the request, but it shouldn't return both at the same time. You will know which one to return based upon the request, so there is no need to generate both -- just generate the one you'll be returning.
Here is how you might choose to decide which to return. Evaluate in order, stopping when you've determined the format to return:
- If an extension has been added to the resource (
GET /user/1234.json
orGET /user/1234.xml
), use that as the requested format. - If an Accept header is set, use that header's value as the requested format.
- If there is a request body (as in the case of a POST), and the Content-Type header specifies JSON or XML, use that.
- Use a default format if none of the above apply (generally use JSON as your default unless your customers are generally still using XML).