How to use
Use your existing model objects, without any modification. For example:
And use your REST services, that go with your model:
Finally use JsonMix to deserialize the JSON into model objects like this:
With this simple tool you now have your JSON data deserialized into an object constructed from you model.
Using factories to create models
A factory can also be used to create models.
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:
To apply Epmloyee to the entire array available on given path:
A slightly more complex example would look like:
;.withObjectEmployee, 'employees'.withObjectPet, 'employees.pet'.build;// now you can use the nested model functionsconsole.logresult.employees.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;;