MMKV icon indicating copy to clipboard operation
MMKV copied to clipboard

MMKV for HarmonyOS NEXT 是否可以提供自定义对象的存取接口?

Open sagdragon opened this issue 1 year ago • 4 comments

目前最新版本1.3.5支持boolean, number, bigint, string,boolean[], number[], string[], ArrayBuffer类型,是否可以提供自定义对象的存取接口?如果可以提供的话,麻烦回复一下时间、版本计划,多谢。

sagdragon avatar Apr 28 '24 08:04 sagdragon

As far as I learned, there's nothing like Parcel in Android.

So no, not in our plans.

lingol avatar Apr 28 '24 13:04 lingol

目前最新版本1.3.5支持boolean, number, bigint, string,boolean[], number[], string[], ArrayBuffer类型,是否可以提供自定义对象的存取接口?如果可以提供的话,麻烦回复一下时间、版本计划,多谢。

1.3.5版本对外导出的 是ets文件, 在鸿蒙工程中无法在ts文件 import ets,导致无法使用。 官方是否有计划 将导出ets文件改为ts, 避免有使用限制。

GkLwPp avatar Apr 29 '24 07:04 GkLwPp

1.3.5版本对外导出的 是ets文件, 在鸿蒙工程中无法在ts文件 import ets,导致无法使用。 官方是否有计划 将导出ets文件改为ts, 避免有使用限制。

How can one do that? I'd love to know. MMKV's wrapper is developed in ArtTS and we don't want to change it to TS. How can we export it as TS without changing the MMKV wrapper from .ets to .ts?

lingol avatar Apr 29 '24 07:04 lingol

For the time being, you can encode/decode the object into JSON first. It's not ideal but it can get you going.

let user = {
  username: 'lingol',
  age: 21
}

// Serialize the object into a JSON string
mmkv.encodeString('user', JSON.stringify(user))

// Deserialize the JSON string into an object
let jsonUser = mmkv.decodeString('user') // { 'username': 'lingol', 'age': 21 }
let userObject = JSON.parse(jsonUser)

lingol avatar May 06 '24 03:05 lingol