Skip to content

Yet Another JSON Parser, but parsed using bottom-up parsing technique for educational purposes

License

Notifications You must be signed in to change notification settings

rhaeguard/gojson

Repository files navigation

gojson

A JSON parser written in Go using Shift-Reduce Parsing technique without any parsing table.

read the article

to add the dependency:

go get github.com/rhaeguard/gojson

how to use:

import (
    "fmt"
    "github.com/rhaeguard/gojson"
)

type WebConfig struct {
    Hostname string
    Port     int
    IsActive bool
}

func main() {
    inputJson := `{
              "Hostname": "localhost",
              "Port": 8282,
              "IsActive": true
        }`
    json, err := gojson.Parse(inputJson)
    
    if err != nil {
        // error occurred
    }
    
	// we can directly try to get the value from the JsonValue object
    objectFields := json.Value.(map[string]gojson.JsonValue)
    port := int(objectFields["Port"].Value.(float64)) // all numeric values are converted to float64
    fmt.Printf("%d\n", port)
    
	// we can also try to map the values to a struct
    var wc WebConfig
    if uerr := json.Unmarshal(&wc); uerr != nil {
        // error occurred
    } else {
        fmt.Printf("%-v\n", wc.Port)
    }

	// we can unmarshall in one step as well
    if uerr := gojson.Unmarshal(inputJson, &wc); uerr != nil {
        // error occurred
    } else {
        fmt.Printf("%-v\n", wc.Port)
    }
}

About

Yet Another JSON Parser, but parsed using bottom-up parsing technique for educational purposes

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages