| 1 | # flatted (Go)
|
|---|
| 2 |
|
|---|
| 3 | A super light and fast circular JSON parser.
|
|---|
| 4 |
|
|---|
| 5 | ## Usage
|
|---|
| 6 |
|
|---|
| 7 | ```go
|
|---|
| 8 | package main
|
|---|
| 9 |
|
|---|
| 10 | import (
|
|---|
| 11 | "fmt"
|
|---|
| 12 | "github.com/WebReflection/flatted/golang/pkg/flatted"
|
|---|
| 13 | )
|
|---|
| 14 |
|
|---|
| 15 | type Group struct {
|
|---|
| 16 | Name string `json:"name"`
|
|---|
| 17 | }
|
|---|
| 18 |
|
|---|
| 19 | type User struct {
|
|---|
| 20 | Name string `json:"name"`
|
|---|
| 21 | Friend *User `json:"friend"`
|
|---|
| 22 | Group *Group `json:"group"`
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | func main() {
|
|---|
| 26 | group := &Group{Name: "Developers"}
|
|---|
| 27 | alice := &User{Name: "Alice", Group: group}
|
|---|
| 28 | bob := &User{Name: "Bob", Group: group}
|
|---|
| 29 |
|
|---|
| 30 | alice.Friend = bob
|
|---|
| 31 | bob.Friend = alice // Circular reference
|
|---|
| 32 |
|
|---|
| 33 | // Stringify Alice
|
|---|
| 34 | s, _ := flatted.Stringify(alice)
|
|---|
| 35 | fmt.Println(s)
|
|---|
| 36 | // Output: [{"name":"Alice","friend":"1","group":"2"},{"name":"Bob","friend":"0","group":"2"},{"name":"Developers"}]
|
|---|
| 37 |
|
|---|
| 38 | // Flattening in action:
|
|---|
| 39 | // Index "0" is Alice, Index "1" is Bob, Index "2" is the shared Group.
|
|---|
| 40 |
|
|---|
| 41 | // Parse back into a generic map structure
|
|---|
| 42 | res, _ := flatted.Parse(s)
|
|---|
| 43 | aliceMap := res.(map[string]any)
|
|---|
| 44 | fmt.Println(aliceMap["name"]) // Alice
|
|---|
| 45 | }
|
|---|
| 46 | ```
|
|---|
| 47 |
|
|---|
| 48 | ## CLI
|
|---|
| 49 |
|
|---|
| 50 | Build the binary using the provided Makefile:
|
|---|
| 51 |
|
|---|
| 52 | ```bash
|
|---|
| 53 | make build
|
|---|
| 54 | ```
|
|---|
| 55 |
|
|---|
| 56 | Then use it to parse flatted JSON from stdin:
|
|---|
| 57 |
|
|---|
| 58 | ```bash
|
|---|
| 59 | echo '[{"a":"1"},"b"]' | ./flatted
|
|---|
| 60 | ```
|
|---|