Search code examples
angulartypescriptclassclone

how to deep clone JSON to a class


I have a JSON object and I need to parse it to a class instance. The JSON object can be really large, and I need to execute methods from the classes within.

I tried Object.assign and the Lodash API. The Lodash API is the best approach with the merge or mergeWith function, but I came to an impasse when the class property is an array. Even with the Lodash I have to initialize the properties from the class (Which I don't like but if I have to I'll do it).

I used this post Deep Cloning in Actionscript and this Deep clone class instance JavaScript

and nothing seems to do what I really want.

Here is the stackblitz https://stackblitz.com/edit/angular-1rva1k

I need to be able to execute methods from the classes within.

Thanks for your help.


Solution

  • As @MichaelDesigaud sugested, I used the class-transformer. Sorry, I don't how to mark a comment as an answer.