Browse Source

[php] correctly Json.stringify() empty objects (fixes #5015)

Alexander Kuzmenko 8 years ago
parent
commit
2ace6d23f9

+ 1 - 0
extra/CHANGES.txt

@@ -23,6 +23,7 @@
 	php : fixed invalid detection of `NativeArray` by `Std.is()` (#5565)
 	php : fixed invalid detection of `NativeArray` by `Std.is()` (#5565)
 	php : fixed `stdin()`, `stdout()`, `stderr()` of `Sys` to use predefined constants for corresponding channels (#5733)
 	php : fixed `stdin()`, `stdout()`, `stderr()` of `Sys` to use predefined constants for corresponding channels (#5733)
 	php : fixed Std.parseInt() on hexstrings for PHP7+ (#5521)
 	php : fixed Std.parseInt() on hexstrings for PHP7+ (#5521)
+	php : fixed json encoding of empty objects (#5015)
 
 
 2016-11-29: 3.4.0-RC1
 2016-11-29: 3.4.0-RC1
 
 

+ 3 - 1
std/php/_std/haxe/Json.hx

@@ -73,7 +73,9 @@ class Json {
 		var arr:php.NativeArray;
 		var arr:php.NativeArray;
 		if (untyped __call__("is_object", val)) {
 		if (untyped __call__("is_object", val)) {
 			switch (untyped __call__("get_class", val)) {
 			switch (untyped __call__("get_class", val)) {
-				case "_hx_anonymous", "stdClass" : arr = php.Lib.associativeArrayOfObject(val);
+				case "_hx_anonymous", "stdClass" :
+					arr = php.Lib.associativeArrayOfObject(val);
+					if(untyped __php__('!{0}', arr)) return {};
 				case "_hx_array" : arr = php.Lib.toPhpArray(val);
 				case "_hx_array" : arr = php.Lib.toPhpArray(val);
 				case "Date" : return Std.string(val); //.split(" ").join("T"); //better with "T"?
 				case "Date" : return Std.string(val); //.split(" ").join("T"); //better with "T"?
 				case "HList" : arr = php.Lib.toPhpArray(Lambda.array(val)); //convert List to array?
 				case "HList" : arr = php.Lib.toPhpArray(Lambda.array(val)); //convert List to array?

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

@@ -94,9 +94,9 @@ class Json {
 		}
 		}
 
 
 		if (Global.is_object(value)) {
 		if (Global.is_object(value)) {
-			var result = new NativeAssocArray();
+			var result = {};
 			Syntax.foreach(value, function(fieldName:String, fieldValue:Dynamic) {
 			Syntax.foreach(value, function(fieldName:String, fieldValue:Dynamic) {
-				result[fieldName] = convertBeforeEncode(fieldValue);
+				Syntax.setField(result, fieldName, convertBeforeEncode(fieldValue));
 			});
 			});
 
 
 			return result;
 			return result;

+ 9 - 0
tests/unit/src/unit/issues/Issue5015.hx

@@ -0,0 +1,9 @@
+package unit.issues;
+
+using haxe.Json;
+
+class Issue5015 extends Test {
+	function test() {
+		eq('{}'.parse().stringify(), '{}');
+	}
+}