# class-transformer-arkts **Repository Path**: ArkTSCentralRepository/class-transformer-arkts ## Basic Information - **Project Name**: class-transformer-arkts - **Description**: Nowadays you are working with classes and constructor objects more than ever. Class-transformer allows you to transform plain object to some instance of class and versa. - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-05-26 - **Last Updated**: 2025-05-26 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # class-transformer-arkts 基于[class-transformer]([class-transformer - npm](https://www.npmjs.com/package/class-transformer))原库0.5.1版本进行适配, 所有功能代码已经转换为`ArkTS`文件 ## Install ```sh ohpm install class-transformer-arkts ``` ## description Nowadays you are working with classes and constructor objects more than ever. Class-transformer allows you to transform plain object to some instance of class and versa. Also it allows to serialize / deserialize object based on criteria. This tool is super useful on both frontend and backend. ## Methods ### plainToClass This method transforms a plain object to instance of specific class. ```typescript import { plainToClass, Type, PlainObject, ClassTransformOptions } from 'class-transformer-arkts'; class Address { x?: number; y?: number; } class User { name?: string; age?: number; @Type(() => Address) address?: Address; } const address: PlainObject = { "x": 10, "y": 12 }; const user: PlainObject = { "name": "xxx", "age": 20, "address": address }; const options: ClassTransformOptions = { targetMaps: [{ target: User, properties: { "address": Address } }] }; const userObj: ESObject = classTransform.plainToClass(User, user); // 如果不支持装饰器@Type, 需要在ClassTransformOptions中标明非基本类型的属性的类型 // const userObj: ESObject = classTransform.plainToClass(User, user, options); expect(userObj instanceof User).assertTrue(); ``` ### plainToClassFromExist This method transforms a plain object into an instance using an already filled Object which is an instance of the target class. ```typescript import { plainToClassFromExist } from 'class-transformer-arkts'; const addressPlain: PlainObject = { "y": 12 }; const addressObj: Address = new Address(); addressObj.x = 10; const address: ESObject = plainToClassFromExist(addressObj, addressPlain); expect(address instanceof Address).assertTrue(); expect(address.x === 10 && address.y == 12).assertTrue(); ``` ### classToPlain This method transforms your class object back to plain javascript object, that can be `JSON.stringify` later. ```typescript import { classToPlain } from 'class-transformer-arkts'; const address = new Address(); address.x = 10; address.y = 12; const user = new User(); user.name = 'xxx'; user.age = 20; user.address = address; const userPlain = classToPlain(user) as PlainObject; expect(userPlain instanceof User).assertFalse(); expect(userPlain.address instanceof Address).assertFalse(); expect(JSON.stringify(userPlain)).assertEqual(`{"name":"xxx","age":20,"address":{"x":10,"y":12}}`); ``` ### classToClass This method transforms your class object into a new instance of the class object. This may be treated as deep clone of your objects. ```typescript import { classToClass } from 'class-transformer-arkts'; const address = new Address(); address.x = 10; address.y = 12; const user = new User(); user.name = 'cln'; user.age = 20; user.address = address; const newUser = classToClass(user) as User; expect(newUser instanceof User).assertTrue(); expect(newUser.address instanceof Address).assertTrue(); expect(user === newUser).assertFalse(); ``` You can also use an `ignoreDecorators` option in transformation options to ignore all decorators you classes is using. ### serialize You can serialize your model right to json using `serialize` method: ```typescript import { serialize } from 'class-transformer'; const address = new Address(); address.x = 10; address.y = 12; const user = new User(); user.name = 'xxx'; user.age = 20; user.address = address; const userString = serialize(user); expect(userString).assertEqual(`{"name":"xxx","age":20,"address":{"x":10,"y":12}}`); ``` `serialize` works with both arrays and non-arrays. ### deserialize and deserializeArray You can deserialize your model from json using the `deserialize` method: ```typescript import { deserialize, ClassTransformOptions } from 'class-transformer'; const userString = `{"name":"xxx","age":20,"address":{"x":10,"y":12}}`; const options: ClassTransformOptions = { targetMaps: [{ target: User, properties: { "address": Address } }] }; const user: ESObject = classTransform.deserialize(User, userString, options); expect(user instanceof User).assertTrue(); expect(user.address instanceof Address).assertTrue(); ``` To make deserialization work with arrays, use the `deserializeArray` method: ```typescript import { deserializeArray, ClassTransformOptions } from 'class-transformer'; const usersString = `[{"name":"xxx","age":20,"address":{"x":10,"y":12}},{"name":"yyy","age":22,"address":{"x":14,"y":26}}]`; const options: ClassTransformOptions = { targetMaps: [{ target: User, properties: { "address": Address } }] }; const users: ESObject[] = deserializeArray(User, usersString, options); expect(Array.isArray(users)).assertTrue(); expect(users.length === 2).assertTrue(); expect(users[0] instanceof User && users[1] instanceof User).assertTrue(); expect(users[0].address instanceof Address).assertTrue(); expect(users[0].name === 'xxx' && users[1].name === 'yyy').assertTrue(); ```