https://github.com/rhaeguard/gojson
Yet Another JSON Parser, but parsed using bottom-up parsing technique for educational purposes
https://github.com/rhaeguard/gojson
bottom-up-parser golang json json-parser
Last synced: 9 months ago
JSON representation
Yet Another JSON Parser, but parsed using bottom-up parsing technique for educational purposes
- Host: GitHub
- URL: https://github.com/rhaeguard/gojson
- Owner: rhaeguard
- License: mit
- Created: 2023-10-20T08:13:42.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-11-14T19:52:01.000Z (over 2 years ago)
- Last Synced: 2025-09-23T17:03:49.756Z (9 months ago)
- Topics: bottom-up-parser, golang, json, json-parser
- Language: Go
- Homepage: https://rhaeguard.github.io/posts/json-parsing-shift-reduce/
- Size: 61.5 KB
- Stars: 7
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# gojson

A JSON parser written in Go using Shift-Reduce Parsing technique without any parsing table.
read the [article](https://rhaeguard.github.io/posts/json-parsing-shift-reduce/)
### to add the dependency:
```shell
go get github.com/rhaeguard/gojson
```
### how to use:
```go
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)
}
}
```