-
-
Save anissen/999e2ef0ee057319e99e to your computer and use it in GitHub Desktop.
Import JSON in Haxe at compile time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-js main.js | |
-main Main | |
-dce full | |
-D analyzer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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(){}}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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