Sfoglia il codice sorgente

[cs] fix Json.stringify for @:struct-annotated classes
#8972

Aleksandr Kuzmenko 5 anni fa
parent
commit
bb997f7fcc
2 ha cambiato i file con 25 aggiunte e 1 eliminazioni
  1. 1 1
      std/cs/_std/Type.hx
  2. 24 0
      tests/unit/src/unit/issues/Issue8979.hx

+ 1 - 1
std/cs/_std/Type.hx

@@ -269,7 +269,7 @@ enum ValueType {
 		if (Std.is(v, HxEnum))
 			return ValueType.TEnum(cast t.BaseType); // enum constructors are subclasses of an enum type
 		if (t.IsValueType) {
-			var vc:cs.system.IConvertible = cast v;
+			var vc = Std.downcast(v, cs.system.IConvertible);
 			if (vc != null) {
 				switch (vc.GetTypeCode()) {
 					case cs.system.TypeCode.Boolean:

+ 24 - 0
tests/unit/src/unit/issues/Issue8979.hx

@@ -0,0 +1,24 @@
+package unit.issues;
+
+import haxe.Json;
+
+class Issue8979 extends unit.Test {
+#if cs
+	function test() {
+		var d = new DummyStruct();
+		d.hello = 'world';
+		var json = Json.stringify(d);
+		eq('{"hello":"world"}', json);
+		eq('world', Json.parse(json).hello);
+	}
+#end
+}
+
+#if cs
+@:struct
+private class DummyStruct {
+	public var hello:String;
+	public function new() {}
+}
+#end
+