Migrating to Openfl from starting

Hi guys, I really appreciate what you are doing, you have done an amazing job. I tried to migrate to openfl, so I translated the code to haxe from there I tried to integrate with openfl. My game is backend intensive. The limitation for me on how haxe handles json object what drove me off the effort. Otherwise, it would have been a perfect solution for me. Parsing json object’s was hell :slight_smile: any recommendations?

Hello, and welcome :smiley:

There’s some helpful tips here:

TL/DR use of typedef really helps:

typedef MyType
{
    @:optional var a:String;
    @:optional var b:String;
}

...

var object = Json.parse(data);
var typedObject:MyType = cast object;
trace(typedObject.a);
trace(typedObject.b);

or haxe.DynamicAccess is another choice

var object:DynamicAccess = Json.parse(data);
trace(object["a"]);
trace(object.get("b"));

EDIT: Another choice is to use a Haxe class and to parse to it so you can be sure that everything is typed all throughout your application. This is a little extra overhead but the type-safety can help make things cleaner, faster and easier throughout your application elsewhere

class MyType
{
    public var a:String;
    public var b:String;

    public function new(data:String)
    {
        var object:DynamicAccess = Json.parse(data);
        if (object.exists("a")) a = object["a"];
        if (object.exists("b")) b = object["b"];
    }
}

...

var object = new MyType(data);
trace(object.a);
trace(object.b);

Thank you for the pointers. I will try again. And I will share on here my showstoppers.
Hopefully this thread will help new comers from starling like me.

The biggest problem was enumerating the json fields. Let’s say the client is blind to the server response. A dynamic response with dynamic fields. That’s what got me most.
It has been 2 weeks since I stopped the effort. But I will provide detailed example this week once I resume. In AS3, I can simply assign the response to a json object and do my checking. If (a && a.b && a.b.c && a.b.c.d ==“hello”) then do something. In haxe How can something like that be handled?

I think it might be like this?

var object = Json.parse(data);

if (Reflect.hasField(object, "a") && Reflect.hasField(Reflect.field(object, "a"), "b") && Reflect.hasField(Reflect.field(Reflect.field(object, "a"), "b"), "c") && Reflect.hasField(Reflect.field(Reflect.field(Reflect.field(object, "a"), "b"), "c"), "d") && Reflect.field(Reflect.field(Reflect.field(Reflect.field(object, "a"), "b"), "c"), "d") == "hello")
{
    // oi!
}

That’s awful so let’s try haxe.DynamicAccess instead.

Actually I thought that get() would return another DynamicAccess but instead it returns an object type. I guess that would require casting each time:

var object:DynamicAccess = Json.parse(data);
var lookup = null;

if (object.exists("a"))
{
    lookup = object["a"];
    if (lookup.exists("b"))
    {
        lookup = lookup["b"];
        ...

This could be made a lot easier using a Haxe abstract that returns itself. I’m really interested now in perhaps openfl.utils.Object adding this functionality

That will be awesome. It really made me stop working on the transition to openfl. I was doing the reflect statements like the example that you provided. But I gave up, the code quickly becomes unreadable.

Actually I think I overcomplicated this

AS3

if (object.a && object.a.b && object.a.b.c && object.a.b.c.d == "hello")
{
    ...
}

Haxe

if (object.a != null && object.a.b != null && object.a.b.c != null && object.a.b.c.d == "hello")
{
    ...
}

…so long as object is typed as Dynamic

Got it! Maybe the as3to haxe translator the reason I did the reflect statements. I will try your solution. Thanks for your help again

Also, there’s a library with a safe navigation operator macro to avoid going crazy when writing chained null checks like that:

if (object!.a!.b!.d == "hello")
{
    ...
}
1 Like