HxJsonDef is command-line app/macro/class written in Haxe that helps you quickly define a typedef from some arbitrary .json
I found a gist written in ruby that does the same (written by Justin Donaldson) and it inspired to write it in Haxe/Neko.
There are some rules you have to keep in mind.
For the conversion it best NOT to use null
as a value.
It will be automatically converted to Dynamic
, but if you know what the value will be; remove null
and replace with one of the values described below.
JSON values can be:
- A number (integer or floating point
1
/1.1
) - A string (in double quotes
"...."
) - A Boolean (
true
orfalse
) - An array (in square brackets
[...]
) - An object (in curly braces
{...}
) - null
https://www.w3schools.com/js/js_json_datatypes.asp
How to get this working?
You might want to update and/or correct the externs: install this repo locally
haxelib dev hxjsondef path/to/folder/src
Or use the developers version / cutting edge: this git repo
haxelib git hxjsondef https://github.com/MatthijsKamstra/hxjsondef
And don't forget to add it to your build file
-lib hxjsondef
You choose what works best for you:
- Initialization macro in your build file
- haxelib run
- neko
- via class
- online
Install the lib and add this to your build.hmxl
--macro hxjsondef.macro.AutomaticJsonDef.build('bin/example','src/ast')
First param is the source folder of the jsons, the second param is the "export" folder
Remember this is done every time you build. And the .hx
are automaticly overwritten so any changes to that file are gone.
NOTE: this macro needs more love, so let me know what would work better.
Install the lib and use your terminal to generate a .hx
file based upon a .json
file.
cd path/to/folder
haxelib run hxjsondef filename.json
And next to the filename.json
there will be the converted ASTfilename.hx
file.
You need to send the file you want to change. I might consider creating it for folders as well. Just let me know. Create an issue or a pull request.
- Download this repo
- Open your terminal
cd
to the correct folder (where hxjsondef is located:/hxjsondef/bin/
)
neko hxjsondef /Volumes/path/to/foobar.json
And next to the foobar.json
there will be the converted ASTfoobar.hx
file.
See two json file converted in the example folder
Add lib to build.hxml
-lib hxjsondef
Example code
var str = '{
"id": 1,
"name": "A green door",
"price": 12.50,
"tags": ["home", "green"]
}';
var hxjsondef = new Hxjsondef();
hxjsondef.fileName = 'foo';
trace(hxjsondef.convert('foobar', str));