You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was trying to... serialize/deserialize a class with some transformation (rounding numbers to 3rd digit) and serialize/deserialize class that contains plain class inside to test if transformation in a nested class will be picked up automatically.
The problem:
When I serialize the class, transformations work. However, when I try to deserialize using plainToInstance as per this repo's readme file, I do not receive instance of the object but rather a string back which is not as the documentation suggests.
This is pretty similar to the basic usage scenario from the documentation so I am puzzled why is it not transforming into an object, perhaps I am missing something and/or the docs need an update?
/* eslint-disable @typescript-eslint/no-explicit-any */import'reflect-metadata';import{Expose,instanceToPlain,plainToInstance,serialize,Transform,Type,}from'class-transformer';// Simple class with rounding when serialized (but not when deserialized)classPoint{
@Transform(({ value })=>parseFloat(value.toFixed(3)),{toPlainOnly: true})x: number;
@Transform(({ value })=>parseFloat(value.toFixed(3)),{toPlainOnly: true})y: number;constructor(x: number,y: number){this.x=x;this.y=y;}getMe(){console.log('Point: getMe',this.x,this.y);}}// class using subclass with transformations and a methodclassTwoPoints{
@Expose()
@Type(()=>Point)one: Point;
@Expose()
@Type(()=>Point)two: Point;constructor(one: Point,two: Point){this.one=one;this.two=two;}getMe(){console.log('TwoPoint: getMe',this.one,this.two);}}// first test rounding when serializing the simple Point - works OKconstp=newPoint(3.4559,1.4449);constplainJson=JSON.stringify(p);constresult1=serialize(p);constresult2=JSON.stringify(instanceToPlain(p));console.log('Point',result1,'or',result2);// now let's test if rounding works when we serialize complex TwoPoints - works OKconstp2=newPoint(10.9999,11.3234);consttwoPoints=newTwoPoints(p,p2);constresult3=serialize(twoPoints);constresult4=JSON.stringify(instanceToPlain(twoPoints));constresult5=JSON.stringify(twoPoints);console.log('Two points',result3,'or',result4);// now let's try to deserialize Point to instance - FAILSconsole.log('Point source',plainJson);constnewPoint=plainToInstance(Point,plainJson);console.log('Point result',newPointinstanceofPoint,newPoint);// FAIL: not an instanceofnewPoint.getMe();// Fails: Not a function// deserialize TwoPoint to instance - FAILSconsole.log('Source',result5);constnewTwoPoint=plainToInstance(TwoPoints,result5);console.log('Result',newTwoPointinstanceofTwoPoints,newTwoPoint);// FAIL: not an instanceofnewTwoPoint.getMe();// Fails: Not a function
The text was updated successfully, but these errors were encountered:
I was trying to... serialize/deserialize a class with some transformation (rounding numbers to 3rd digit) and serialize/deserialize class that contains plain class inside to test if transformation in a nested class will be picked up automatically.
The problem:
When I serialize the class, transformations work. However, when I try to deserialize using
plainToInstance
as per this repo's readme file, I do not receive instance of the object but rather a string back which is not as the documentation suggests.This is pretty similar to the basic usage scenario from the documentation so I am puzzled why is it not transforming into an object, perhaps I am missing something and/or the docs need an update?
A simple code demonstrating what I done is here https://stackblitz.com/edit/class-transformer-playground-3u1swce5?file=index.ts
In case the stackblitz does not work for some reason here is the code as well
The text was updated successfully, but these errors were encountered: