提交 6ee709d9 编写于 作者: M mahaifeng

[json]去除文档中手动生成的代码,添加注释

上级 a5bd6e13
...@@ -23,16 +23,16 @@ type PersonJSON = { ...@@ -23,16 +23,16 @@ type PersonJSON = {
} }
function countOccurrences(str:string, substr:string):number { function countOccurrences(str : string, substr : string) : number {
let count = 0 let count = 0
let position = 0 let position = 0
while (true) { while (true) {
position = str.indexOf(substr, position) position = str.indexOf(substr, position)
if (position == -1) break if (position == -1) break
count++ count++
position += substr.length position += substr.length
} }
return count; return count;
...@@ -42,8 +42,17 @@ function countOccurrences(str:string, substr:string):number { ...@@ -42,8 +42,17 @@ function countOccurrences(str:string, substr:string):number {
export function testJSON() : Result { export function testJSON() : Result {
return describe("JSON", () => { return describe("JSON", () => {
test('parse', () => { test('parse', () => {
// #TEST JSON.parse_tip,JSON.parse
const json = `{"result":true, "count":42}`; const json = `{"result":true, "count":42}`;
const obj = JSON.parse(json) as UTSJSONObject; const obj = JSON.parse(json) as UTSJSONObject;
console.log(obj["count"]);
// expected output: 42
console.log(obj["result"]);
// expected output: true
// #END
expect(obj["count"]).toEqual(42); expect(obj["count"]).toEqual(42);
expect(obj["result"] as boolean).toEqual(true); expect(obj["result"] as boolean).toEqual(true);
...@@ -60,9 +69,10 @@ export function testJSON() : Result { ...@@ -60,9 +69,10 @@ export function testJSON() : Result {
// }); // });
expect((obj1! as UTSJSONObject).getString('name')).toEqual("John") expect((obj1! as UTSJSONObject).getString('name')).toEqual("John")
// #TEST JSON.parse_1
const json2 = '{"string":"Hello","number":42,"boolean":true,"nullValue":null,"array":[1,2,3],"object":{"nestedKey":"nestedValue"}}'; const json2 = '{"string":"Hello","number":42,"boolean":true,"nullValue":null,"array":[1,2,3],"object":{"nestedKey":"nestedValue"}}';
const obj2 = JSON.parse<UTSJSONObject>(json2)!; const obj2 = JSON.parse<UTSJSONObject>(json2)!;
// #END
// expect(obj2).toEqual({ // expect(obj2).toEqual({
// string: 'Hello', // string: 'Hello',
// number: 42, // number: 42,
...@@ -131,18 +141,24 @@ export function testJSON() : Result { ...@@ -131,18 +141,24 @@ export function testJSON() : Result {
}) })
test('parseObject', () => { test('parseObject', () => {
// #TEST JSON.parseObject
const json = `{"result":true, "count":42}`; const json = `{"result":true, "count":42}`;
const obj = JSON.parseObject(json); const obj = JSON.parseObject(json);
console.log(obj?.["count"])//42
// #END
expect(obj?.["count"]).toEqual(42); expect(obj?.["count"]).toEqual(42);
expect(obj?.["result"] as boolean).toEqual(true); expect(obj?.["result"] as boolean).toEqual(true);
expect(JSON.parseObject('{}')!).toEqual({}); expect(JSON.parseObject('{}')!).toEqual({});
// #TEST JSON.parseObject_1
const json1 = `{ const json1 = `{
"name": "John", "name": "John",
"id": "30" "id": "30"
}`; }`;
let obj2 = JSON.parseObject<UserJSON>(json1); let obj2 = JSON.parseObject<UserJSON>(json1);
console.log(obj2!.id) //30
// #END
expect(obj2!.id).toEqual("30"); expect(obj2!.id).toEqual("30");
const json2 = `{ const json2 = `{
...@@ -157,8 +173,11 @@ export function testJSON() : Result { ...@@ -157,8 +173,11 @@ export function testJSON() : Result {
expect(obj4).toEqual(null); expect(obj4).toEqual(null);
}) })
test('parseArray', () => { test('parseArray', () => {
// #TEST JSON.parseArray
const json1 = `[1,2,3]`; const json1 = `[1,2,3]`;
const array1 = JSON.parseArray(json1); const array1 = JSON.parseArray(json1);
console.log(array1)//[1, 2, 3]
// #END
expect(array1).toEqual([1, 2, 3]); expect(array1).toEqual([1, 2, 3]);
const json2 = `[1,"hello world",3]`; const json2 = `[1,"hello world",3]`;
...@@ -167,82 +186,85 @@ export function testJSON() : Result { ...@@ -167,82 +186,85 @@ export function testJSON() : Result {
// #ifdef APP-ANDROID // #ifdef APP-ANDROID
// #TEST JSON.parseArray_1
const json3 = `[{"name":"John","id":"30"},{"name":"jack","id":"21"}]`; const json3 = `[{"name":"John","id":"30"},{"name":"jack","id":"21"}]`;
const array3 = JSON.parseArray<UTSJSONObject>(json3); const array3 = JSON.parseArray<UTSJSONObject>(json3);
console.log((array3![0])["name"])//"John"
// #END
expect((array3![0])["name"]).toEqual("John"); expect((array3![0])["name"]).toEqual("John");
// #endif // #endif
}) })
test('merge-test-1', () => { test('merge-test-1', () => {
// #ifdef APP-ANDROID // #ifdef APP-ANDROID
const data1 = { const data1 = {
name: 'Tom1', name: 'Tom1',
age: 21 age: 21
}; };
const data2 = { const data2 = {
aa: { aa: {
name: 'Tom2', name: 'Tom2',
age: 22, age: 22,
bb: { bb: {
name: 'Tom3', name: 'Tom3',
age: 23 age: 23
} }
}
} }
const obj = Object.assign(JSON.parse<UTSJSONObject>(JSON.stringify(data2))!, JSON.parse<UTSJSONObject>(JSON.stringify(data1))!) as UTSJSONObject; }
const innerObj = obj.getJSON("aa.bb") const obj = Object.assign(JSON.parse<UTSJSONObject>(JSON.stringify(data2))!, JSON.parse<UTSJSONObject>(JSON.stringify(data1))!) as UTSJSONObject;
expect(innerObj instanceof UTSJSONObject).toEqual(true); const innerObj = obj.getJSON("aa.bb")
expect(innerObj instanceof UTSJSONObject).toEqual(true);
// #endif // #endif
}) })
test('stringify-with-replacer', () => { test('stringify-with-replacer', () => {
// #ifdef APP-ANDROID // #ifdef APP-ANDROID
let a = JSON.stringify({"x":111,"y":"aaa"}) let a = JSON.stringify({ "x": 111, "y": "aaa" })
expect(a).toEqual('{"x":111,"y":"aaa"}'); expect(a).toEqual('{"x":111,"y":"aaa"}');
let a1 = JSON.stringify({"x":111,"y":"aaa"},function(key:any,value:any|null):any|null{ let a1 = JSON.stringify({ "x": 111, "y": "aaa" }, function (key : any, value : any | null) : any | null {
if(key == "x"){ if (key == "x") {
return "x" return "x"
} }
return value return value
}) })
expect(a1).toEqual('{"x":"x","y":"aaa"}'); expect(a1).toEqual('{"x":"x","y":"aaa"}');
let a2 = JSON.stringify({"x":111,"y":"aaa"},function(key:any,value:any|null):any|null{ let a2 = JSON.stringify({ "x": 111, "y": "aaa" }, function (key : any, value : any | null) : any | null {
if(key == "x"){ if (key == "x") {
return "x" return "x"
} }
return value return value
},6) }, 6)
expect(a2.length).toEqual(36); expect(a2.length).toEqual(36);
let a3 = JSON.stringify({"x":111,"y":"aaa"},function(key:any,value:any|null):any|null{ let a3 = JSON.stringify({ "x": 111, "y": "aaa" }, function (key : any, value : any | null) : any | null {
if(key == "x"){ if (key == "x") {
return "x" return "x"
} }
return value return value
},11) }, 11)
expect(a3.length).toEqual(44); expect(a3.length).toEqual(44);
let a4 = JSON.stringify({"x":111,"y":"aaa"},function(key:any,value:any|null):any|null{ let a4 = JSON.stringify({ "x": 111, "y": "aaa" }, function (key : any, value : any | null) : any | null {
if(key == "x"){ if (key == "x") {
return "x" return "x"
} }
return value return value
},-11) }, -11)
expect(a4.length).toEqual(19); expect(a4.length).toEqual(19);
let a5 = JSON.stringify({"x":111,"y":"aaa","z":{"x":123}},["x",'z']) let a5 = JSON.stringify({ "x": 111, "y": "aaa", "z": { "x": 123 } }, ["x", 'z'])
expect(a5).toEqual('{"x":111,"z":{"x":123}}'); expect(a5).toEqual('{"x":111,"z":{"x":123}}');
let a6 = JSON.stringify({"x":111,"y":"aaa","z":{"x":123}},["x",'y','z']) let a6 = JSON.stringify({ "x": 111, "y": "aaa", "z": { "x": 123 } }, ["x", 'y', 'z'])
expect(a6).toEqual('{"x":111,"y":"aaa","z":{"x":123}}'); expect(a6).toEqual('{"x":111,"y":"aaa","z":{"x":123}}');
let a7 = JSON.stringify({"x":111,"y":"aaa","z":{"x":123}},["x",'y','z'],8) let a7 = JSON.stringify({ "x": 111, "y": "aaa", "z": { "x": 123 } }, ["x", 'y', 'z'], 8)
expect(a7.length).toEqual(91); expect(a7.length).toEqual(91);
let a8 = JSON.stringify({"x":111,"y":"aaa","z":{"x":123}},["x",'y','z'],"99999") let a8 = JSON.stringify({ "x": 111, "y": "aaa", "z": { "x": 123 } }, ["x", 'y', 'z'], "99999")
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', () => {
...@@ -251,9 +273,20 @@ export function testJSON() : Result { ...@@ -251,9 +273,20 @@ export function testJSON() : Result {
// const obj1 = { name: 'John', age: 30, address: { city: 'New York', country: 'USA' } }; // const obj1 = { name: 'John', age: 30, address: { city: 'New York', country: 'USA' } };
// const json1 = JSON.stringify(obj1); // const json1 = JSON.stringify(obj1);
// expect(json1).toEqual('{"address":{"country":"USA","city":"New York"},"name":"John","age":30}'); // expect(json1).toEqual('{"address":{"country":"USA","city":"New York"},"name":"John","age":30}');
// #endif // #endif
// #TEST JSON.stringify
console.log(JSON.stringify({ x: 5, y: 6 }));
// expected output: "{"x":5,"y":6}"
console.log(JSON.stringify([3, 'false', boolean]));
// expected output: "[3,"false",false]"
console.log(JSON.stringify(new Date(2006, 0, 2, 15, 4, 5)));
// expected output: ""2006-01-02T15:04:05.000Z""
// #END
const obj2 = ['apple', 'banana', 'cherry']; const obj2 = ['apple', 'banana', 'cherry'];
const json2 = JSON.stringify(obj2); const json2 = JSON.stringify(obj2);
expect(json2).toEqual('["apple","banana","cherry"]'); expect(json2).toEqual('["apple","banana","cherry"]');
...@@ -343,7 +376,7 @@ export function testJSON() : Result { ...@@ -343,7 +376,7 @@ export function testJSON() : Result {
expect(JSON.stringify(obj22)).toEqual('{"data":[{}]}') expect(JSON.stringify(obj22)).toEqual('{"data":[{}]}')
// #endif // #endif
type A = { type A = {
inserted : number inserted : number
} }
...@@ -384,7 +417,7 @@ export function testJSON() : Result { ...@@ -384,7 +417,7 @@ export function testJSON() : Result {
a: 1 a: 1
}.toMap() }.toMap()
expect(map.get('a')).toEqual(1) expect(map.get('a')).toEqual(1)
const json = `{"result":true, "count":42}`; const json = `{"result":true, "count":42}`;
const obj = JSON.parse(json) as UTSJSONObject; const obj = JSON.parse(json) as UTSJSONObject;
let retStr = JSON.stringify(obj) let retStr = JSON.stringify(obj)
...@@ -395,7 +428,7 @@ export function testJSON() : Result { ...@@ -395,7 +428,7 @@ export function testJSON() : Result {
test('parse Map', () => { test('parse Map', () => {
type A = { type A = {
num: number, num : number,
} }
const map = JSON.parse<Map<string, A>>(`{"a": {"num": 1}}`) const map = JSON.parse<Map<string, A>>(`{"a": {"num": 1}}`)
// #ifndef APP-IOS // #ifndef APP-IOS
...@@ -407,18 +440,18 @@ export function testJSON() : Result { ...@@ -407,18 +440,18 @@ export function testJSON() : Result {
test('parse generic type', () => { test('parse generic type', () => {
// #ifndef APP-ANDROID // #ifndef APP-ANDROID
type A = { type A = {
num: number, num : number,
} }
function test<T>(json: string): T { function test<T>(json : string) : T {
return JSON.parse<T>(json)! return JSON.parse<T>(json)!
} }
// #ifndef APP-IOS // #ifndef APP-IOS
const a = test<A>(`{"num": 1}`) const a = test<A>(`{"num": 1}`)
expect(a.num).toEqual(1) expect(a.num).toEqual(1)
// #endif // #endif
// #endif // #endif
}) })
}) })
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册