JsonConverter
convert json object's value. Usually use to convert server response data format, for instance from hateoas to jsonapi, If your server is spring data, and javascript side is Emberjs.
Design
Use visitor pattern.
entry point:
;
every visitor in visitors's visit method will be called with three parameters.
;
- parent, obj's parent object, for top object, it's value is null.
- key, parent[k] === obj.
- obj, the obj for you to convert.
as a convention, if your visitor doesn't change json's structure,you can put them as an array to traversal, or else it's better to traverse object separate. any way, you can decide how to implentation.
How to use Hateoas2Jsonapi
convert hateoas response to jsonapi, you must provide extra information to converter. For example, this is a response.
fixtrures/person.js
You have no idea of which type of model this response stand for. and you cannot decide what "roles" field mean, in the "_embedded" field, we can guess it, but it's not accurate. So, *** We must identify all the model type the response contains ***, In other word, You must know exactly your app's data structure.
in the test folder, there is a tutils.js, which is a configuration pass to converter.
{ return typePathMap: person: //for response for person model. /api/people/1, or /api/people role: "roles|_embedded/roles" // start from main model, roles, or _embedded.roles may contain role model. person: "_embedded/creator" task: "_embedded/tasks" ;}
Now, in test/hateoas2jsonapi/hateoas2jsonapi-test.js
let convertor = ; let p = ; let result = convertor; // for every convert action, pass in the main model name. // p is response from server.
For Emberjs Data
I'm not write a ember plugin, instead write a general convertor, you can import the converter in you ember app. just write some hook in adapter.