I am trying to deserialize a JSON string into a class instance in Haxe.
class Action
{
public var id:Int;
public var name:String;
public function new(id:Int, name:String)
{
this.id = id;
this.name = name;
}
}
I would like to do something like this:
var action:Action = haxe.Json.parse(actionJson);
trace(action.name);
However, this produces an error:
TypeError: Error #1034: Type Coercion failed: cannot convert Object@3431809 to Action
Json doesn't have a mechanism to map language specific data types and only supports a subset of the data types included in JS. To keep the information about the Haxe types you can certainly build your own mechanism.
// This works only for basic class instances but you can extend it to work with
// any type.
// It doesn't work with nested class instances; you can detect the required
// types with macros (will fail for interfaces or extended classes) or keep
// track of the types in the serialized object.
// Also you will have problems with objects that have circular references.
class JsonType {
public static function encode(o : Dynamic) {
// to solve some of the issues above you should iterate on all the fields,
// check for a non-compatible Json type and build a structure like the
// following before serializing
return haxe.Json.stringify({
type : Type.getClassName(Type.getClass(o)),
data : o
});
}
public static function decode<T>(s : String) : T {
var o = haxe.Json.parse(s),
inst = Type.createEmptyInstance(Type.resolveClass(o.type));
populate(inst, o.data);
return inst;
}
static function populate(inst, data) {
for(field in Reflect.fields(data)) {
Reflect.setField(inst, field, Reflect.field(data, field));
}
}
}
I extended Franco's answer to allow for recursively containing objects within your json objects, as long as the _explicitType
property is set on that object.
For instance, the following json:
{
intPropertyExample : 5,
stringPropertyExample : 'my string',
pointPropertyExample : {
_explicitType : 'flash.geom.Point',
x : 5,
y : 6
}
}
will correctly be serialized into an object whose class looks like this:
import flash.geom.Point;
class MyTestClass
{
public var intPropertyExample:Int;
public var stringPropertyExample:String;
public var pointPropertyExample:Point;
}
when calling:
var serializedObject:MyTestClass = EXTJsonSerialization.decode([string of json above], MyTestClass)
Here's the code (note that it uses TJSON as a parser, as CrazySam recommended):
import tjson.TJSON;
class EXTJsonSerialization
{
public static function encode(o : Dynamic)
{
return TJSON.encode(o);
}
public static function decode<T>(s : String, typeClass : Class<Dynamic>) : T
{
var o = TJSON.parse(s);
var inst = Type.createEmptyInstance(typeClass);
EXTJsonSerialization.populate(inst, o);
return inst;
}
private static function populate(inst, data)
{
for (field in Reflect.fields(data))
{
if (field == "_explicitType")
continue;
var value = Reflect.field(data, field);
var valueType = Type.getClass(value);
var valueTypeString:String = Type.getClassName(valueType);
var isValueObject:Bool = Reflect.isObject(value) && valueTypeString != "String";
var valueExplicitType:String = null;
if (isValueObject)
{
valueExplicitType = Reflect.field(value, "_explicitType");
if (valueExplicitType == null && valueTypeString == "Array")
valueExplicitType = "Array";
}
if (valueExplicitType != null)
{
var fieldInst = Type.createEmptyInstance(Type.resolveClass(valueExplicitType));
populate(fieldInst, value);
Reflect.setField(inst, field, fieldInst);
}
else
{
Reflect.setField(inst, field, value);
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With