json

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 3, 2024 License: Apache-2.0 Imports: 5 Imported by: 2

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

func Escape(in string) string

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

func Marshal(v any) ([]byte, error)

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

func MarshalIndent(v any, a, b string) ([]byte, error)

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

func Unmarshal(buf []byte, v any) error

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) JSON

func (s This) JSON() ([]byte, error)

JSON implements AsJSON.

func (This) Log

func (s This) Log()

Log implements AsJSON.

func (This) Print

func (s This) Print()

Print implements AsJSON printing with fmt.Println (adding a line return).

func (This) String

func (s This) String() string

String implements AsJSON and logs any error.

func (*This) UnmarshalJSON

func (s *This) UnmarshalJSON(buf []byte) error

UnmarshalJSON implements AsJSON

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL