提交 b4a9a16b 编写于 作者: 杜庆泉's avatar 杜庆泉

增加自定制输出 JSON.stringify的示例

上级 1ec178eb
...@@ -38,9 +38,59 @@ function countOccurrences(str : string, substr : string) : number { ...@@ -38,9 +38,59 @@ function countOccurrences(str : string, substr : string) : number {
return count; return count;
} }
class A1 implements IJsonStringify{
override toJSON():any|null{
let jsonRet = {
'name': "zhangsan",
'age': 12,
}
return jsonRet
}
}
class A2 implements IJsonStringify{
override toJSON():any|null{
return 2
}
}
class A3 implements IJsonStringify{
override toJSON():any|null{
return "json"
}
}
class A4 implements IJsonStringify{
override toJSON():any|null{
return null
}
}
class A5 implements IJsonStringify{
override toJSON():any|null{
return new A1()
}
}
class A6 implements IJsonStringify{
override toJSON():any|null{
return new A5()
}
}
export function testJSON() : Result { export function testJSON() : Result {
return describe("JSON", () => { return describe("JSON", () => {
test('custom-stringify', () => {
expect(JSON.stringify(new A1()).length).toEqual(28)
expect(JSON.stringify(new A2())).toEqual("2")
expect(JSON.stringify(new A3())).toEqual('"json"')
expect(JSON.stringify(new A4())).toEqual('null')
expect(JSON.stringify(new A5()).length).toEqual(28)
expect(JSON.stringify(new A6()).length).toEqual(28)
})
test('parse', () => { test('parse', () => {
// #TEST JSON.parse_tip,JSON.parse // #TEST JSON.parse_tip,JSON.parse
const json = `{"result":true, "count":42}`; const json = `{"result":true, "count":42}`;
...@@ -264,9 +314,11 @@ export function testJSON() : Result { ...@@ -264,9 +314,11 @@ export function testJSON() : Result {
expect(a8.length).toEqual(73); expect(a8.length).toEqual(73);
expect(countOccurrences(a8, "99999")).toEqual(6); expect(countOccurrences(a8, "99999")).toEqual(6);
// #endif // #endif
}) })
test('stringify', () => { test('stringify', () => {
// #ifdef APP-ANDROID // #ifdef APP-ANDROID
......
...@@ -2,7 +2,7 @@ import { Matchers,NumberMatchers } from './Matchers.uts' ...@@ -2,7 +2,7 @@ import { Matchers,NumberMatchers } from './Matchers.uts'
export const describes = new Map<string, Result>() export const describes = new Map<string, Result>()
export class Result implements io.dcloud.uts.json.IJsonStringify { export class Result implements IJsonStringify {
total = 0 total = 0
passed: string[] = [] passed: string[] = []
failed: string[] = [] failed: string[] = []
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册