Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
@creately/jsonmix
Advanced tools
Easy to use deserialisation from JSON into JavaScript Objects complete with functions
JsonMix provides a kind of deserialisation from JSON into JavaScript Objects complete with functions.
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());
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.
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();
});
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();
FAQs
Easy to use deserialisation from JSON into JavaScript Objects complete with functions
We found that @creately/jsonmix demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 6 open source maintainers collaborating on the project.
Did you know?
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.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.