New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

jsonade

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jsonade

Json de/serialization made for consistency

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
60
increased by172.73%
Maintainers
1
Weekly downloads
 
Created
Source

Jsonade

De/serialize json for consistency!

Build Status

Installation

$ npm install ...

Examples

Basic example

Create a serializer
const { Serializer } = require('jsonade');

const userSerializer = new Serializer('user', {
  attributes: ['firstName', 'lastName', 'address'],
  address: {
    attributes: ['street', 'number'],
  },
});
Serialize a single resource
const data = {
  firstName: 'John',
  lastName: 'Doe',
  age: 27,
  address: {
    street: 'Markt',
    number: '100',
    city: 'Zonnedorp',
    country: 'Belgium',
  },
};

const result = userSerializer.serialize(data);

//
// {
//   firstName: 'John',
//   lastName: 'Doe',
//   address: {
//     street: 'Markt',
//     number: '100',
//   },
// }
Serialize a list of resources
const data = [
	{
	  firstName: 'John',
	  lastName: 'Doe',
	  age: 27,
	  address: {
	    street: 'Markt',
	    number: '100',
	    city: 'Zonnedorp',
	    country: 'Belgium',
	  },
	}, {
	  firstName: 'Jessie',
	  lastName: 'Doe',
	  age: 26,
	  address: {
	    street: 'Marketstreet',
	    number: '101',
	    city: 'Sunvillage',
	    country: 'United Kingdom',
	  },
	}
];

const result = userSerializer.serialize(data);

// result:
// [
//   {
//     firstName: 'John',
//     lastName: 'Doe',
//     address: {
//       street: 'Markt',
//       number: '100',
//     },
//   },
//   { ... },
// ]

Example using a nested serializer

const { Serializer } = require('jsonade');

const addressSerializer = new Serializer('address', {
  attributes: ['street', 'number'],
});

const userSerializer = new Serializer('user', {
  attributes: ['firstName', 'lastName', 'address'],
  address: addresssSerializer,
});

const data = {
  firstName: 'John',
  lastName: 'Doe',
  age: 27,
  address: {
    street: 'Markt',
    number: '100',
    city: 'Zonnedorp',
    country: 'Belgium',
  },
};

const result = userSerializer.serialize(data);

// result:
// {
//   firstName: 'John',
//   lastName: 'Doe',
//   address: {
//     street: 'Markt',
//     number: '100',
//   },
// }

Example using a function to transform one property

const { Serializer } = require('jsonade');

const userSerializer = new Serializer('user', {
  attributes: ['firstName', 'lastName', 'age'],
  age: val => `${val} years old`,
});

const data = {
  firstName: 'John',
  lastName: 'Doe',
  age: '27',
};

const result = userSerializer.serialize(data);

// result:
// {
//   firstName: 'John',
//   lastName: 'Doe',
//   age: '27 years old',
// }

Example serializing an error

const { ErrorSerializer } = require('jsonade');
const errorResponse = ErrorSerializer.serialize(ex);

Serializer

constructor

const mySerializer = new Serializer(resource, options);
Arguments

resource (string): Name of the resource being serialized

options (Object): Options for serialisation

Options:

  • attributes (Array): attributes to serialize
Returns

Returns an instance of a custom serializer, ready to use for serializing data.

Serializer instance

serialize

mySerializer.serialize(data);
Arguments

data (Object|Array): Dataset to serialize.

Returns

Returns a serialized data respresentation of the given data.

ErrorSerializer

serialize

Arguments

error (Object|Array): Error(s) to serialize.

const errors = ErrorSerializer.serialize([error]);
Returns
  • errors (Object): object containing multiple errors.

Every error can have these properties:

key
statusrequired
titlerequired
idoptional
codeoptional, application specific
detailoptional
metaoptional
Example
{
  "errors": [
    {
      "id": "ba4b9f14-5b83-4dfd-ac46-1c3868e1b3ec",
        "status": 400,
        "code": "2006",
        "title": "Article not found",
        "detail": "Article with id 892bb574-090d-4d63-a5b5-cb928d5f5c5f not found",
        "meta": {
          "stack": "NotFoundError: ..."
        }
    }
  ]
}

FAQs

Package last updated on 10 Nov 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc