Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nadako/hxjsonast
Parse JSON into position-aware AST with Haxe!
https://github.com/nadako/hxjsonast
ast haxe json parsing
Last synced: 2 months ago
JSON representation
Parse JSON into position-aware AST with Haxe!
- Host: GitHub
- URL: https://github.com/nadako/hxjsonast
- Owner: nadako
- License: mit
- Created: 2016-09-12T20:53:04.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2024-03-11T09:01:30.000Z (10 months ago)
- Last Synced: 2024-05-01T21:40:13.922Z (8 months ago)
- Topics: ast, haxe, json, parsing
- Language: Haxe
- Size: 201 KB
- Stars: 32
- Watchers: 6
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
[![Build Status](https://travis-ci.org/nadako/hxjsonast.svg?branch=master)](https://travis-ci.org/nadako/hxjsonast)
# hxjsonast - typed position aware JSON parsing for Haxe
This library contains a JSON parser that parses JSON (sic!) into a position-aware typed
value objects. It also contains a printer for those objects, supporting pretty-printing.This is useful for writing all kinds of JSON validation and processing software.
The parsing and printing code comes from standard `haxe.format.JsonParser/JsonPrinter` classes,
adapted to work with custom data structures.## Installation
```
haxelib install hxjsonast
```## Usage
Generated API documentation is here: ,
but a code snippet is worth a thousand words (compile with `-lib hxjsonast`):
```haxe
import hxjsonast.*;class Main {
static function main() {
var filename = 'person.json';
var contents = '{"name": "Dan", "age": 29, "married": true}';// parsing is easy!
var json = hxjsonast.Parser.parse(contents, filename);// `pos` store the filename, start and end characters
trace(json.pos); // {file: 'person.json', min: 0, max: 43}// `value` is an enum, easy to work with pattern matching
switch (json.value) {
case JNull: trace('null!');
case JString(string): trace('string!');
case JBool(bool): trace('boolean!');
case JNumber(number): trace('number!');
case JArray(values): trace('array!');
case JObject(fields): trace('object!');
}// constructing Json is easy too, we just pass position and value to its constructor
var myJson = new Json(
JArray([
new Json(JString("hello"), new Position("some.json", 3, 10)),
new Json(JString("world"), new Position("other.json", 11, 30)),
]),
new Position("some.json", 0, 42)
);// with Haxe 3.3, we can also use new fancy @:structInit syntax instead of classic `new` operator, e.g.
var myJson:Json = {
pos: {file: "some.json", min: 0, max: 42},
value: JArray([
{
pos: {file: "some.json", min: 3, max: 10},
value: JString("hello"),
},
{
pos: {file: "other.json", min: 11, max: 30},
value: JString("world")
}
])
};// printing is easy as well (you can also pretty-print by specifying the second argument)
var out = hxjsonast.Printer.print(myJson);
trace(out); // ["hello","world"]// there's a tool to convert Json values into "normal" objects and arrays
var value = hxjsonast.Tools.getValue(myJson);
trace(Std.is(value, Array)); // true
}
}
```