const jsl = require('jsl');
List of options
default values:
{
type: 'resource',
id: 'id',
attributes: [],
relationships: []
}
const toBeSerialized = {
id: 'unique_id',
fullName: 'Fabrício',
description: 'JSON API',
newEvent: '1'
};
jsl.serialize(toBeSerialized, {
type: 'users',
attributes: [
'fullName',
'description',
'anniversary'
],
relationships: [
{ rel: 'newEvent', type: 'events' }
]
});
Returns:
{
data: {
id: 'unique_id',
type: 'users',
attributes: {
'full-name': 'Fabrício',
description: 'JSON API',
anniversary: null
},
relationships: {
'new-event': {
data: {
id: '1',
type: 'events'
}
}
}
}
}
List of options
default values:
{
flat: true,
convertCase: 'camelCase' // one of: camelCase, kebabCase, snakeCase
}
const jsonapiObject = {
data: {
attributes: {
'full-name': 'Fabrício',
description: 'JSON API'
},
relationships: {
'created-by': {
data: {
type: 'people',
id: '1'
}
},
'modified-by': {
data: {
type: 'people',
id: '1'
}
}
}
}
};
jsl.deserialize(jsonapiObject, { flat: false });
Returns:
{
attributes: {
fullName: 'Fabrício',
description: 'JSON API'
},
relationships: {
createdBy: '1',
modifiedBy: '1'
}
}