A Node.js framework agnostic library for (de)serializing your data to JSON API
If you are interested, send me an email: sandro@munda.me
A Node.js framework agnostic library for (de)serializing your data to JSON
API (1.0 compliant).
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 - it uses jsonapi-serializer to de/serialize data data coming from/to the APIs.
$ npm install jsonapi-serializer
var JSONAPISerializer = require('jsonapi-serializer').Serializer;
new JSONAPISerializer(type, opts).serialize(data);
The function JSONAPISerializer
takes two arguments:
type
: The resource type.opts
: The serialization options.Calling the serialize
method on the returned object will serialize your data
(object or array) to a compliant JSONAPI document.
opts
argument)id
.data
key inside the relationship. Default: false.dash-case
(default), lisp-case
, spinal-case
, kebab-case
, underscore_case
, snake_case
, camelCase
, CamelCase
.undefined
, ignores the flag for that attribute. Option pluralizeType ignored if set.Examples
var data = [
{ id: 1, firstName: 'Sandro', lastName: 'Munda' },
{ id: 2, firstName: 'John', lastName: 'Doe' }
];
var JSONAPISerializer = require('jsonapi-serializer').Serializer;
var UserSerializer = new JSONAPISerializer('users', {
attributes: ['firstName', 'lastName']
});
var users = UserSerializer.serialize(data);
// `users` here are JSON API compliant.
The result will be something like:
{
"data": [{
"type": "users",
"id": "1",
"attributes": {
"first-name": "Sandro",
"last-name": "Munda"
}
}, {
"type": "users",
"id": "2",
"attributes": {
"first-name": "John",
"last-name": "Doe"
}
}]
}
var JSONAPIDeserializer = require('jsonapi-serializer').Deserializer;
new JSONAPIDeserializer(opts).deserialize(data);
The function JSONAPIDeserializer
takes one argument:
opts
: The deserializer options.Calling the deserialize
method on the returned object will deserialize your data
(JSONAPI document) to a plain javascript object.
opts
argument)dash-case
(default), lisp-case
, spinal-case
, kebab-case
, underscore_case
, snake_case
, camelCase
, CamelCase
.Examples
{
data: [{
type: 'users',
id: '1',
attributes: {
'first-name': Sandro,
'last-name': Munda
}
}, {
type: 'users',
id: '2',
attributes: {
'first-name': 'John',
'last-name': 'Doe'
}
}]
}
var JSONAPIDeserializer = require('jsonapi-serializer').Deserializer;
new JSONAPIDeserializer().deserialize(jsonapi, function (err, users) {
// `users` is...
});
[
{ id: 1, firstName: 'Sandro', lastName: 'Munda' },
{ id: 2, firstName: 'John', lastName: 'Doe' }
];
{
data: [{
type: 'users',
id: '54735750e16638ba1eee59cb',
attributes: {
'first-name': 'Sandro',
'last-name': 'Munda'
},
relationships: {
address: {
data: { type: 'addresses', id: '54735722e16620ba1eee36af' }
}
}
}, {
type: 'users',
id: '5490143e69e49d0c8f9fc6bc',
attributes: {
'first-name': 'Lawrence',
'last-name': 'Bennett'
},
relationships: {
address: {
data: { type: 'addresses', id: '54735697e16624ba1eee36bf' }
}
}
}]
}
var JSONAPIDeserializer = require('jsonapi-serializer').Deserializer;
new JSONAPIDeserializer({
addresses: {
valueForRelationship: function (relationship) {
return {
id: relationship.id,
'address-line1': '406 Madison Court',
'zip-code': '49426',
country: 'USA'
};
}
}
}).deserialize(jsonapi, function (err, users) {
// `users` is...
});
[{
id: '54735750e16638ba1eee59cb',
'first-name': 'Sandro',
'last-name': 'Munda',
address: {
id: '54735722e16620ba1eee36af',
'address-line1': '406 Madison Court',
'zip-code': '49426',
country: 'USA'
}
}, {
id: '5490143e69e49d0c8f9fc6bc',
'first-name': 'Lawrence',
'last-name': 'Bennett',
address: {
id: '54735697e16624ba1eee36bf',
'address-line1': '406 Madison Court',
'zip-code': '49426',
country: 'USA'
}
}]
The deserialization option valueForRelationship
supports returning a Promise
and so this library uses Promises
under the hood. bluebird
was previously used as a dependency, but due to bundle size concerns on both node and the web it was replaced with native promises.
bluebird
is definitely more performant than native Promises. If performance is a major concern Promise
can be globally polyfilled
global.Promise = require('bluebird')
Promise
automatically gets assigned when using the script tag to load bluebird
var JSONAPIError = require('jsonapi-serializer').Error;
var error = new JSONAPIError(opts);
The function JSONAPIError takes one argument:
opts
: The error options. All options are optional.opts
argument)Examples
var JSONAPIError = require('jsonapi-serializer').Error;
var errors = new JSONAPIError({
code: '123',
source: { 'pointer': '/data/attributes/first-name' },
title: 'Value is too short',
detail: 'First name must contain at least three characters.'
});
// `errors` here are JSON API compliant.
The result will be something like:
{
"errors": [
{
"code": "123",
"source": { "pointer": "/data/attributes/first-name" },
"title": "Value is too short",
"detail": "First name must contain at least three characters."
}
]
}