Giter Club home page Giter Club logo

hxjsonast's Introduction

Build Status

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: https://nadako.github.io/hxjsonast/, but a code snippet is worth a thousand words (compile with -lib hxjsonast):

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
    }
}

hxjsonast's People

Contributors

elnabo avatar nadako avatar neimanpinchas avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

hxjsonast's Issues

Better library name

I'm concerned that the name hxjson is too generic for this library. It's for JSON indeed, but its purpose is quite specific, so maybe it needs a better name. I can't think of a good one right now...

invalid numbers are parsed

invalid JSONs like 1asd will be parsed as JNumber("1") while it should throw an error
same behaviour in haxe.format.JsonParser...

Typed error throwing

Instead of string errors, I'd like to throw a hxjson.Error object containing message and Position. This one could be reused for validation errors as well.

Please make a release on haxelib

The last published version is 5 years old, and the recent pull request #11 fixed an important issue for Haxe 4 users. When installing hxjsonast as a dependency of another project, haxelib / lix will install the 5 years old published version by default. This means broken functionality for Haxe 4 users. Please consider making a release on haxelib to let users get the latest version of this great library by default. Thank you for your efforts.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.