Git Product home page Git Product 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

        //Duplicate keys are detected and not allowed (ECMAScript JSON.parse doesn't detect)
        var json = hxjsonast.Parser.parse('{"a":1,"a":2}', "bad"); //throws

        //Option to disable (for parsing non-conforming JSON e. g. FreeSwitch)
        var parser=new hxjsonast.Parser();
        parser.allowDuplicateKeys=true;
        trace(parser.doParse());//allowing parse;
    }
}

hxjsonast's People

Contributors

nadako avatar elnabo avatar neimanpinchas avatar

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.