kamikat / moshi-jsonapi

JSON API v1.0 Specification in Moshi.
MIT License
156 stars 34 forks source link

Serialization does not follow JSONApi Specification #28

Closed tylergets closed 8 years ago

tylergets commented 8 years ago

According to the JSONAPIs documentation around Creating of CRUD resources, it shows that objects must be wrapped in a data tag like so

POST /photos HTTP/1.1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json

{
  "data": {
    "type": "photos",
    "attributes": {
      "title": "Ember Hamster",
      "src": "http://example.com/images/productivity.png"
    }
  }
}

However, this package (as shown in the README) does not wrap the object inside the data attribute.

moshi.adapter(Person.class).toJson(person);
// => { "type": "people", "attributes": { "first-name": "Yuki", "last-name": "Kiriyama", "twitter": "kamikat_bot" } }

Is this intentional? It seems like a breaking change for many existing projects however I believe the goal of this package is to built to spec.

tylergets commented 8 years ago

Reference to the JSONApi documentation here: http://jsonapi.org/format/#crud-creating

kamikat commented 8 years ago

Umm... I'm sorry for the ambiguous documentation. To serialize a Resource as document, the Resource object should be added to a Document. Call either Resource.addTo(Document) or Document.addData(Resource). Here is an example:

person.addTo(Document.create());
// or 
Document doc = Document.create();
doc.addData(person);

// and then
moshi.adapter(Person.class).toJson(person);
// => { "data": { "type": "people", "attributes": { "first-name": "Yuki", "last-name": "Kiriyama", "twitter": "kamikat_bot" } } }

// alternatively, for list of resources
moshi.adapter(Person[].class).toJson(new Person[] { person });
// => { "data": [{ "type": "people", "attributes": { "first-name": "Yuki", "last-name": "Kiriyama", "twitter": "kamikat_bot" } }] }