Documentation
¶
Overview ¶
Package gson provides operations of JSON encoding and decoding.
Package gson provides operations of JSON encoding and decoding, using the provided codec arbitrary values using pluggable serialization strategies (e.g., JSON, MsgPack).
Codec Abstraction ¶
The Codec interface unifies encoding and decoding logic for arbitrary types. You can implement this interface for various formats such as JSON, YAML, or MsgPack, Avro.
For example:
- Use encoding/json for JSONCodec
- Use github.com/bytedance/sonic for high-performance JSONCodec
- Use github.com/json-iterator/go for customizable JSONCodec
- Use github.com/vmihailenco/msgpack/v5 for MsgpackCodec
Supported Operations ¶
- Validation: ValidBy
- Marshal to []byte: MarshalBy, MarshalIndentBy
- Marshal to string: MarshalStringBy, ToStringBy, ToStringIndentBy
- Unmarshal to object: UnmarshalBy
Example ¶
type testStruct struct {
Name string `json:"name"`
Age int `json:"age"`
}
testcase := testStruct{Name: "test", Age: 10}
fmt.Println(string(gresult.Of(Marshal(testcase)).Value())) // `{"name":"test","age":10}`
fmt.Println(gresult.Of(MarshalString(testcase)).Value()) // `{"name":"test","age":10}`
fmt.Println(ToString(testcase)) // `{"name":"test","age":10}`
fmt.Println(string(gresult.Of(MarshalIndent(testcase, "", " ")).Value())) // "{\n \"name\": \"test\",\n \"age\": 10\n}"
fmt.Println(ToStringIndent(testcase, "", " ")) // "{\n \"name\": \"test\",\n \"age\": 10\n}"
fmt.Println(Valid(`{"name":"test","age":10}`)) // true
fmt.Println(Unmarshal[testStruct](`{"name":"test","age":10}`)) // {test 10} nil
Output: {"name":"test","age":10} {"name":"test","age":10} {"name":"test","age":10} { "name": "test", "age": 10 } { "name": "test", "age": 10 } true {test 10} <nil>
Index ¶
- func Marshal[V any](v V) ([]byte, error)
- func MarshalBy[T any](codec Marshaler, v T) ([]byte, error)
- func MarshalIndent[V any](v V, prefix, indent string) ([]byte, error)
- func MarshalIndentBy[T any](codec PrettyMarshaler, v T, prefix, indent string) ([]byte, error)
- func MarshalString[V any](v V) (string, error)
- func MarshalStringBy[T any](codec Marshaler, v T) (string, error)
- func ToString[V any](v V) string
- func ToStringBy[T any](codec Marshaler, v T) string
- func ToStringIndent[V any](v V, prefix, indent string) string
- func ToStringIndentBy[T any](codec PrettyMarshaler, v T, prefix, indent string) string
- func Unmarshal[T any, V ~[]byte | ~string](v V) (T, error)
- func UnmarshalBy[T any, V ~[]byte | ~string](codec Unmarshaler, v V) (T, error)
- func Valid[V ~[]byte | ~string](data V) bool
- func ValidBy[V ~[]byte | ~string](codec Validator, data V) bool
- type Codec
- type JSONCodec
- type Marshaler
- type PrettyMarshaler
- type Unmarshaler
- type Validator
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MarshalBy ¶
MarshalBy marshals the value v into bytes using the provided codec.
🚀 Example:
MarshalBy(codec, map[string]any{"name": "test", "age": 10}) ⏩ []byte("{\"name\":\"test\",\"age\":10}")
💡 HINT: For high-performance JSON serialization, see github.com/json-iterator/go or github.com/bytedance/sonic. Common implementations include jsoniter.ConfigDefault, and sonic.ConfigDefault.
func MarshalIndent ¶
MarshalIndent returns the JSON-encoded bytes with indent and prefix.
func MarshalIndentBy ¶
func MarshalIndentBy[T any](codec PrettyMarshaler, v T, prefix, indent string) ([]byte, error)
MarshalIndentBy marshals the value v into indented bytes using the provided codec.
func MarshalString ¶
MarshalString returns the JSON-encoded string of v.
func MarshalStringBy ¶
MarshalStringBy marshals the value v into a JSON string using the provided codec.
🚀 Example:
MarshalStringBy(codec, map[string]any{"name": "test", "age": 10}) ⏩ "{\"name\":\"test\",\"age\":10}"
💡 HINT: For high-performance JSON serialization, see github.com/json-iterator/go or github.com/bytedance/sonic.
func ToStringBy ¶
ToStringBy returns the marshaled string representation of v using the codec, ignoring errors.
func ToStringIndent ¶
ToStringIndent returns the JSON-encoded string with indent and prefix of v and ignores error.
func ToStringIndentBy ¶
func ToStringIndentBy[T any](codec PrettyMarshaler, v T, prefix, indent string) string
ToStringIndentBy returns the indented string representation of v using the codec, ignoring errors.
func UnmarshalBy ¶
func UnmarshalBy[T any, V ~[]byte | ~string](codec Unmarshaler, v V) (T, error)
UnmarshalBy unmarshals the input data v into a value of type T using the provided codec.
🚀 Example:
UnmarshalBy[User](codec, `{"name":"test","age":10}`) ⏩ User{Name: "test", Age: 10}
💡 HINT: For high-performance JSON decoding, see github.com/json-iterator/go or github.com/bytedance/sonic.
Types ¶
type Codec ¶
type Codec interface {
Marshaler
Unmarshaler
}
Codec is the minimal interface for encoding and decoding values. It combines Marshaler and Unmarshaler.
type JSONCodec ¶
type JSONCodec interface {
Codec
Validator
PrettyMarshaler
}
JSONCodec is an extended interface for codecs that support indentation and validation. It combines Codec, Validator, and PrettyMarshaler.
type PrettyMarshaler ¶
PrettyMarshaler defines the interface for pretty-printing serialized output with indentation.
type Unmarshaler ¶
Unmarshaler defines the interface for deserializing data into a Go value.