فهرست منبع

added Date Hash IntHash support.

Nicolas Cannasse 19 سال پیش
والد
کامیت
67d8556b8d
2فایلهای تغییر یافته به همراه72 افزوده شده و 18 حذف شده
  1. 43 14
      std/haxe/Serializer.hx
  2. 29 4
      std/haxe/Unserializer.hx

+ 43 - 14
std/haxe/Serializer.hx

@@ -55,26 +55,32 @@ class Serializer {
 	}
 
 	/* prefixes :
-		n : null
-		t : true
-		f : false
-		i : Int
-		z : zero
+		a : array
+		b : hash
+		c : class
 		d : Float
 		e : reserved (float exp)
+		f : false
+		g : object end
+		h : array/list/hash end
+		i : Int
+		j : utf8 escaped string
 		k : NaN
+		l : list
 		m : -Inf
+		n : null
+		o : object
 		p : +Inf
+		q : inthash
+		r : reference
 		s : utf8 string
-		j : utf8 escaped string
-		a : array
+		t : true
 		u : array nulls
-		h : array end
-		o : object
-		g : object end
-		r : reference
-		c : class
+		v : date
 		w : enum
+		x : exception
+		y : *unused
+		z : zero
 	*/
 
 	function serializeString( s : String ) {
@@ -154,7 +160,7 @@ class Serializer {
 				return;
 			}
 			buf.add("i");
-			buf.add(Std.string(v));
+			buf.add(v);
 		case TFloat:
 			if( Math.isNaN(v) )
 				buf.add("k");
@@ -162,7 +168,7 @@ class Serializer {
 				buf.add(if( v < 0 ) "m" else "p");
 			else {
 				buf.add("d");
-				buf.add(Std.string(v));
+				buf.add(v);
 			}
 		case TBool:
 			buf.add(if( v ) "t" else "f");
@@ -203,6 +209,29 @@ class Serializer {
 					}
 				}
 				buf.add("h");
+			case cast List:
+				buf.add("l");
+				for( i in v.iterator() )
+					serialize(i);
+				buf.add("h");
+			case cast Date:
+				buf.add("v");
+				buf.add(v);
+			case cast Hash:
+				buf.add("b");
+				for( k in v.keys() ) {
+					serializeString(k);
+					serialize(v.get(k));
+				}
+				buf.add("h");
+			case cast IntHash:
+				buf.add("q");
+				for( k in v.keys() ) {
+					buf.add(":");
+					buf.add(k);
+					serialize(v.get(k));
+				}
+				buf.add("h");
 			default:
 				cache.pop();
 				buf.add("c");

+ 29 - 4
std/haxe/Unserializer.hx

@@ -176,8 +176,6 @@ class Unserializer {
  			var a = new Array<Dynamic>();
  			cache.push(a);
  			while( true ) {
- 				if( pos >= length )
- 					throw "Invalid array";
  				var c = buf.charCodeAt(pos);
  				if( c == 104 ) { /*h*/
 					pos++;
@@ -186,8 +184,6 @@ class Unserializer {
  				if( c == 117 ) { /*u*/
 					pos++;
  					var n = readDigits();
- 					if( n <= 0 )
- 						throw "Invalid array null counter";
  					a[a.length+n-1] = null;
  				} else
  					a.push(unserialize());
@@ -245,6 +241,35 @@ class Unserializer {
 			var e = Reflect.callMethod(edecl,constr,args);
 			cache.push(e);
 			return e;
+		case 108: // l
+			var l = new List();
+			while( buf.charCodeAt(pos) != 104 /*h*/ )
+				l.add(unserialize());
+			pos++;
+			return l;
+		case 98: // b
+			var h = new Hash();
+			while( buf.charCodeAt(pos) != 104 /*h*/ ) {
+				var s = unserialize();
+				h.set(s,unserialize());
+			}
+			pos++;
+			return h;
+		case 113: // q
+			var h = new IntHash();
+			var c = buf.charCodeAt(pos++);
+			while( c == 58 ) { /*:*/
+				var i = readDigits();
+				h.set(i,unserialize());
+				c = buf.charCodeAt(pos++);
+			}
+			if( c != 104 )
+				throw "Invalid IntHash format";
+			return h;
+		case 118:
+			var d = Date.fromString(buf.substr(pos,19));
+			pos += 19;
+			return d;
  		default:
  		}
  		pos--;