Permalink
Browse files

add user defined serializer, default to jsonSerializer

  • Loading branch information...
1 parent f25762a commit aab8009b5abcb6a5a188dcb8983795f186e2f980 @cloudshift committed Feb 18, 2012
Showing with 43 additions and 12 deletions.
  1. +10 −0 cloudshift/Core.hx
  2. +15 −1 cloudshift/Data.hx
  3. +16 −9 cloudshift/data/Sqlite3Bucket.hx
  4. +2 −2 cloudshift/data/Sqlite3Store.hx
View
@@ -174,5 +174,15 @@ class Core {
LogImpl.debug(msg,category,inf);
}
+ public static function
+ parse(str:String):Dynamic {
+ return untyped __js__("JSON.parse(str)");
+ }
+
+ public static function
+ stringify(obj:Dynamic):String {
+ return untyped __js__("JSON.stringify(obj)");
+ }
+
}
View
@@ -9,8 +9,13 @@ import cloudshift.Remote;
import cloudshift.data.RemoteBucketProxy;
#end
+typedef Serializer = {
+ var serialize:Dynamic->String;
+ var deSerialize:String->Dynamic;
+}
+
interface Store {
- function bucket<T>(bucketName:String):Outcome<String,Bucket<T>>;
+ function bucket<T>(bucketName:String,?serialize:Serializer):Outcome<String,Bucket<T>>;
function hash<T>(bucketName:String):Outcome<String,BHash<T>>;
function name():String;
function lookupBucket<T>(bucketName:String):Option<Bucket<T>>;
@@ -78,9 +83,18 @@ class Data {
http.handler(new EReg(url+bucket.name(),""),rem.httpHandler);
}
#end
+
+ public static function jsonSerializer():Serializer {
+ return { serialize:Core.stringify,deSerialize:Core.parse};
+ }
+
+ public static function haxeSerializer():Serializer {
+ return {serialize:haxe.Serializer.run,deSerialize:haxe.Unserializer.run};
+ }
public static function oid(pkt:Dynamic):Null<Int> {
return Reflect.field(pkt,"__oid");
}
+
}
@@ -14,12 +14,20 @@ class Sqlite3Bucket<T> implements Bucket<T> {
var _db:Database;
var _table:String;
var _indexers:Hash<Indexer<T>>;
+ var _serialize:Dynamic->String;
+ var _deserialize:String->Dynamic;
- public function new(store:Sqlite3Store,table:String) {
+ public function new(store:Sqlite3Store,table:String,?serialize:Serializer) {
_store = store;
_db = store.db();
_table = table;
_indexers = new Hash() ;
+
+ if (serialize == null)
+ serialize = Data.jsonSerializer();
+
+ _serialize = serialize.serialize;
+ _deserialize= serialize.deSerialize;
}
public function
@@ -83,7 +91,7 @@ class Sqlite3Bucket<T> implements Bucket<T> {
return;
}
- var o:T = haxe.Unserializer.run(new String(row.__obj));
+ var o:T = _deserialize(new String(row.__obj));
reindexObj(o,row.rowid,function(b) {
trace("indexed:"+row.rowid);
@@ -110,7 +118,7 @@ class Sqlite3Bucket<T> implements Bucket<T> {
}
var
- o:T = haxe.Unserializer.run(new String(row.__obj)),
+ o:T = _deserialize(new String(row.__obj)),
indexVal = indexer(o),
update = "update "+_table+" set "+name+" = '"+ indexVal+"' where rowid="+row.rowid;
@@ -138,7 +146,7 @@ class Sqlite3Bucket<T> implements Bucket<T> {
var
p = Core.outcome(),
indexVals = indexUpdate(o),
- obj = haxe.Serializer.run(o),
+ obj = _serialize(o),
sql = "update "+_table+" set __obj = '" + obj + "'"+indexVals+" where rowID="+rowId;
_db.run(sql,function(err) {
@@ -166,8 +174,7 @@ class Sqlite3Bucket<T> implements Bucket<T> {
p = Core.outcome(),
indexVals = indexInsert(o);
- trace("insert sql = insert into "+_table+indexVals);
- _db.run('insert into '+_table+indexVals,haxe.Serializer.run(o),function(err) {
+ _db.run('insert into '+_table+indexVals,_serialize(o),function(err) {
if (err != null) {
p.resolve(Left(err));
return;
@@ -219,7 +226,7 @@ class Sqlite3Bucket<T> implements Bucket<T> {
return;
}
- var o:T = haxe.Unserializer.run(new String(row.__obj));
+ var o:T = _deserialize(new String(row.__obj));
Reflect.setField(o,"__oid",id);
p.resolve(Right(o));
});
@@ -272,7 +279,7 @@ class Sqlite3Bucket<T> implements Bucket<T> {
return;
}
- var o = haxe.Unserializer.run(new String(row.__obj));
+ var o = _deserialize(new String(row.__obj));
Reflect.setField(o,"__oid",row.rowid);
results.push(o);
},function() {
@@ -330,7 +337,7 @@ class Sqlite3Bucket<T> implements Bucket<T> {
return;
}
- results.push(haxe.Unserializer.run(row.__obj));
+ results.push(_deserialize(row.__obj));
},function() {
p.resolve((results.length > 0) ? Right(Some(results)) : Right(None));
});
@@ -29,15 +29,15 @@ class Sqlite3Store implements Store {
});
}
- public function bucket<T>(bucketName:String):Outcome<String,Bucket<T>> {
+ public function bucket<T>(bucketName:String,?serialize:Serializer):Outcome<String,Bucket<T>> {
var p = Core.outcome();
_db.run("create table if not exists "+bucketName+" (__obj text);",function(err) {
if (err != null) {
Left("can't create bucket:"+bucketName+"->"+err);
return;
}
- var b:Bucket<T> = new cloudshift.data.Sqlite3Bucket(this,bucketName);
+ var b:Bucket<T> = new cloudshift.data.Sqlite3Bucket(this,bucketName,serialize);
_buckets.set(bucketName,b);
p.resolve(Right(b));
});

0 comments on commit aab8009

Please sign in to comment.