Skip to content

Instantly share code, notes, and snippets.

@markknol
Created December 4, 2015 13:23
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save markknol/59f0ede823f7d511362b to your computer and use it in GitHub Desktop.
Save markknol/59f0ede823f7d511362b to your computer and use it in GitHub Desktop.
Macro - validate a JSON compiletime
/**
* @author Mark Knol
*/
class MacroJsonValidator {
public static macro function validateJson(path:String) {
if (sys.FileSystem.exists(path)) {
var content = sys.io.File.getContent(path);
try {
// Test the json by parsing it.
// It will throw an error when you made a mistake.
haxe.Json.parse(content);
} catch (error:String) {
// create position inside the json, FlashDevelop handles this very nice.
var position = Std.parseInt(error.split("position").pop());
var pos = haxe.macro.Context.makePosition({
min:position,
max:position + 1,
file:path
});
haxe.macro.Context.error(path + " is not valid Json. " + error, pos);
}
} else {
haxe.macro.Context.warning(path + " does not exist", haxe.macro.Context.currentPos());
}
return macro null;
}
}
class Test {
static function main() {
#if debug
MacroJsonValidator.validateJson("bin/inc/json/levels.json");
MacroJsonValidator.validateJson("bin/inc/json/copy.json");
#end
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment