gson

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 29, 2025 License: Apache-2.0 Imports: 2 Imported by: 1

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:

Supported Operations

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

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

Marshal returns the JSON-encoded bytes of v.

func MarshalBy

func MarshalBy[T any](codec Marshaler, v T) ([]byte, error)

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

func MarshalIndent[V any](v V, prefix, indent string) ([]byte, error)

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

func MarshalString[V any](v V) (string, error)

MarshalString returns the JSON-encoded string of v.

func MarshalStringBy

func MarshalStringBy[T any](codec Marshaler, v T) (string, error)

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 ToString

func ToString[V any](v V) string

ToString returns the JSON-encoded string of v and ignores error.

func ToStringBy

func ToStringBy[T any](codec Marshaler, v T) string

ToStringBy returns the marshaled string representation of v using the codec, ignoring errors.

func ToStringIndent

func ToStringIndent[V any](v V, prefix, indent string) string

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 Unmarshal

func Unmarshal[T any, V ~[]byte | ~string](v V) (T, error)

Unmarshal parses the JSON-encoded bytes and string and returns the result.

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.

func Valid

func Valid[V ~[]byte | ~string](data V) bool

Valid reports whether data is a valid JSON encoding.

func ValidBy

func ValidBy[V ~[]byte | ~string](codec Validator, data V) bool

ValidBy reports whether the input data is valid according to the given codec.

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 Marshaler

type Marshaler interface {
	Marshal(v any) ([]byte, error)
}

Marshaler defines the interface for serializing a value into a byte slice.

type PrettyMarshaler

type PrettyMarshaler interface {
	MarshalIndent(v any, prefix, indent string) ([]byte, error)
}

PrettyMarshaler defines the interface for pretty-printing serialized output with indentation.

type Unmarshaler

type Unmarshaler interface {
	Unmarshal(data []byte, out any) error
}

Unmarshaler defines the interface for deserializing data into a Go value.

type Validator

type Validator interface {
	Valid(data []byte) bool
}

Validator defines the interface for validating whether a byte slice is a valid-encoded format.

Jump to

Keyboard shortcuts

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