序列化

更新时间:

接口声明

{ "name": "blueos.baseservice.messagepack" }
复制代码

导入模块

import messagepack from '@blueos.baseservice.messagepack'const messagepack = require('@blueos.baseservice.messagepack')
复制代码

接口定义

messagepack.encode(OBJECT)

序列化数据

参数

参数名 类型 必填 说明
array Array 需要序列化的数据数组
success Function 成功返回的回调函数
fail Function 失败的回调函数
complete Function 执行结束后的回调

success 返回值

参数名 类型 说明
buffer Int8Array 序列成功返回的字节数组

示例

/*
规则:
encode(OBJECT) array参数为数组;
1、需要序列化的数据中如果包含数组类型,必须使用相同的数据类型(类型包含:number,string,bool,Arraybuffer);
2、需要序列化的数据中如果包含结构体类型,必须使用Arraybuffer类型,先将结构体类型encode,再做序列化
*/
import messagepack from '@blueos.baseservice.messagepack'

/* 1、简单类型 */
let array = [10, -10, false, 'Hello, world!']

let bufTemp = new Int8Array()

msgpack.encode({
  array: arrayValue1,
  success: data => {
    console.log(`handling success: ${JSON.stringify(data.buffer)}`)
    bufTemp = new Int8Array(data.buffer)
  },
  fail: (data, code) => {
    console.log(`handling fail, code = ${code}, errorMsg=${data}`)
  }
});

/* 2、包含数组类型(注意:如果是数组类型必须保证数组中的成员类型相同,否则encode返回undefined) */
let arrayValue1 =
[
  10,
  -10,
  false,
  [10, 20, 30],
  'Hello, world!',
];
msgpack.encode({
  array: arrayValue1,
  success: data => {
    console.log(`handling success: ${JSON.stringify(data.buffer)}`)
    bufTemp = new Int8Array(data.buffer)
  },
  fail: (data, code) => {
    console.log(`handling fail, code = ${code}, errorMsg=${data}`)
  }
});

/* 3、包含结构体类型(需要将结构体先encode成arraybuffer,再做一次encode) */
let arrayValue1 =
[
  1,
  -1,
  true,
  [10, 20],
  "xixi"
];
let buf1 = new Int8Array()

//上面为需要序列化的结构体类型
msgpack.encode({
  array: arrayValue1,
  success: data => {
    console.log(`handling success: ${JSON.stringify(data.buffer)}`)
    buf1 = new Int8Array(data.buffer)
    this.encodeFunc() // 结构体序列化成功后执行,继续完成序列化
  },
  fail: (data, code) => {
    console.log(`handling fail, code = ${code}, errorMsg=${data}`)
  }
});

//接收到结构体序列化成功的结果后调用encode再次完成序列化
encodeFunc() {
  let arrayValue2 =
  [
    10,
    false,
    [100, 200],
    'Hello, world!',
    buf1.buffer,
  ];
  msgpack.encode({
    array: arrayValue2,
    success: data => {
      console.log(`handling success: ${JSON.stringify(data.buffer)}`)
      bufTemp = new Int8Array(data.buffer)
    },
    fail: (data, code) => {
      console.log(`handling fail, code = ${code}, errorMsg=${data}`)
    }
  });
}
复制代码

messagepack.decode(OBJECT)

反序列化数据

参数

参数名 类型 必填 说明
buffer Int8Array 需要反序列的字节数组 buffer
success Function 成功返回的回调函数
fail Function 失败的回调函数
complete Function 执行结束后的回调
success 返回值
参数名 类型 必填 说明
array Array 反序列成功返回的数据数组

示例

import messagepack from '@blueos.baseservice.messagepack'

let arrayValue1 = [10, -10, false, 'Hello, world!']
let bufTemp = new Int8Array()

msgpack.encode({
  array: arrayValue1,
  success: (data) => {
    console.log(`handling success: ${JSON.stringify(data.buffer)}`)
    bufTemp = new Int8Array(data.buffer)
  },
  fail: (data, code) => {
    console.log(`handling fail, code = ${code}, errorMsg=${data}`)
  },
})

msgpack.decode({
  buffer: bufTemp.buffer,
  success: (data) => {
    console.log(`handling success: ${data.array}`)
    arrayTemp = data.array
  },
  fail: (data, code) => {
    console.log(`handling fail, code = ${code}, errorMsg=${data}`)
  },
})
复制代码
上一篇
解包
以上内容对您是否有帮助?
  • 毫无帮助
  • 帮助不大
  • 一般
  • 很好
  • 非常好
意见反馈