提交 a5bd6e13 编写于 作者: M mahaifeng

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

上级 93ca2b13
...@@ -2,6 +2,7 @@ import { describe, test, expect, expectNumber, Result } from './tests.uts' ...@@ -2,6 +2,7 @@ import { describe, test, expect, expectNumber, Result } from './tests.uts'
export function testUTSJSONObject() : Result { export function testUTSJSONObject() : Result {
return describe("utsjsonobject", () => { return describe("utsjsonobject", () => {
test('keys', () => { test('keys', () => {
let obj = { let obj = {
name:"zhangsan", name:"zhangsan",
...@@ -9,6 +10,15 @@ export function testUTSJSONObject() : Result { ...@@ -9,6 +10,15 @@ export function testUTSJSONObject() : Result {
} }
expect(UTSJSONObject.keys(obj).length).toEqual(2); expect(UTSJSONObject.keys(obj).length).toEqual(2);
console.log(UTSJSONObject.keys(obj)) console.log(UTSJSONObject.keys(obj))
// #TEST UTSJSONObject.keys
let obj = {
name: "zhangsan",
age: 11
}
let ret1 = UTSJSONObject.keys(obj).length
console.log(ret1) //2
// #END
}) })
test('toJSONObject', () => { test('toJSONObject', () => {
...@@ -38,49 +48,67 @@ export function testUTSJSONObject() : Result { ...@@ -38,49 +48,67 @@ export function testUTSJSONObject() : Result {
test('assign-notype', () => { test('assign-notype', () => {
// #ifdef APP-ANDROID // #ifdef APP-ANDROID
const target = { a: 1, b: 2 }; // #TEST UTSJSONObject.assign
const source = { b: 4, c: 5 }; let target = { a: 1, b: 2 };
let source = { b: 4, c: 5 };
// 得到一个UTSJSONObject对象
const returnedTarget = UTSJSONObject.assign(target, source); const returnedTarget = UTSJSONObject.assign(target, source);
// #END
expect(returnedTarget.toMap().size).toEqual(3); expect(returnedTarget.toMap().size).toEqual(3);
type A = { type A = {
username:string username : string
age:number age : number
} }
type C = { type C = {
work:number work : number
} }
let b = UTSJSONObject.assign( { let b = UTSJSONObject.assign({
username:"张三", username: "张三",
age:12 age: 12
} as A, } as A,
{b:2}, { b: 2 },
{ {
work:0.002 work: 0.002
} as C) } as C)
expect(b['age']).toEqual(12); expect(b['age']).toEqual(12);
expect(b['username']).toEqual("张三"); expect(b['username']).toEqual("张三");
expect(b['b']).toEqual(2); expect(b['b']).toEqual(2);
expect(b['work']).toEqual(0.002); expect(b['work']).toEqual(0.002);
// #TEST UTSJSONObject.assign_1
type User = {
a : number
b : number
}
let target1 = { a: 1, b: 2 };
let source1 = { b: 4, c: 5 };
// 得到一个User对象
const returnedTarget = UTSJSONObject.assign<User>(target1, source1);
// #END
// #endif // #endif
}) })
test('setvalue-after-get', () => { test('setvalue-after-get', () => {
// #ifdef APP-ANDROID // #ifdef APP-ANDROID
// #TEST UTSJSONObject.getJSON,UTSJSONObject.getArray
let obj = { let obj = {
"cars":[ "cars": [
{ {
name:"car1", name: "car1",
value:100 value: 100
} }
] ]
} }
let cars = obj.getArray<UTSJSONObject>("cars") let cars = obj.getArray<UTSJSONObject>("cars")
cars![0].set("value",20) cars![0].set("value", 20)
let firstCar = obj.getJSON("cars[0]") let firstCar = obj.getJSON("cars[0]")
console.log(firstCar!['value'])//20
// #END
expect(firstCar!['value']).toEqual(20); expect(firstCar!['value']).toEqual(20);
// #endif // #endif
}) })
...@@ -88,7 +116,8 @@ export function testUTSJSONObject() : Result { ...@@ -88,7 +116,8 @@ export function testUTSJSONObject() : Result {
test('get-speed', () => { test('get-speed', () => {
// #ifdef APP-ANDROID // #ifdef APP-ANDROID
const utsObj: UTSJSONObject = {} as any as UTSJSONObject // #TEST UTSJSONObject.getString
const utsObj : UTSJSONObject = {} as any as UTSJSONObject
for (let i = 0; i < 100; i++) { for (let i = 0; i < 100; i++) {
utsObj.set('' + i, '' + i) utsObj.set('' + i, '' + i)
} }
...@@ -98,6 +127,7 @@ export function testUTSJSONObject() : Result { ...@@ -98,6 +127,7 @@ export function testUTSJSONObject() : Result {
for (let i = 0; i < 10000; i++) { for (let i = 0; i < 10000; i++) {
utsObj.getString('0') utsObj.getString('0')
} }
// #END
let spendTime = Date.now() - startTime let spendTime = Date.now() - startTime
expect(spendTime < 300).toEqual(true); expect(spendTime < 300).toEqual(true);
// #endif // #endif
...@@ -106,8 +136,8 @@ export function testUTSJSONObject() : Result { ...@@ -106,8 +136,8 @@ export function testUTSJSONObject() : Result {
test('assign-withtype', () => { test('assign-withtype', () => {
// #ifdef APP-ANDROID // #ifdef APP-ANDROID
type User = { type User = {
a:number a : number
b:number b : number
} }
const target = { a: 1, b: 2 }; const target = { a: 1, b: 2 };
const source = { b: 4, c: 5 }; const source = { b: 4, c: 5 };
...@@ -117,5 +147,83 @@ export function testUTSJSONObject() : Result { ...@@ -117,5 +147,83 @@ export function testUTSJSONObject() : Result {
console.log(returnedTarget) console.log(returnedTarget)
// #endif // #endif
}) })
test('getArray_1', () => {
// #ifdef APP-ANDROID
// #TEST UTSJSONObject.getArray_1
//这个方法用来获取指定元素类型的数组
let obj = JSON.parseObject('{"name":"tom","tag":["student","user"]}')
// 这里得到是 Array<*>
let noGenericArray = obj!.getArray("tag")
console.log(noGenericArray)
// 这里得到是 Array<string>
let genericArray = obj!.getArray<string>("tag")
console.log(genericArray)
// #END
// #endif
})
test('sample', () => {
// #TEST UTSJSONObject.sample_create,UTSJSONObject.get,UTSJSONObject.set
const person : UTSJSONObject = {
name: 'Tom',
printName: () => {
// ...
}
}
//返回指定键对应的值,如果对象中不存在此键则返回 null。
let name : string = person.get('name') as string
//get 方法可以简化为使用下标运算符 `[]` 访问
name = person['name'] as string
//增加或更新指定键对应的值。
person.set('name', 'Tom1')
//set 方法可以简化为使用下标运算符 `[]` 赋值
person['name'] = 'Tom2'
// #END
// #TEST UTSJSONObject.sample_create1
// 写法1 推荐
let person1 : UTSJSONObject = JSON.parseObject('{"name":"Tom"}')!
// 写法2 推荐
const person2 : UTSJSONObject = JSON.parse<UTSJSONObject>('{"name":"Tom"}')!
// 写法3 如果 as 转换的实际类型不匹配 会导致 crash,建议先通过 `instanceof` 判断类型再进行as转换。
const parseRet3 = JSON.parse('{"name":"Tom"}')
if (parseRet3 instanceof UTSJSONObject) {
const person = parseRet3 as UTSJSONObject
}
// #END
// #TEST UTSJSONObject.toMap
person1 = JSON.parseObject('{"name":"Tom"}')!
person1.toMap().forEach((value, key) => {
console.log(key)
console.log(value)
})
// #END
// #ifdef APP-ANDROID
// #TEST UTSJSONObject.convert
type User = {
name : string,
age : number
}
let jsonObj = {
name: "张三",
age: 12
}
// UTSJSONObject => 自定义type
let userA = JSON.parse<User>(jsonObj.toJSONString())
console.log(userA!.name)
// 自定义type => UTSJSONObject
let utsJsonA = JSON.parseObject(JSON.stringify(userA))
console.log(utsJsonA)
// #END
// #endif
})
}) })
} }
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册