jsonapi-suite / jsonapi_compliable

MIT License
20 stars 35 forks source link

Add support for XML and JSON #118

Closed richmolj closed 6 years ago

richmolj commented 6 years ago

The beginning of support for multiple content types. You can now do this:

employees = EmployeeResource.all(params)
employees.to_json # simple plain json
employees.to_xml # plain json in xml format
employees.to_jsonapi # json in jsonapi format

All serialization, sparse fields and "sideloads" will be respected. When rendering JSON, the top-level keys are employees (or whatever the jsonapi "type" is) and meta. This way we can continue to support total count and other metadata.

With some minor additions to a Rails app + responders gem, which we will add to our generator, that means this:

def index
  employees = EmployeeResource.all(params)
  respond_with(employees)
end

Will render the approprate response based on the request format (.json, .xml, .jsonapi), or content type (application/json, application/xml, application/vnd.api+json').

Note there are no custom xml or json renderers registered in the Railtie! Our proxy object correctly responds to to_xml and to_json, so it works out-of-the-box, even alongside existing APIs already rendering vanilla json.

Some exceptions:

Other notes: