瀏覽代碼

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
-	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
 		actual object to be encoded. The `replacer` function two parameters,
 		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 {
 
-	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);
 		return printer.buf.toString();
 	}
 
 	var buf : #if flash9 flash.utils.ByteArray #else StringBuf #end;
 	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.indent = space;
+		this.nl = '\n';
+		this.pretty = space != null;
+		this.nind = 0;
 
 		#if flash9
 		buf = new flash.utils.ByteArray();
@@ -22,6 +30,14 @@ class JsonPrinter {
 		buf = new StringBuf();
 		#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) {
 		if (replacer != null) v = replacer(k, v);
@@ -42,15 +58,23 @@ class JsonPrinter {
 			else if( c == Array ) {
 				var v : Array<Dynamic> = v;
 				addChar('['.code);
+				nind++;
 				var len = v.length;
 				if( len > 0 ) {
+					newl();
+					ipad();
 					write(0, v[0]);
 					var i = 1;
 					while( i < len ) {
 						addChar(','.code);
+						newl();
+						ipad();
 						write(i, v[i++]);
 					}
 				}
+				nind--;
+				newl();
+				ipad();
 				addChar(']'.code);
 			} else if( c == haxe.ds.StringMap ) {
 				var v : haxe.ds.StringMap<Dynamic> = v;
@@ -107,14 +131,21 @@ class JsonPrinter {
 	function fieldsString( v : Dynamic, fields : Array<String> ) {
 		var first = true;
 		addChar('{'.code);
+		nind++;
 		for( f in fields ) {
 			var value = Reflect.field(v,f);
 			if( Reflect.isFunction(value) ) continue;
 			if( first ) first = false else addChar(','.code);
+			newl();
+			ipad();
 			quote(f);
 			addChar(':'.code);
+			if (pretty) addChar(' '.code);
 			write(f, value);
 		}
+		nind--;
+		newl();
+		ipad();
 		addChar('}'.code);
 	}
 

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

@@ -33,8 +33,8 @@ class Json {
 	}
 
 	#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)

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

@@ -32,11 +32,11 @@ class Json {
 		#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
-		return phpJsonEncode(value, replacer);
+		return phpJsonEncode(value, replacer, space);
 		#else
-		return haxe.format.JsonPrinter.print(value, replacer);
+		return haxe.format.JsonPrinter.print(value, replacer, space);
 		#end
 	}
 
@@ -59,9 +59,9 @@ class Json {
 			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));
 		if (untyped __physeq__(json, false))
 			return throw "invalid json";