Documentation
¶
Overview ¶
Package json contains interface specifications for representing any Go type as JSON where possible. Using the goprintasjson tool allows for quick code generation of scaffolding to make any Go type easily used as JSON.
For querying JSON/YAML/TOML/XML structured data the github.com/rwxrob/yq functions are recommended but not included due to the large number of dependencies that would forced into this project.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Escape ¶
specification (unlike the encoding/json standard which defaults to escaping many other characters as well unnecessarily).
Example ¶
package main
import (
"github.com/rwxrob/bonzai/fn"
json "github.com/rwxrob/bonzai/json"
)
func main() {
set := fn.A[string]{
`<`, `>`, `&`, `"`, `'`,
"\t", "\b", "\f", "\n", "\r",
"\\", "\"", "💢", "д",
}
set.Map(json.Escape).Print()
}
Output: <>&\"'\t\b\f\n\r\\\"💢д
func Marshal ¶
Marshal mimics json.Marshal from the encoding/json package without the broken, unnecessary HTML escapes and extraneous newline that the json.Encoder adds. Call this from your own MarshalJSON methods to get JSON rendering that is more readable and compliant with the JSON specification (unless you are using the extremely rare case of dumping that into HTML, for some reason). Note that this cannot be called from any structs MarshalJSON method on itself because it will cause infinite functional recursion. Write a proper MarshalJSON method or create a dummy struct and call json.Marshal on that instead.
Example ¶
package main
import (
stdjson "encoding/json"
"fmt"
json "github.com/rwxrob/bonzai/json"
)
func main() {
m := map[string]string{"<foo>": "&bar"}
// the good way
buf, err := json.Marshal(m)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(buf))
// the broken encoding/json way
buf, err = stdjson.Marshal(m)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(buf))
}
Output: {"<foo>":"&bar"} {"\u003cfoo\u003e":"\u0026bar"}
func MarshalIndent ¶
MarshalIndent mimics json.Marshal from the encoding/json package but without the escapes, etc. See Marshal.
Example ¶
package main
import (
stdjson "encoding/json"
"fmt"
json "github.com/rwxrob/bonzai/json"
)
func main() {
m := map[string]string{"<foo>": "&bar"}
// the good way
buf, err := json.MarshalIndent(m, " ", " ")
if err != nil {
fmt.Println(err)
}
fmt.Println(string(buf))
// the broken encoding/json way
buf, err = stdjson.MarshalIndent(m, " ", " ")
if err != nil {
fmt.Println(err)
}
fmt.Println(string(buf))
}
Output: { "<foo>": "&bar" } { "\u003cfoo\u003e": "\u0026bar" }
func Unmarshal ¶
Unmarshal mimics json.Unmarshal from the encoding/json package.
Example ¶
package main
import (
"fmt"
json "github.com/rwxrob/bonzai/json"
)
func main() {
m := new(map[string]string)
if err := json.Unmarshal([]byte(`{"<foo>":"&bar"}`), m); err != nil {
fmt.Println(err)
}
fmt.Println(m)
if err := json.Unmarshal([]byte(`{"<foo>":"&bar"}`), m); err != nil {
fmt.Println(err)
}
fmt.Println(m)
}
Output: &map[<foo>:&bar] &map[<foo>:&bar]
Types ¶
type This ¶
type This struct{ This any }
This encapsulates anything with the AsJSON interface from this package by simply assigning a new variable with that item as the only value in the structure:
something := []string{"some","thing"}
jsonified := json.This{something}
jsonified.Print()
Example (Bool) ¶
package main
import (
json "github.com/rwxrob/bonzai/json"
)
func main() {
this := json.This{true}
this.Print()
}
Output: true
Example (Map) ¶
package main
import (
json "github.com/rwxrob/bonzai/json"
)
func main() {
this := json.This{map[string]string{"foo": "bar"}}
this.Print()
}
Output: {"foo":"bar"}
Example (Nil) ¶
package main
import (
json "github.com/rwxrob/bonzai/json"
)
func main() {
this := json.This{nil}
this.Print()
}
Output: null
Example (Numbers) ¶
package main
import (
json "github.com/rwxrob/bonzai/json"
)
func main() {
this := json.This{23434}
this.Print()
this.This = -24.24234
this.Print()
}
Output: 23434 -24.24234
Example (Slice) ¶
package main
import (
json "github.com/rwxrob/bonzai/json"
)
func main() {
this := json.This{[]string{"foo", "bar"}}
this.Print()
}
Output: ["foo","bar"]
Example (String) ¶
package main
import (
json "github.com/rwxrob/bonzai/json"
)
func main() {
this := json.This{"foo"}
this.Print()
this.This = "!some"
this.Print()
}
Output: "foo" "!some"
Example (Struct) ¶
package main
import (
json "github.com/rwxrob/bonzai/json"
)
func main() {
this := json.This{struct {
Foo string
Slice []string
}{"foo", []string{"one", "two"}}}
this.Print()
}
Output: {"Foo":"foo","Slice":["one","two"]}
func (This) Print ¶
func (s This) Print()
Print implements AsJSON printing with fmt.Println (adding a line return).
func (*This) UnmarshalJSON ¶
UnmarshalJSON implements AsJSON