COMMUNITY

Haxe returns object of anonymous type to Python


(Peter Taylor Coates) #1

I’d be most grateful for a tip from one of you seasoned Haxers.

I have a Haxe object (basically, a “bean”) that can print itself out a JSON. I want to have identical code for to go back and forth between beans and JSON in several languages. The JSON will evolve over time, so I’m writing the core library in Haxe as a nice way to keep everyone on the same page regardless of language.

There are no problems with creating a bean in hand-written test code and spitting out JSON.

I also have a Haxe method that takes JSON and makes into an object and returns it. I can access this object from the hand-written Python code via something like “print gjd.data.snrs[0].s_ty;” but I can’t use any of the methods in the object because it is of of anonymous type. (See below.)

// This method is in a bean class called GenterateJSONDriver.
// Generates a JSON version of the object. You can use the field names to look up values
// but you can’t use the getMyField() or toString(), etc.
//
public static function jsonToDriver(jsn:String):GenerateJSONDriver{
var ret:GenerateJSONDriver = haxe.Json.parse(jsn);
return ret;
}

Handwritten code using the GenerateJSONDriver object works fine if I do it as follows.
print “test2.py calling jsonToDriver()”;
gjd=gen.jsonToDriver(jsonstring);
print gjd.data.tm;
print gjd.data.snrs[0].s_ty;

The field names are available but none of the methods are, so you can’t use getters and setters, etc. I have include GenerateJSONDriver in my hand-written python test code.

How can I get my hand-written Python code to understand that this object is of a type that it knows about?