Kaynağa Gözat

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

Aleksandr Kuzmenko 5 yıl önce
ebeveyn
işleme
28c2ad933b

+ 1 - 0
extra/CHANGES.txt

@@ -9,6 +9,7 @@
 	all : fixed EnumValue handling in constant propagation with analyzer enabled (#8959)
 	all : fixed compiler crash upon Void items in array declarations (#8972)
 	java/cs : fixed `Reflect.callMethod(o, method, args)` for `args` not containing optional arguments (#8975)
+	cs : fixed Json.stringify for @:struct-annotated classes (#8979)
 	python : fixed invalid generation of some inlined code blocks (#8971)
 
 

+ 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
+