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

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

上级 a5bd6e13
......@@ -23,7 +23,7 @@ type PersonJSON = {
}
function countOccurrences(str:string, substr:string):number {
function countOccurrences(str : string, substr : string) : number {
let count = 0
let position = 0
......@@ -42,8 +42,17 @@ function countOccurrences(str:string, substr:string):number {
export function testJSON() : Result {
return describe("JSON", () => {
test('parse', () => {
// #TEST JSON.parse_tip,JSON.parse
const json = `{"result":true, "count":42}`;
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["result"] as boolean).toEqual(true);
......@@ -60,9 +69,10 @@ export function testJSON() : Result {
// });
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 obj2 = JSON.parse<UTSJSONObject>(json2)!;
// #END
// expect(obj2).toEqual({
// string: 'Hello',
// number: 42,
......@@ -131,18 +141,24 @@ export function testJSON() : Result {
})
test('parseObject', () => {
// #TEST JSON.parseObject
const json = `{"result":true, "count":42}`;
const obj = JSON.parseObject(json);
console.log(obj?.["count"])//42
// #END
expect(obj?.["count"]).toEqual(42);
expect(obj?.["result"] as boolean).toEqual(true);
expect(JSON.parseObject('{}')!).toEqual({});
// #TEST JSON.parseObject_1
const json1 = `{
"name": "John",
"id": "30"
}`;
let obj2 = JSON.parseObject<UserJSON>(json1);
console.log(obj2!.id) //30
// #END
expect(obj2!.id).toEqual("30");
const json2 = `{
......@@ -157,8 +173,11 @@ export function testJSON() : Result {
expect(obj4).toEqual(null);
})
test('parseArray', () => {
// #TEST JSON.parseArray
const json1 = `[1,2,3]`;
const array1 = JSON.parseArray(json1);
console.log(array1)//[1, 2, 3]
// #END
expect(array1).toEqual([1, 2, 3]);
const json2 = `[1,"hello world",3]`;
......@@ -167,8 +186,11 @@ export function testJSON() : Result {
// #ifdef APP-ANDROID
// #TEST JSON.parseArray_1
const json3 = `[{"name":"John","id":"30"},{"name":"jack","id":"21"}]`;
const array3 = JSON.parseArray<UTSJSONObject>(json3);
console.log((array3![0])["name"])//"John"
// #END
expect((array3![0])["name"]).toEqual("John");
// #endif
})
......@@ -197,50 +219,50 @@ export function testJSON() : Result {
})
test('stringify-with-replacer', () => {
// #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"}');
let a1 = JSON.stringify({"x":111,"y":"aaa"},function(key:any,value:any|null):any|null{
if(key == "x"){
let a1 = JSON.stringify({ "x": 111, "y": "aaa" }, function (key : any, value : any | null) : any | null {
if (key == "x") {
return "x"
}
return value
})
expect(a1).toEqual('{"x":"x","y":"aaa"}');
let a2 = JSON.stringify({"x":111,"y":"aaa"},function(key:any,value:any|null):any|null{
if(key == "x"){
let a2 = JSON.stringify({ "x": 111, "y": "aaa" }, function (key : any, value : any | null) : any | null {
if (key == "x") {
return "x"
}
return value
},6)
}, 6)
expect(a2.length).toEqual(36);
let a3 = JSON.stringify({"x":111,"y":"aaa"},function(key:any,value:any|null):any|null{
if(key == "x"){
let a3 = JSON.stringify({ "x": 111, "y": "aaa" }, function (key : any, value : any | null) : any | null {
if (key == "x") {
return "x"
}
return value
},11)
}, 11)
expect(a3.length).toEqual(44);
let a4 = JSON.stringify({"x":111,"y":"aaa"},function(key:any,value:any|null):any|null{
if(key == "x"){
let a4 = JSON.stringify({ "x": 111, "y": "aaa" }, function (key : any, value : any | null) : any | null {
if (key == "x") {
return "x"
}
return value
},-11)
}, -11)
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}}');
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}}');
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);
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(countOccurrences(a8,"99999")).toEqual(6);
expect(countOccurrences(a8, "99999")).toEqual(6);
// #endif
......@@ -254,6 +276,17 @@ export function testJSON() : Result {
// #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 json2 = JSON.stringify(obj2);
expect(json2).toEqual('["apple","banana","cherry"]');
......@@ -395,7 +428,7 @@ export function testJSON() : Result {
test('parse Map', () => {
type A = {
num: number,
num : number,
}
const map = JSON.parse<Map<string, A>>(`{"a": {"num": 1}}`)
// #ifndef APP-IOS
......@@ -407,9 +440,9 @@ export function testJSON() : Result {
test('parse generic type', () => {
// #ifndef APP-ANDROID
type A = {
num: number,
num : number,
}
function test<T>(json: string): T {
function test<T>(json : string) : T {
return JSON.parse<T>(json)!
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册