浏览代码

add pretty print to JsonPrinter

Sam MacPherson 11 年之前
父节点
当前提交
2181cf2626
共有 5 个文件被更改,包括 50 次插入16 次删除
  1. 2 2
      std/flash/_std/haxe/Json.hx
  2. 5 2
      std/haxe/Json.hx
  3. 35 4
      std/haxe/format/JsonPrinter.hx
  4. 2 2
      std/js/_std/haxe/Json.hx
  5. 6 6
      std/php/_std/haxe/Json.hx

+ 2 - 2
std/flash/_std/haxe/Json.hx

@@ -33,7 +33,7 @@ class Json {
 	}
 	}
 
 
 	#if (haxeJSON || !flash11) inline #end
 	#if (haxeJSON || !flash11) inline #end
-	public static function stringify( value : Dynamic, ?replacer:Dynamic -> Dynamic -> Dynamic ) : String {
-		return haxe.format.JsonPrinter.print(value, replacer);
+	public static function stringify( value : Dynamic, ?replacer:Dynamic -> Dynamic -> Dynamic, ?space:String ) : String {
+		return haxe.format.JsonPrinter.print(value, replacer, space);
 	}
 	}
 }
 }

+ 5 - 2
std/haxe/Json.hx

@@ -46,9 +46,12 @@ class Json {
 		If `replacer` is given and is not null, it is used to retrieve
 		If `replacer` is given and is not null, it is used to retrieve
 		actual object to be encoded. The `replacer` function two parameters,
 		actual object to be encoded. The `replacer` function two parameters,
 		the key and the value being encoded. Initial key value is an empty string.
 		the key and the value being encoded. Initial key value is an empty string.
+		
+		If `space` is given and is not null, the result will be pretty-printed.
+		Successive levels will be indented by this string.
 	**/
 	**/
-	public static inline function stringify( value : Dynamic, ?replacer:Dynamic -> Dynamic -> Dynamic ) : String {
-		return haxe.format.JsonPrinter.print(value, replacer);
+	public static inline function stringify( value : Dynamic, ?replacer:Dynamic -> Dynamic -> Dynamic, ?space : String ) : String {
+		return haxe.format.JsonPrinter.print(value, replacer, space);
 	}
 	}
 
 
 }
 }

+ 35 - 4
std/haxe/format/JsonPrinter.hx

@@ -2,17 +2,25 @@ package haxe.format;
 
 
 class JsonPrinter {
 class JsonPrinter {
 
 
-	static public function print(o:Dynamic, ?replacer:Dynamic -> Dynamic -> Dynamic) : String {
-		var printer = new JsonPrinter(replacer);
+	static public function print(o:Dynamic, ?replacer:Dynamic -> Dynamic -> Dynamic, ?space:String) : String {
+		var printer = new JsonPrinter(replacer, space);
 		printer.write("", o);
 		printer.write("", o);
 		return printer.buf.toString();
 		return printer.buf.toString();
 	}
 	}
 
 
 	var buf : #if flash9 flash.utils.ByteArray #else StringBuf #end;
 	var buf : #if flash9 flash.utils.ByteArray #else StringBuf #end;
 	var replacer : Dynamic -> Dynamic -> Dynamic;
 	var replacer : Dynamic -> Dynamic -> Dynamic;
-
-	function new(replacer:Dynamic -> Dynamic -> Dynamic) {
+	var indent:String;
+	var nl:String;
+	var pretty:Bool;
+	var nind:Int;
+	
+	function new(replacer:Dynamic -> Dynamic -> Dynamic, space:String) {
 		this.replacer = replacer;
 		this.replacer = replacer;
+		this.indent = space;
+		this.nl = '\n';
+		this.pretty = space != null;
+		this.nind = 0;
 
 
 		#if flash9
 		#if flash9
 		buf = new flash.utils.ByteArray();
 		buf = new flash.utils.ByteArray();
@@ -22,6 +30,14 @@ class JsonPrinter {
 		buf = new StringBuf();
 		buf = new StringBuf();
 		#end
 		#end
 	}
 	}
+	
+	inline function ipad ():Void {
+		if (pretty) add(StringTools.lpad('', indent, nind * indent.length));
+	}
+	
+	inline function newl ():Void {
+		if (pretty) add(nl);
+	}
 
 
 	function write(k:Dynamic, v:Dynamic) {
 	function write(k:Dynamic, v:Dynamic) {
 		if (replacer != null) v = replacer(k, v);
 		if (replacer != null) v = replacer(k, v);
@@ -42,15 +58,23 @@ class JsonPrinter {
 			else if( c == Array ) {
 			else if( c == Array ) {
 				var v : Array<Dynamic> = v;
 				var v : Array<Dynamic> = v;
 				addChar('['.code);
 				addChar('['.code);
+				nind++;
 				var len = v.length;
 				var len = v.length;
 				if( len > 0 ) {
 				if( len > 0 ) {
+					newl();
+					ipad();
 					write(0, v[0]);
 					write(0, v[0]);
 					var i = 1;
 					var i = 1;
 					while( i < len ) {
 					while( i < len ) {
 						addChar(','.code);
 						addChar(','.code);
+						newl();
+						ipad();
 						write(i, v[i++]);
 						write(i, v[i++]);
 					}
 					}
 				}
 				}
+				nind--;
+				newl();
+				ipad();
 				addChar(']'.code);
 				addChar(']'.code);
 			} else if( c == haxe.ds.StringMap ) {
 			} else if( c == haxe.ds.StringMap ) {
 				var v : haxe.ds.StringMap<Dynamic> = v;
 				var v : haxe.ds.StringMap<Dynamic> = v;
@@ -107,14 +131,21 @@ class JsonPrinter {
 	function fieldsString( v : Dynamic, fields : Array<String> ) {
 	function fieldsString( v : Dynamic, fields : Array<String> ) {
 		var first = true;
 		var first = true;
 		addChar('{'.code);
 		addChar('{'.code);
+		nind++;
 		for( f in fields ) {
 		for( f in fields ) {
 			var value = Reflect.field(v,f);
 			var value = Reflect.field(v,f);
 			if( Reflect.isFunction(value) ) continue;
 			if( Reflect.isFunction(value) ) continue;
 			if( first ) first = false else addChar(','.code);
 			if( first ) first = false else addChar(','.code);
+			newl();
+			ipad();
 			quote(f);
 			quote(f);
 			addChar(':'.code);
 			addChar(':'.code);
+			if (pretty) addChar(' '.code);
 			write(f, value);
 			write(f, value);
 		}
 		}
+		nind--;
+		newl();
+		ipad();
 		addChar('}'.code);
 		addChar('}'.code);
 	}
 	}
 
 

+ 2 - 2
std/js/_std/haxe/Json.hx

@@ -33,8 +33,8 @@ class Json {
 	}
 	}
 
 
 	#if haxeJSON inline #end
 	#if haxeJSON inline #end
-	public static function stringify( value : Dynamic, ?replacer:Dynamic -> Dynamic -> Dynamic ) : String {
-		return haxe.format.JsonPrinter.print(value, replacer);
+	public static function stringify( value : Dynamic, ?replacer:Dynamic -> Dynamic -> Dynamic, ?space:String ) : String {
+		return haxe.format.JsonPrinter.print(value, replacer, space);
 	}
 	}
 
 
 	#if (!haxeJSON && old_browser)
 	#if (!haxeJSON && old_browser)

+ 6 - 6
std/php/_std/haxe/Json.hx

@@ -32,11 +32,11 @@ class Json {
 		#end
 		#end
 	}
 	}
 
 
-	public static inline function stringify( value : Dynamic, ?replacer:Dynamic -> Dynamic -> Dynamic ) : String {
+	public static inline function stringify( value : Dynamic, ?replacer:Dynamic -> Dynamic -> Dynamic, ?space:String ) : String {
 		#if !haxeJSON
 		#if !haxeJSON
-		return phpJsonEncode(value, replacer);
+		return phpJsonEncode(value, replacer, space);
 		#else
 		#else
-		return haxe.format.JsonPrinter.print(value, replacer);
+		return haxe.format.JsonPrinter.print(value, replacer, space);
 		#end
 		#end
 	}
 	}
 
 
@@ -59,9 +59,9 @@ class Json {
 			return val;
 			return val;
 	}
 	}
 
 
-	static function phpJsonEncode(val:Dynamic, ?replacer:Dynamic -> Dynamic -> Dynamic):String {
-		if(null != replacer)
-			return haxe.format.JsonPrinter.print(val, replacer);
+	static function phpJsonEncode(val:Dynamic, ?replacer:Dynamic -> Dynamic -> Dynamic, ?space:String):String {
+		if(null != replacer || null != space)
+			return haxe.format.JsonPrinter.print(val, replacer, space);
 		var json = untyped __call__("json_encode", convertBeforeEncode(val));
 		var json = untyped __call__("json_encode", convertBeforeEncode(val));
 		if (untyped __physeq__(json, false))
 		if (untyped __physeq__(json, false))
 			return throw "invalid json";
 			return throw "invalid json";