4:# JSON API Serializer
7:  <img src="https://github.com/SeyZ/jsonapi-serializer/blob/master/logo.jpg?raw=true" alt="JSONAPI Serializer Logo">
10:[![Build Status](https://travis-ci.org/SeyZ/jsonapi-serializer.svg?branch=master)](https://travis-ci.org/SeyZ/jsonapi-serializer)
11:[![npm version](https://img.shields.io/npm/v/jsonapi-serializer.svg)](https://yarnpkg.com/en/package/jsonapi-serializer)
12:[![download](https://img.shields.io/npm/dm/jsonapi-serializer.svg)](https://yarnpkg.com/en/package/jsonapi-serializer)
14:A Node.js framework agnostic library for (de)serializing your data to [JSON
17:psst: If you need an off-the-shelf admin panel for your app, check out what I build in my day job at [forestadmin.com](https://www.forestadmin.com) - it uses jsonapi-serializer to de/serialize data data coming from/to the APIs.
19:- [Migrate from 2.0 to 3.0](https://github.com/SeyZ/jsonapi-serializer/wiki/Migrate-from-2.0-to-3.0)
20:- [Migrate from 1.x to 2.0](https://github.com/SeyZ/jsonapi-serializer/wiki/Migrate-from-1.x-to-2.0)
23:`$ npm install jsonapi-serializer`
29:    var JSONAPISerializer = require('jsonapi-serializer').Serializer;
30:    new JSONAPISerializer(type, opts).serialize(data);
37:Calling the `serialize` method on the returned object will serialize your `data` (object or array) to a compliant JSONAPI document.
42:- **attributes**: An array of attributes to show. You can define an attribute as an option if you want to define some relationships (included or not).
43:    - **ref**: If present, it's considered as a relationships.
44:    - **included**: Consider the relationships as [compound document](http://jsonapi.org/format/#document-compound-documents). Default: true.
50:    - **relationshipLinks**: An object that describes the links inside relationships. Values can be *string* or a *function*
51:    - **relationshipMeta**: An object that describes the meta inside relationships. Values can be a plain value or a *function*
52:    - **ignoreRelationshipData**: Do not include the `data` key inside the relationship. Default: false.
62:- [Express example](https://github.com/SeyZ/jsonapi-serializer/tree/master/examples/express)
63:- [Simple usage](#simple-usage-serializer)
64:- [More examples in tests](https://github.com/SeyZ/jsonapi-serializer/blob/master/test/serializer.js)
66:<a name="simple-usage-serializer"></a>
77:var JSONAPISerializer = require('jsonapi-serializer').Serializer;
83:var users = UserSerializer.serialize(data);
85:// `users` here are JSON API compliant.
112:    var JSONAPIDeserializer = require('jsonapi-serializer').Deserializer;
113:    new JSONAPIDeserializer(opts).deserialize(data);
115:The function `JSONAPIDeserializer` takes one argument:
117:- `opts`: The deserializer options.
119:Calling the `deserialize` method on the returned object will deserialize your `data` (JSONAPI document) to a plain javascript object.
124:- **AN\_ATTRIBUTE\_TYPE**: this option name corresponds to the type of a relationship from your JSONAPI document.
125:	- **valueForRelationship**: A function that returns whatever you want for a relationship (see examples below) ***can return a Promise (see tests)***
130:- [Simple usage](#simple-usage-deserializer)
131:- [Relationship](#relationship-deserializer)
132:- [More examples in tests](https://github.com/SeyZ/jsonapi-serializer/blob/master/test/deserializer.js)
134:<a name="simple-usage-deserializer"></a>
158:var JSONAPIDeserializer = require('jsonapi-serializer').Deserializer;
160:new JSONAPIDeserializer().deserialize(jsonapi, function (err, users) {
171:<a name="relationship-deserializer"></a>
183:    relationships: {
195:    relationships: {
205:var JSONAPIDeserializer = require('jsonapi-serializer').Deserializer;
207:new JSONAPIDeserializer({
209:    valueForRelationship: function (relationship) {
211:        id: relationship.id,
218:}).deserialize(jsonapi, function (err, users) {
256:    var JSONAPIError = require('jsonapi-serializer').Error;
257:    var error = new JSONAPIError(opts);
261:- `opts`: The error options. All options are optional.
263:#### Available error option (`opts` argument)
267:- **code**: an application-specific error code, expressed as a string value.
270:- **source**: an object containing references to the source of the error, optionally including any of the following members:
272:  - **parameter**: a string indicating which URI query parameter caused the error.
275:- **meta**: a meta object containing non-standard meta-information about the error.
279:- [Simple usage](#simple-usage-error)
280:- [More example](https://github.com/SeyZ/jsonapi-serializer/blob/master/test/error.js)
282:<a name="simple-usage-error"></a>
286:var JSONAPIError = require('jsonapi-serializer').Error;
288:var errors = new JSONAPIError({
295:// `errors` here are JSON API compliant.
302:  "errors": [
315:[MIT](https://github.com/SeyZ/jsonapi-serializer/blob/master/LICENSE)
