Skip to content

Feature request: built in method to get Typescript instances from JSON #7233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
baloodevil opened this issue Feb 25, 2016 · 5 comments
Closed
Labels
Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript

Comments

@baloodevil
Copy link

The capability has been discussed here and here and here. I would like to see a built-in way of doing this in the Typescript language itself. Sort of like a deserializer.

JSON is the language of data. Typescript will have greater adoption if there is an easy way to 'cast' a JSON string (or the javascript object created from it) into an instance of a Typescript class. If it is just the javascript object, it doesn't have methods of the class and items initialized with the constructor.

In the links above, one challenge is complex classes, ie: instance of Car class that has instance of Engine class in the same JSON. There is not an elegant way of doing this. The closest is to adopt a convention for metadata in the JSON which shows the subclass name. For example, a property like name = "Engine", so the 'deserializer' knows what type of class to instantiate for the subclass items. One suggestion to make the convention less arbitrary would be to use metadata that Typescript adds to the javascript when using Typescript's reflection. If that was the approach, it would probably make sense to also create a 'serializer' that when stringifying the Typescript instance, also included the necessary metadata.

Could there be a way to accomplish this with Typescript declaration merging or mixins?

@yuit yuit added the Suggestion An idea for TypeScript label Feb 25, 2016
@stunaz
Copy link

stunaz commented Apr 1, 2016

Looking forward for it.
Have a look at this solution https://github.com/pleerock/serializer.ts

@mhegazy
Copy link
Contributor

mhegazy commented Apr 22, 2016

duplicate of #2064

@mhegazy mhegazy added the Duplicate An existing issue was already created label Apr 22, 2016
@baloodevil
Copy link
Author

@mhegazy I believe #7233 and #2064 are distinct. #2064 is focused on validating json against a type definition. #7233 is focused on creating an instance of an object according to a type definition. The latter is similar to what underscore and jquery libraries do with "extend".

@baloodevil
Copy link
Author

@stunaz Great! That is exactly what is needed. I hope something like this can be integrated into the Typescript library itself. Re: "Since Typescript does not have good reflection abilities yet, we must implicitly specify what type of object each property contain." Could properties created with emitDecoratorMetadata be used for this purpose to avoid new annotations specific to your lib? Minor tweak suggestion: change "Skip" to "NotMapped" (term from Entity Framework code first annotations).

@mhegazy
Copy link
Contributor

mhegazy commented Apr 27, 2016

Providing a runtime library and/or builtin reflection capabilities are out of scope of the typescript project for the time being.

@mhegazy mhegazy added Out of Scope This idea sits outside of the TypeScript language design constraints and removed Duplicate An existing issue was already created labels Apr 27, 2016
@mhegazy mhegazy closed this as completed Apr 27, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants