jsonapi-serializer-lite

0.10.0 • Public • Published

jsonapi-serializer-lite

Usage

const jsl = require('jsl');

jsl.serialize(data[, options])

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'
        }
      }
    }
  }
}

jsl.deserialize(jsonapiObject[, options])

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'
  }
}

Readme

Keywords

Package Sidebar

Install

npm i jsonapi-serializer-lite

Weekly Downloads

35

Version

0.10.0

License

MIT

Unpacked Size

17.1 kB

Total Files

7

Last publish

Collaborators

  • fabriciotav