Skip to content

Instantly share code, notes, and snippets.

@anissen
Forked from francescoagati/Main.hx
Last active November 26, 2015 13:13
Show Gist options
  • Save anissen/999e2ef0ee057319e99e to your computer and use it in GitHub Desktop.
Save anissen/999e2ef0ee057319e99e to your computer and use it in GitHub Desktop.
Import JSON in Haxe at compile time
-js main.js
-main Main
-dce full
-D analyzer
#if macro
import haxe.macro.Context;
#end
class Main {
macro static function load_json(file :String) {
var data = sys.io.File.getContent(file);
var json = haxe.Json.parse(data);
return Context.makeExpr(json, Context.currentPos());
}
static function main() {
var json = load_json('person.json');
trace(json);
}
}
(function (console) { "use strict";
var Main = function() { };
Main.main = function() {
var json = { isAlive : true, phoneNumbers : [{ number : "212 555-1234", type : "home"},{ number : "646 555-4567", type : "office"}], address : { state : "NY", streetAddress : "21 2nd Street", postalCode : "10021-3100", city : "New York"}, age : 25, firstName : "John", children : [], lastName : "Smith", spouse : null};
console.log(json);
};
Main.main();
})(typeof console != "undefined" ? console : {log:function(){}});
{
"firstName": "John",
"lastName": "Smith",
"isAlive": true,
"age": 25,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100"
},
"phoneNumbers": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "office",
"number": "646 555-4567"
}
],
"children": [],
"spouse": null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment