Documentation
¶
Overview ¶
Package jsonify provides utility functions for JSON encoding of various types, including protobuf messages and standard Go types.
It uses a custom jsoniter configuration for improved performance and consistent output.
custom jsoniter configuration:
var config = jsoniter.Config{
SortMapKeys: true,
ValidateJsonRawMessage: true,
}.Froze()
This configuration is similar to jsoniter.ConfigCompatibleWithStandardLibrary. The only difference is that EscapeHTML is set to false.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Bytes ¶
Bytes encodes the given value as JSON and returns it as a byte slice.
It handles json.RawMessage, proto.Message, and other types differently. For json.RawMessage, it returns the raw bytes. For proto.Message, it uses protojson for marshaling. For other types, it uses a custom jsoniter configuration.
Example ¶
package main
import (
"fmt"
"github.com/goaux/jsonify"
)
func main() {
b, _ := jsonify.Bytes(map[string]interface{}{"A": true, "B": "<b>"})
fmt.Printf(">%s<\n", b)
}
Output: >{"A":true,"B":"<b>"}<
func MustBytes ¶
MustBytes is similar to Bytes but panics if an error occurs during encoding.
It's useful when you're certain that the encoding will succeed.
Example ¶
package main
import (
"fmt"
"github.com/goaux/jsonify"
)
func main() {
fmt.Printf(">%s<\n", jsonify.MustBytes(map[string]any{"A": true, "B": "<b>"}))
}
Output: >{"A":true,"B":"<b>"}<
func MustString ¶
MustString is similar to String but panics if an error occurs during encoding.
It's useful when you're certain that the encoding will succeed.
Example ¶
package main
import (
"fmt"
"github.com/goaux/jsonify"
)
func main() {
fmt.Println(">" + jsonify.MustString(map[string]any{"A": true, "B": "<b>"}) + "<")
}
Output: >{"A":true,"B":"<b>"}<
func String ¶
String encodes the given value as JSON and returns it as a string.
It handles json.RawMessage, proto.Message, and other types differently. For json.RawMessage, it returns the raw message as a string. For proto.Message, it uses protojson for marshaling. For other types, it uses a custom jsoniter configuration.
Example ¶
package main
import (
"fmt"
"github.com/goaux/jsonify"
)
func main() {
s, _ := jsonify.String(map[string]interface{}{"A": true, "B": "<b>"})
fmt.Printf(">%s<\n", s)
}
Output: >{"A":true,"B":"<b>"}<
Types ¶
This section is empty.