added native serialization for kv store

available as a flag: kv_use_native
This commit is contained in:
Niklas Kapelle 2024-04-19 13:00:13 +02:00
parent d89956a626
commit e04021425a
Signed by: niklas
GPG Key ID: 4EB651B36D841D16

View File

@ -1,12 +1,17 @@
package lib; package lib;
import haxe.Serializer;
import haxe.Unserializer;
import kernel.fs.FS; import kernel.fs.FS;
import haxe.ds.StringMap; import haxe.ds.StringMap;
#if kv_use_native
import cc.TextUtils;
#else
import haxe.Unserializer;
import haxe.Serializer;
#end
/** /**
Key value store with persistence. Key value store with persistence.
Use flag kv_use_native to use the cc serialization.
**/ **/
class KVStore { class KVStore {
private var kvStore:StringMap<Dynamic> = new StringMap(); private var kvStore:StringMap<Dynamic> = new StringMap();
@ -43,14 +48,21 @@ class KVStore {
public function save() { public function save() {
var handle = FS.openWrite(getNamespaceFile(this.namespace)); var handle = FS.openWrite(getNamespaceFile(this.namespace));
#if kv_use_native
handle.write(TextUtils.serialize(this.kvStore));
#else
handle.write(Serializer.run(this.kvStore)); handle.write(Serializer.run(this.kvStore));
#end
handle.close(); handle.close();
} }
private function parseFile(content:String) { private function parseFile(content:String) {
#if kv_use_native
this.kvStore = TextUtils.unserialize(content);
#else
var unserializer = new Unserializer(content); var unserializer = new Unserializer(content);
this.kvStore = unserializer.unserialize(); this.kvStore = unserializer.unserialize();
#end
} }
public inline function set(key:String, value:Dynamic) { public inline function set(key:String, value:Dynamic) {