Parse JSON into an AST (Work In Progress)
Go Makefile
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.gitignore
LICENSE
Makefile
README.md
array.go
bool.go
glide.lock
glide.yaml
null.go
number.go
number_test.go
object.go
parse.go
path.go
string.go
token.go
value.go
walker.go
writer.go
writer_test.go

README.md

jsonast

This repository is dedicated to parsing a JSON string into an abstract syntax tree (AST).

Based on the following Scala libraries:

Beware: it is pre-alpha and a major work in progress!

Example Usage

type Address struct {
    StreetNum int
    StreetName string
    City string
    State string
    GateCode int
}

val := jsonast.Parse(`{
    "street_num":1,
    "street_name": "Microsoft Way",
    "city": "Redmond",
    "state": "WA",
    "other_info": {
        "needs_signature": false,
        "gate_codes": ["abcd", "efgh"]
    }
}`)

walked := jsonast.NewWalker(val).
    Field("street_num").
    Field("street_name").
    Field("city").
    Field("state").Validate(func(v Value) bool {
        str, ok := v.(jsonast.String)
        if !ok {
            return false
        }
        // only accept the JSON if the state has a coast on the pacific
        // ocean
        return str.String() == "AK" || 
            str.String() == "WA" ||
            str.String() == "OR" ||
            str.String() == "CA"
    }).
    Path(
        jsonast.NewStringPathElt("other_info"),
        jsonast.NewStringPathElt("gate_codes"),
        jsonast.NewIntPathElt(0),
    )

if transformer.Err() != nil {
    log.Fatal(transformer.Err())
}

// walked now has the following fields in the following properties:
//
// - .Strings has 3 jsonast.String values (street_name, city, state)
// - .Numbers has 2 jsonast.Number values (street_num, other_info.gate_codes[0])
//
// you can now create an Address from these fields
address := Address{
    StreetNum: walked.Numbers[0],
    StreetName: walked.Strings[0],
    City: walked.Strings[1],
    State: walked.Strings[2],
    GateCode: walked.Numbers[1],
}