This project is a solution for Write Your Own JSON Parser build for my personal educational purposes
- Parse json into interface{}
- TODO: implement json stringify
The implementation is based on the json specification Introducing JSON.
This step is responsible to create the tokens. A json scanner was implemented from scratch to accomplish this task.
This step is responsible to validate the correct structure that matches the formal grammar and create the syntax tree. The parser implemented in this project is a recursive descent parser based on the json context free grammar seen in the specification Introducing JSON.
The json is parsed directly as an interface{}. Can be used exactly like go manipulates Generic JSON
package main
import (
"JSONParser/JSONParser"
"JSONParser/Util"
"log"
"os"
)
func main() {
input, err := os.ReadFile("tests/step4/valid2.json")
if err != nil {
log.Fatal(err)
}
parsed, err := JSONParser.Parse(input)
if err != nil {
log.Fatal(err)
}
Util.Printify(parsed)
}
{
"key": "value",
"y": "\u0020",
"e": 12.5e-4,
"e2": 12e+4,
"e3": 12e-4,
"key-n": 101000,
"n" : -12445.1,
"key-o": {
"inner key": "inner value"
},
"key-l": [
"list value"
],
"l" : [1, 2, "dd", 3],
"nested": {
"n": {
"attr": true
}
}
}
{
"key-n": 101000,
"n": -12445.1,
"key-l": ["list value"],
"e": 0.00125,
"e2": 120000,
"e3": 0.0012,
"key-o": {
"inner key": "inner value"
},
"l": [1, 2, "dd", 3],
"nested": {
"n": {
"attr": true
}
},
"key": "value",
"y": " "
}
- clone the repo
git clone https://github.com/Leonardpepa/JSONParser
- build
go build
- run on windows
JSONParser.exe
- run on linux
./JSONParser
- test
go test ./...
The parser is tested comparing the results against the native go json package.
Run the tests go test ./...