diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/JSONMessageCodec.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/JSONMessageCodec.ets index 6224740ed6d838efdeaea84a3f81d829a5e14ac1..e766db57fc08160e21e5061e525a2c03e986621f 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/JSONMessageCodec.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/JSONMessageCodec.ets @@ -17,6 +17,12 @@ import StringUtils from '../../util/StringUtils'; import MessageCodec from './MessageCodec'; import MethodCodec from './MethodCodec'; import StringCodec from './StringCodec'; +import TreeMap from '@ohos.util.TreeMap'; +import HashMap from '@ohos.util.HashMap'; +import LightWeightMap from '@ohos.util.LightWeightMap'; +import PlainArray from '@ohos.util.PlainArray'; +import List from '@ohos.util.List'; +import LinkedList from '@ohos.util.LinkedList'; /** * A {@link MethodCodec} using UTF-8 encoded JSON method calls and result envelopes. @@ -35,7 +41,7 @@ export default class JSONMessageCodec implements MessageCodec { if (message == null) { return StringUtils.stringToArrayBuffer(""); } - return StringCodec.INSTANCE.encodeMessage(JSON.stringify(message)); + return StringCodec.INSTANCE.encodeMessage(JSON.stringify(this.toBaseData(message))); } decodeMessage(message: ArrayBuffer): ESObject { @@ -49,4 +55,21 @@ export default class JSONMessageCodec implements MessageCodec { throw new Error("Invalid JSON"); } } + + toBaseData(message: ESObject): ESObject { + if (message == null || message == undefined) { + return ""; + } else if (message instanceof List || message instanceof LinkedList) { + return message.convertToArray(); + } else if (message instanceof Map || message instanceof HashMap || message instanceof TreeMap + || message instanceof LightWeightMap || message instanceof PlainArray) { + let messageObj: ESObject = {}; + message.forEach((value: ESObject, key: ESObject) => { + messageObj[this.toBaseData(key)] = this.toBaseData(value); + }); + return messageObj; + } else { + return message; + } + } } \ No newline at end of file diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/StandardMessageCodec.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/StandardMessageCodec.ets index d9b52ff3efb983a94766e8b8fbee154f9834071a..e10e6a2237b8b603ad18b627af283193255c263c 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/StandardMessageCodec.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/StandardMessageCodec.ets @@ -16,6 +16,12 @@ import { ByteBuffer } from '../../util/ByteBuffer'; import StringUtils from '../../util/StringUtils'; import MessageCodec from './MessageCodec'; +import TreeMap from '@ohos.util.TreeMap'; +import HashMap from '@ohos.util.HashMap'; +import LightWeightMap from '@ohos.util.LightWeightMap'; +import PlainArray from '@ohos.util.PlainArray'; +import List from '@ohos.util.List'; +import LinkedList from '@ohos.util.LinkedList'; /** * MessageCodec using the Flutter standard binary encoding. @@ -103,63 +109,71 @@ export default class StandardMessageCodec implements MessageCodec { } else if (typeof value === "number") { if (Number.isInteger(value)) { //整型 if (-0x7fffffff - 1 <= value && value <= 0x7fffffff) { //int32 - stream.writeInt8(StandardMessageCodec.INT32) - stream.writeInt32(value, true) + stream.writeInt8(StandardMessageCodec.INT32); + stream.writeInt32(value, true); } else if(Number.MIN_SAFE_INTEGER <= value && value <= Number.MAX_SAFE_INTEGER) { //int64 number整型取值范围 - stream.writeInt8(StandardMessageCodec.INT64) - stream.writeBigInt64(BigInt(value), true) + stream.writeInt8(StandardMessageCodec.INT64); + stream.writeBigInt64(BigInt(value), true); } else { //被判为整型的double型 - stream.writeInt8(StandardMessageCodec.FLOAT64) + stream.writeInt8(StandardMessageCodec.FLOAT64); this.writeAlignment(stream, 8); - stream.writeFloat64(value, true) + stream.writeFloat64(value, true); } } else { //浮点型 - stream.writeInt8(StandardMessageCodec.FLOAT64) + stream.writeInt8(StandardMessageCodec.FLOAT64); this.writeAlignment(stream, 8); - stream.writeFloat64(value, true) + stream.writeFloat64(value, true); } }else if (typeof value === "bigint") { - stream.writeInt8(StandardMessageCodec.INT64) + stream.writeInt8(StandardMessageCodec.INT64); stream.writeBigInt64(value, true) } else if (typeof value === "string") { - stream.writeInt8(StandardMessageCodec.STRING) + stream.writeInt8(StandardMessageCodec.STRING); let stringBuff = StringUtils.stringToArrayBuffer(value) this.writeBytes(stream, new Uint8Array(stringBuff)) } else if (value instanceof Uint8Array) { - stream.writeInt8(StandardMessageCodec.UINT8_ARRAY) + stream.writeInt8(StandardMessageCodec.UINT8_ARRAY); this.writeBytes(stream, value) } else if (value instanceof Int32Array) { - stream.writeInt8(StandardMessageCodec.INT32_ARRAY) + stream.writeInt8(StandardMessageCodec.INT32_ARRAY); this.writeSize(stream, value.length); this.writeAlignment(stream, 4); value.forEach(item => stream.writeInt32(item, true)) } else if(value instanceof BigInt64Array) { - stream.writeInt8(StandardMessageCodec.INT64_ARRAY) + stream.writeInt8(StandardMessageCodec.INT64_ARRAY); this.writeSize(stream, value.length); this.writeAlignment(stream, 8); value.forEach(item => stream.writeBigInt64(item, true)) } else if (value instanceof Float32Array) { - stream.writeInt8(StandardMessageCodec.FLOAT32_ARRAY) + stream.writeInt8(StandardMessageCodec.FLOAT32_ARRAY); this.writeSize(stream, value.length); this.writeAlignment(stream, 4); value.forEach(item => stream.writeFloat32(item, true)) } else if (value instanceof Float64Array) { - stream.writeInt8(StandardMessageCodec.FLOAT64_ARRAY) + stream.writeInt8(StandardMessageCodec.FLOAT64_ARRAY); this.writeSize(stream, value.length); this.writeAlignment(stream, 8); value.forEach(item => stream.writeFloat64(item, true)) } else if (value instanceof Array || value instanceof Int8Array || value instanceof Int16Array - || value instanceof Uint16Array || value instanceof Uint32Array) { - stream.writeInt8(StandardMessageCodec.LIST) + || value instanceof Uint16Array || value instanceof Uint32Array || value instanceof List + || value instanceof LinkedList) { + stream.writeInt8(StandardMessageCodec.LIST); this.writeSize(stream, value.length); - value.forEach((item: ESObject): void => this.writeValue(stream, item)) + value.forEach((item: ESObject): void => this.writeValue(stream, item)); } else if (value instanceof Map) { - stream.writeInt8(StandardMessageCodec.MAP) + stream.writeInt8(StandardMessageCodec.MAP); this.writeSize(stream, value.size); value.forEach((value: ESObject, key: ESObject) => { this.writeValue(stream, key); this.writeValue(stream, value); }) + } else if(value instanceof HashMap || value instanceof TreeMap || value instanceof LightWeightMap + || value instanceof PlainArray) { + this.writeValue(stream, value.length); + value.forEach((value: ESObject, key: ESObject) => { + this.writeValue(stream, key); + this.writeValue(stream, value); + }); } else if (typeof value == 'object') { let map: Map = new Map(); Object.keys(value).forEach(key => {