jsonx

package
v3.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2025 License: MIT Imports: 8 Imported by: 0

README

jsonx

A specialized JSON handling package with enhanced support for Protocol Buffers.

Features

  • Marshal Method: When handling proto.Message or its pointers (regardless of nesting depth), internally uses protojson with EmitUnpopulated(true) to ensure consistency across all data consumers.

  • Unmarshal Method: When handling proto.Message or its multi-level pointers, internally uses protojson with DiscardUnknown(true) to ensure forward compatibility.

  • Patch Method: An enhanced version of Unmarshal that preserves original values.

  • Copy Method: Implemented based on Patch, used for data copying between objects.

Handling Nil / Null

When marshaling a nil value:

  • protojson.Marshal returns an empty object ("{}") for nil root Protocol Buffer messages
  • This behavior differs from standard json.Marshal which would typically return "null"
  • The jsonx package deliberately follows json.Marshal's behavior for nil Protocol Buffer messages, returning "null" instead of an empty object, providing more intuitive and consistent results

When unmarshaling a complete JSON string that is just "null":

  • json.Unmarshal doesn't produce an error when encountering a JSON string that is just "null"; it simply leaves the message unmodified
  • protojson.Unmarshal returns an error when the entire JSON string is "null", with a message like "unexpected token null"
  • The jsonx package follows json.Unmarshal's behavior, ensuring no errors occur when unmarshaling a JSON "null" to the destination

Patch vs. Unmarshal

The key differences between Patch and standard unmarshal methods:

  1. json.Unmarshal can deserialize a patch string to a destination struct while preserving original values for fields not specified in the patch. However, it will lose original values when the target is a nested map[string]any.

  2. protojson.Unmarshal differs from the standard JSON unmarshaler and doesn't preserve original values.

  3. Patch addresses these limitations by preserving original values even in complex nested structures.

Deviation from RFC7386

Patch doesn't fully comply with the RFC7386 JSON Merge Patch standard for the following reasons:

  1. In RFC7386, a null value in the patch indicates deletion of the original field in the destination JSON.

  2. In Go's typical unmarshal scenarios applied to structs, null is usually intended to override the original value.

  3. Patch works by first using Marshal(dest) to create a patch that can restore original values, then combining it with the user-provided patch to create a final patch that meets the required use case.

Important Note

For array fields, Patch, Copy, and RFC7386 all use full replacement. If incremental array modifications are needed in your application, you should:

  1. Clone the original information before calling Patch/Copy
  2. Manually handle incremental changes after the call

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Beautify

func Beautify[T ~string | ~[]byte](rawJSON T) (T, error)

func Compact added in v3.2.0

func Compact(dst *bytes.Buffer, src []byte) error

func Copy

func Copy(dst any, src any) error

func Equal

func Equal(a, b any) bool

func HTMLEscape added in v3.2.0

func HTMLEscape(dst *bytes.Buffer, src []byte)

func Indent added in v3.2.0

func Indent(dst *bytes.Buffer, src []byte, prefix string, indent string) error

func Marshal

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

Marshal converts a value to JSON bytes, with special handling for proto.Message types. Supports marshaling from proto.Message through multiple levels of pointers.

func MarshalIndent added in v3.2.0

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

func MarshalX

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

func MustBeautify

func MustBeautify[T ~string | ~[]byte](rawJSON T) T

func MustMarshal

func MustMarshal(v any) []byte

func MustMarshalX

func MustMarshalX[T ~string | ~[]byte](v any) T

func MustToMap added in v3.2.0

func MustToMap(v any) map[string]any

func NewDecoder added in v3.2.0

func NewDecoder(r io.Reader) *json.Decoder

func NewEncoder added in v3.2.0

func NewEncoder(w io.Writer) *json.Encoder

func Patch

func Patch(patch []byte, dest any) error

func ToMap added in v3.2.0

func ToMap(v any) (map[string]any, error)

func Unmarshal

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

Unmarshal parses JSON bytes into a value, with special handling for proto.Message types. Supports unmarshaling into proto.Message through multiple levels of pointers.

func Valid added in v3.2.0

func Valid(data []byte) bool

Types

type Decoder added in v3.2.0

type Decoder = json.Decoder

type Delim added in v3.2.0

type Delim = json.Delim

type Encoder added in v3.2.0

type Encoder = json.Encoder

type InvalidUnmarshalError added in v3.2.0

type InvalidUnmarshalError = json.InvalidUnmarshalError

type Marshaler added in v3.2.0

type Marshaler = json.Marshaler

type MarshalerError added in v3.2.0

type MarshalerError = json.MarshalerError

type Number added in v3.2.0

type Number = json.Number

type RawMessage added in v3.2.0

type RawMessage = json.RawMessage

type SyntaxError added in v3.2.0

type SyntaxError = json.SyntaxError

type Token added in v3.2.0

type Token = json.Token

type UnmarshalTypeError added in v3.2.0

type UnmarshalTypeError = json.UnmarshalTypeError

type Unmarshaler added in v3.2.0

type Unmarshaler = json.Unmarshaler

type UnsupportedTypeError added in v3.2.0

type UnsupportedTypeError = json.UnsupportedTypeError

type UnsupportedValueError added in v3.2.0

type UnsupportedValueError = json.UnsupportedValueError

Jump to

Keyboard shortcuts

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