And the deserialization: let obj: MyObj = JSON.parse(jsonContent); I'm actually struggling with deserializing and accessing the values in the resulting object. Could please one show me how to properly deserialize the above json into the corresponding class with TypeScript and how to access the values? Any help is highly appreciated.
In this tutorial, we will take a JSON string, and parse it to an object in TypeScript.
To parse a JSON string in TypeScript, you can use JSON.parse().
In this example, we will take a JSON string and parse it.
example.ts
Output
In this example, we will take a JSON string and parse it to a TypeScript class object.
example.ts
The parsed JSON string is loaded to an object of specified TypeScript class. And we can access the properties of class object just like we access the elements of JSON object, using dot operator.
Output
In this TypeScript Tutorial, we learned how to parse a JSON string to a JSON object or to a custom TypeScript class object.
It is a common requirement to load a nested Javascript class from a JSON file. Sometimes you want to send the JSON across a network and load it back to a Javascript class.
For better clarity, let us understand the difference between a Javascript class instance and an Object. Take this class for example:
This is a class that draws a rectangle on the canvas
Suppose you have an instance of this rectangle and that you want to save the rectangle to a file. The easy solution would be to use JSON.stringify(rect)
. Once the object is converted into JSON string, it is easy to save to a file or send through wire to be saved to a database.
However, how will you load the rectangle back from persistence?
You can use JSON.parse() to parse the string to an object like this:
However, you can’t call obj.draw() or obj.area() because obj is a plain Javascript object, not an instance of Rectangle()
You can create and load Rectangle() from the plain object:
This solution can work; but it can become too verbose as you add more shapes and constructs that contain other shapes.
The module class-transformer makes it easier to serialize nested class objects, and arrays in to a string. Later, you can reconstruct the same nested structures from the string.
Let us convert a JSON string to a Rectangle instance:
You can transform from plain object to class object as well. Example below:
When one object contains another object, the contained objects also can be serialized. Use the @Type()
decorator.
The canvas and context objects of the Drawing class are runtime only and we don’t want to save those. In order to exclude those members, use the @Exclude() decorator.
We have different types of drawing objects - like Circle, Rectangle, Line etc.All the objects inherit from an abstract class Shape. This works well for drawing making use of the polymorphic features.
But, how do we save all the different types of objects in the shapes array?class-transformer has a discriminator feature precisely for this purpose.
Here is how we can do it:
The @Type()
decorator can access a discriminator option. The property
indicates the class of the object. Then we provide the class names and their corresponding types.
Here is the serialized JSON of a Drawing:
We can now deserialize to a drawing object and call the member functions of the Drawing class. Like so:
As more objects are added and each having a large set of customizable attributes, the JSON becomes too complex and the attributes redundant.For example, check out this drawing:
Notice that the stroke class has default values saved and that makes the JSON export too large with redundant values.One way to avoid this redundancy is to skip saving the default values. However, there is no feature at the moment to skip the default value.The good news is that adding a custom decorator is easy.
Let us add an ExcludeDefault() decorator:
ExcludeDefault
is really a thin wrapper around the Transform() decorator that class-transformer provides. It checks the value is default value itself and then skips saving it if default.
Let us now use this custom decorator to save the drawing above.
Then if we serialize the drawing, you can see that the generated JSON is much more simplified:
As you can see, it is not very difficult to define custom decorators. Decorators can be used to define custom object models that fit the requirements of your application. Most of the time, Transform()
itself can be used for the data transformations.