Deserializing a JSON string to a class instance in Haxe
Asked Answered
P

3

5

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

Paradox answered 17/6, 2012 at 19:55 Comment(0)
W
4

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));
    }
  }
}
Womack answered 18/6, 2012 at 14:23 Comment(2)
(Three years later) is there a haxelib that handles this?Charqui
Yes, there's at least two, see my answer: https://mcmap.net/q/1991757/-deserializing-a-json-string-to-a-class-instance-in-haxeFrowst
C
3

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);
            }
        }
    }
}
Commodus answered 9/2, 2014 at 2:30 Comment(1)
Note that the code above works fine with Flash targets, but for native targets you'll need to do something a bit trickier to handle Arrays. The code in this gist should be able to handle that case.Commodus
F
1

A modern, macro-based library for this purpose is json2object. It can be used like this:

var parser = new json2object.JsonParser<Action>();
var action:Action = parser.fromJson('{"id": 0, "name": "run"}', "action.json");

Another option, also macro-powered, is tink_json. In this case it's a bit more verbose because it requires you to specifiy how exactly a class should be parsed using @:jsonParse metadata:

@:jsonParse(function(json) return new Action(json.id, json.name))
class Action {
// ...

Parsing is a one-liner:

var action:Action = tink.Json.parse('{"id": 0, "name": "run"}');
Frowst answered 3/2, 2019 at 15:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.