diff --git a/README.md b/README.md index 1fb527c5e0a594ff02776b370da5ae5febbf7b6c..1c2c9d6f56a13a0ebed0d927a80fc3ff6f59634b 100644 --- a/README.md +++ b/README.md @@ -7,20 +7,23 @@ Sendable对象生成工具可以根据用户的配置,将用户自定义的tts 1. 从[Download Visual Studio Code](https://code.visualstudio.com/download)下载vscode并安装,或安装其他IDE。 2. 从[Download Node.js](https://nodejs.org/en/download/current)下载Node.js并安装,Node.js为JavaScript的运行时环境,自带包管理器npm。 ``` -3. 拉取bitfunjsontransformer仓库代码 +3. 拉取serialization_codegen仓库代码或者网页下载工具 +拉取工具仓库: ```shell -git clone --recursive git@gitee.com:BitFun4HarmonyOS/bitfunjsontransformer.git +git clone --recursive git@gitee.com:openharmony-sig/serialization_codegen.git or -git clone --recursive https://gitee.com/BitFun4HarmonyOS/bitfunjsontransformer.git +git clone --recursive https://gitee.com/openharmony-sig/serialization_codegen.git ``` +或从网页直接下载工具: +https://gitee.com/openharmony-sig/serialization_codegen/tree/tts-transformer 4. 安装npm依赖 ```shell -cd bitfunjsontransformer +cd serialization_codegen npm install ``` 5. 利用webpack打包 ```shell -cd bitfunjsontransformer +cd serialization_codegen npm run pack ``` #### 使用说明 @@ -64,6 +67,137 @@ cd bitfunjsontransformer/dist "sendable-transformer" : "file:../har/library.har" } ``` +4. tts文件编写 +```shell +import { collections } from '@kit.ArkTS' +import { Expose, Exclude, Type } from 'class-transformer' + +const enum TestEnum { + FIRST = 1, + SECOND = 2, + THIRD = 4, + FOURTH = 8, +} + +export class TestA { + data?:string; + field?:number; + testMethod():void { + console.log('testMethod in TestA'); + } + static fromAnotherJson(jsonObj:object): TestA { + let ans = new TestA(); + ans.field = jsonObj['field'] ?? 1.0; + return ans; + } + static toAnotherJson(target:TestA): object { + return target; + } +} + +// 定义一个抽象类 +export abstract class TestCommon { + commonField?:string; + __type?:string; +} + +export class TestSubA extends TestCommon { + fieldA?:number; + testMethod():void { + console.log('testMethod in TestSubA'); + } +} + +export class TestSubB extends TestCommon { + fieldB?:number; + testMethod():void { + console.log('testMethod in TestSubB'); + } +} + +// TestB中的mergeFrom方法中反序列化后调用postProcess() +@PostProcess +export class TestPostProcess { + field?:number; + postProcess() { + console.log('postProcess() in TestPostProcess'); + } +} + +export class TestB { + // 可以使用@Expose根据name参数指定变量别名 + @Expose({ name : 'string_alias' }) + stringField:string; + // 使用@Exclude可以将变量排除在mergeFrom和toJson之外 + @Exclude() + stringField2:string; + numberField:number; + objectField:object; + @Expose({ name : 'boolean_array_alias' }) + booleanArray:collections.Array; + numberArray:number[]; + bigintArray:bigint[]; + objectArray:object[]; + stringMap:Map; + booleanMap:Map; + @Expose({ name : 'number_map_alias' }) + numberMap:Map; + bigintMap:collections.Map; + objectMap:Map; + stringSet:Set; + booleanSet:Set; + numberSet:Set; + @Expose({ name : 'bigint_set_alias' }) + bigintSet:Set; + objectSet:collections.Set; + + // ENUM场景 + @Expose({ name : 'enum_alias' }) + enumField:TestEnum; + enumArray:TestEnum[]; + enumMap:Map; + enumSet:Set; + + // 嵌套类型场景, TestB中使用TestA类型属性 + @Type(() => TestA) + nestedField?:TestA; + @Type(() => TestA) + nestedFieldArray:TestA[]; + @Type(() => TestA) + nestedFieldMap:Map; + + // 平铺场景 + @Type(() => TestA, {passThrough: true}) + metaA?:TestA; + + // @Transform 自定义fromJson和toJson + // 可以搭配passThrough使用 + @Transform((value:object) => TestA.fromAnotherJson(value), {toClassOnly: true, passThrough: true}) + @Transform((value:TestA) => TestA.toAnotherJson(value), {toPlainOnly: true}) + transformField?: TestA; + + // 继承场景,需要配置抽象类TestCommon的子类 + @Type(() => TestCommon, { + discriminator: { + property: '__type', + subTypes: [ + { value: TestSubA, name: 'TestSubA' }, + { value: TestSubB, name: 'TestSubB' }, + ], + }, + }) + subclassField?:TestCommon; + + @PostProcess + @Type(() => TestPostProcess) + postProcessField?:TestPostProcess; + + testMethod(): void { + console.log('Test Primitive.' + this.stringField); + } +} + +``` #### 软件架构 SRConfigBuilder: 负责解析用户自定义的ttsconfig.json5配置文件,扫描到所有用户配置的tts文件路径,日志级别,日志文件路径,新生成文件是否加入.gitignore等等配置。 diff --git a/dist/README.md b/dist/README.md index 1fb527c5e0a594ff02776b370da5ae5febbf7b6c..1c2c9d6f56a13a0ebed0d927a80fc3ff6f59634b 100644 --- a/dist/README.md +++ b/dist/README.md @@ -7,20 +7,23 @@ Sendable对象生成工具可以根据用户的配置,将用户自定义的tts 1. 从[Download Visual Studio Code](https://code.visualstudio.com/download)下载vscode并安装,或安装其他IDE。 2. 从[Download Node.js](https://nodejs.org/en/download/current)下载Node.js并安装,Node.js为JavaScript的运行时环境,自带包管理器npm。 ``` -3. 拉取bitfunjsontransformer仓库代码 +3. 拉取serialization_codegen仓库代码或者网页下载工具 +拉取工具仓库: ```shell -git clone --recursive git@gitee.com:BitFun4HarmonyOS/bitfunjsontransformer.git +git clone --recursive git@gitee.com:openharmony-sig/serialization_codegen.git or -git clone --recursive https://gitee.com/BitFun4HarmonyOS/bitfunjsontransformer.git +git clone --recursive https://gitee.com/openharmony-sig/serialization_codegen.git ``` +或从网页直接下载工具: +https://gitee.com/openharmony-sig/serialization_codegen/tree/tts-transformer 4. 安装npm依赖 ```shell -cd bitfunjsontransformer +cd serialization_codegen npm install ``` 5. 利用webpack打包 ```shell -cd bitfunjsontransformer +cd serialization_codegen npm run pack ``` #### 使用说明 @@ -64,6 +67,137 @@ cd bitfunjsontransformer/dist "sendable-transformer" : "file:../har/library.har" } ``` +4. tts文件编写 +```shell +import { collections } from '@kit.ArkTS' +import { Expose, Exclude, Type } from 'class-transformer' + +const enum TestEnum { + FIRST = 1, + SECOND = 2, + THIRD = 4, + FOURTH = 8, +} + +export class TestA { + data?:string; + field?:number; + testMethod():void { + console.log('testMethod in TestA'); + } + static fromAnotherJson(jsonObj:object): TestA { + let ans = new TestA(); + ans.field = jsonObj['field'] ?? 1.0; + return ans; + } + static toAnotherJson(target:TestA): object { + return target; + } +} + +// 定义一个抽象类 +export abstract class TestCommon { + commonField?:string; + __type?:string; +} + +export class TestSubA extends TestCommon { + fieldA?:number; + testMethod():void { + console.log('testMethod in TestSubA'); + } +} + +export class TestSubB extends TestCommon { + fieldB?:number; + testMethod():void { + console.log('testMethod in TestSubB'); + } +} + +// TestB中的mergeFrom方法中反序列化后调用postProcess() +@PostProcess +export class TestPostProcess { + field?:number; + postProcess() { + console.log('postProcess() in TestPostProcess'); + } +} + +export class TestB { + // 可以使用@Expose根据name参数指定变量别名 + @Expose({ name : 'string_alias' }) + stringField:string; + // 使用@Exclude可以将变量排除在mergeFrom和toJson之外 + @Exclude() + stringField2:string; + numberField:number; + objectField:object; + @Expose({ name : 'boolean_array_alias' }) + booleanArray:collections.Array; + numberArray:number[]; + bigintArray:bigint[]; + objectArray:object[]; + stringMap:Map; + booleanMap:Map; + @Expose({ name : 'number_map_alias' }) + numberMap:Map; + bigintMap:collections.Map; + objectMap:Map; + stringSet:Set; + booleanSet:Set; + numberSet:Set; + @Expose({ name : 'bigint_set_alias' }) + bigintSet:Set; + objectSet:collections.Set; + + // ENUM场景 + @Expose({ name : 'enum_alias' }) + enumField:TestEnum; + enumArray:TestEnum[]; + enumMap:Map; + enumSet:Set; + + // 嵌套类型场景, TestB中使用TestA类型属性 + @Type(() => TestA) + nestedField?:TestA; + @Type(() => TestA) + nestedFieldArray:TestA[]; + @Type(() => TestA) + nestedFieldMap:Map; + + // 平铺场景 + @Type(() => TestA, {passThrough: true}) + metaA?:TestA; + + // @Transform 自定义fromJson和toJson + // 可以搭配passThrough使用 + @Transform((value:object) => TestA.fromAnotherJson(value), {toClassOnly: true, passThrough: true}) + @Transform((value:TestA) => TestA.toAnotherJson(value), {toPlainOnly: true}) + transformField?: TestA; + + // 继承场景,需要配置抽象类TestCommon的子类 + @Type(() => TestCommon, { + discriminator: { + property: '__type', + subTypes: [ + { value: TestSubA, name: 'TestSubA' }, + { value: TestSubB, name: 'TestSubB' }, + ], + }, + }) + subclassField?:TestCommon; + + @PostProcess + @Type(() => TestPostProcess) + postProcessField?:TestPostProcess; + + testMethod(): void { + console.log('Test Primitive.' + this.stringField); + } +} + +``` #### 软件架构 SRConfigBuilder: 负责解析用户自定义的ttsconfig.json5配置文件,扫描到所有用户配置的tts文件路径,日志级别,日志文件路径,新生成文件是否加入.gitignore等等配置。