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

@creately/jsonmix

Package Overview
Dependencies
Maintainers
6
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@creately/jsonmix

Easy to use deserialisation from JSON into JavaScript Objects complete with functions

  • 3.1.0
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
3
decreased by-50%
Maintainers
6
Weekly downloads
 
Created
Source

JsonMix

JsonMix provides a kind of deserialisation from JSON into JavaScript Objects complete with functions.

How to use

import { JsonMix } from '@creately/jsonmix';

Use your existing model objects, without any modification. For example:

class Employee {
  public firstName: string;
  public lastName: string;

  constructor(public id: string) {
	//...
  }

  public getName() {
    return this.firstName + ' ' + this.lastName;
  }
}

And use your REST services, that go with your model:

{
	"id": "001",
	"firstName": "John",
	"lastName": "Doe"
}

Finally use JsonMix to deserialize the JSON into model objects like this:

const mixer = new JsonMix(json);
const employee = await mixer.withObject(Employee).build();

With this simple tool you now have your JSON data deserialized into an object constructed from you model.

console.log(employee.getName());

Using factories to create models

A factory can also be used to create models.

const mixer = new JsonMix(json);
const employee = await mixer.withObject(data => new Employee(data.id)).build();

The factory can also return a prosmise which resolves to the model.

Multiple objects in a JSON

It's not uncommon to have multiple objects in a single JSON file, for example if you have an array of Employees in a JSON:

{
	"employees": [
		{
			"id": "001",
			"firstName": "John",
			"lastName": "Doe"
		},
		{
			"id": "002",
			"firstName": "John",
			"lastName": "Malkowich"
		},
	]
}

To apply Epmloyee to the entire array available on given path:

const mixer = new JsonMix(json);
const employees = await mixer.withObject(Employee, 'employees').build();
});

Further examples

A slightly more complex example would look like:

const mixer = new JsonMix(json);
const result = await mixer
	.withObject(Employee, 'employees')
	.withObject(Pet, 'employees.pet')
	.build();

// now you can use the nested model functions
console.log(result.employees[1].pet.getName());

The "path" can be any chain of nested objects separated with a dot. For example in this case "epmloyees.pet". JsonMix will find out if an object is an array, and will recursively apply the remaining part of the path to every item in it. You can also use "*" in the path, and this will apply the remaining path to every item in the object (even if it's not an array).

// this recursively applies the same prototype 3 levels deep
const mixer = new JsonMix(json);
const result = await mixer.withObject(Comparable, '*.*.*').build();

Keywords

FAQs

Package last updated on 22 May 2019

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