/// This works
Config.json("assets/data/file.json");
But if you pass it a var it doesn’t works, I get this error:
/// This doesn't work
var test: String = "assets/data/file.json";
parsedJson = Config.json(test);
haxe.macro.Expr should be String
Any help?
Thanks in advance.
/// The Macro
class Config
{
macro public static function json(path: String) {
var value = sys.io.File.getContent(path),
json = haxe.Json.parse(value);
return macro $v{json};
}
}
Macros are run at compile time, if you do Config.json(test) the macro receive an argument “identifier named test” and can’t do anything with that, that macro can only receive string literals directly.
One alternative is you can embed a resource, within your code ( does not need openfl or any toolkit ), I use it on my ‘letters’ repo canvas experiment heavily.
In your hxml file.
-resource assets/data/file.json@myFile
then in haxe
var j: MyFileTdef = haxe.Json.parse( haxe.Resource.getString(myFile));