Skip to content

Instantly share code, notes, and snippets.

@francescoagati
Created November 26, 2015 00:41
Show Gist options
  • Save francescoagati/f89b6cca929b2f75bf9c to your computer and use it in GitHub Desktop.
Save francescoagati/f89b6cca929b2f75bf9c to your computer and use it in GitHub Desktop.
import yaml in haxe at compile time
-js main.js
-main Main
-lib yaml
-dce full
-D analyzer
invoice: 34843
date : 2001-01-23
bill_to: &id001
given : Chris
family : Dumars
address:
lines: |
458 Walkman Dr.
Suite #292
city : Royal Oak
state : MI
postal : 48046
ship_to: *id001
tax : 251.42
457: true
total: 4443.52
comments: >
Late afternoon is best.
Backup contact is Nancy
Billsmer @ 338-4338.
#if macro
import yaml.Yaml;
import yaml.Parser;
import yaml.Renderer;
import yaml.util.ObjectMap;
import haxe.macro.Context;
#end
class Main {
macro static function load_yaml(file:String) {
var data = Yaml.read(file, Parser.options().useObjects());
var s = haxe.Json.stringify(data);
var json = haxe.Json.parse(s);
return Context.makeExpr(json, Context.currentPos());
}
static function main() {
var json = load_yaml('invoice.yaml');
trace(json);
}
}
(function (console) { "use strict";
var Main = function() { };
Main.main = function() {
var json = { total : 4443.52, ship_to : { address : { state : "MI", postal : 48046, lines : "458 Walkman Dr.\nSuite #292\n", city : "Royal Oak"}, given : "Chris", family : "Dumars"}, tax : 251.42, '457' : true, bill_to : { address : { state : "MI", postal : 48046, lines : "458 Walkman Dr.\nSuite #292\n", city : "Royal Oak"}, given : "Chris", family : "Dumars"}, invoice : 34843, date : "2001-01-23 00:00:00", comments : "Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338.\n"};
console.log(json);
};
Main.main();
})(typeof console != "undefined" ? console : {log:function(){}});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment