Documentation
¶
Index ¶
- Constants
- func BareModifierFunc(bytes []byte) ([]byte, error)
- func Compact(dst io.Writer, src []byte) error
- func ErrEmpty(message string) error
- func Marshal(value any) ([]byte, error)
- func MarshalBool(value bool) []byte
- func MarshalFloat32(value float32) []byte
- func MarshalFloat64(value float64) []byte
- func MarshalInt(value int) []byte
- func MarshalInt8(value int8) []byte
- func MarshalInt16(value int16) []byte
- func MarshalInt32(value int32) []byte
- func MarshalInt64(value int64) []byte
- func MarshalSlice[T any](value []T) ([]byte, error)
- func MarshalString(value string) []byte
- func MarshalTextMarshaler(textMarshaler encoding.TextMarshaler) ([]byte, error)
- func MarshalUint(value uint) []byte
- func MarshalUint8(value uint8) []byte
- func MarshalUint16(value uint16) []byte
- func MarshalUint32(value uint32) []byte
- func MarshalUint64(value uint64) []byte
- func MergeAndMarshal(values ...any) ([]byte, error)
- func NormalizeNumberBytes(bytes []byte) []byte
- func NormalizeNumberString(str string) string
- func ObstructedUnmarshal(data []byte, dst any) error
- func StringModifierFunc(bytes []byte) ([]byte, error)
- func Unmarshal(data []byte, dst any) error
- func UnmarshalBool(bytes []byte, dst *bool) error
- func UnmarshalInt(data []byte, dst *int) error
- func UnmarshalInt8(data []byte, dst *int8) error
- func UnmarshalInt16(data []byte, dst *int16) error
- func UnmarshalInt32(data []byte, dst *int32) error
- func UnmarshalInt64(data []byte, dst *int64) error
- func UnmarshalString(data []byte, dst *string) error
- func UnmarshalUint(data []byte, dst *uint) error
- func UnmarshalUint8(data []byte, dst *uint8) error
- func UnmarshalUint16(data []byte, dst *uint16) error
- func UnmarshalUint32(data []byte, dst *uint32) error
- func UnmarshalUint64(data []byte, dst *uint64) error
- func UnobstructedUnmarshal(data []byte, dst any) error
- func UnobstructedUnmarshalBool(bytes []byte, dst *bool) error
- type Const
- type ConstMismatchError
- type Constantizer
- type Emptier
- type ErrorEmpty
- type Marshaler
- type ModifierFunc
- type Nothinger
- type Number
- type OmitAlways
- type UnknownFieldError
- type UnmarshalErrors
- type Unmarshaler
- type Usher
- func (receiver *Usher) ImplantModifier(name string, marshalFn ModifierFunc, unmarshalFn ModifierFunc)
- func (receiver *Usher) Marshal(value any) ([]byte, error)
- func (receiver *Usher) MergeAndMarshal(values ...any) ([]byte, error)
- func (receiver *Usher) ObstructedUnmarshal(data []byte, dst any) error
- func (receiver *Usher) Unmarshal(data []byte, dst any) error
- func (receiver *Usher) UnobstructedUnmarshal(data []byte, dst any) error
Constants ¶
Variables ¶
This section is empty.
Functions ¶
func BareModifierFunc ¶
BareModifierFunc is the modifier that, in the DefaultUsher, is the implementation behind the "bare" modifier.
For example:
struct {
// ...
Banana string `json:"banana,bare"`
// ...
}
func ErrEmpty ¶
ErrEmpty returns an error of type ErrorEmpty.
ErrorEmpty is used with the "omitempty" struct-field tag option.
func Marshal ¶
Marshal returns the JSON version of 'value'.
For example:
bytes, err := json.Marshal(value)
See Usher.Marshal for more information.
Calling:
bytes, err := json.Marshal(value)
Is the same as calling:
bytes, err := json.DefaultUsher.Marshal(value)
json.Marshal() is a convenience function for json.DefaultUsher.Marshal().
func MarshalBool ¶
MarshalBool returns the JSON version of a Go bool.
func MarshalFloat32 ¶
MarshalFloat32 returns the JSON version of a Go float32.
func MarshalFloat64 ¶
MarshalFloat64 returns the JSON version of a Go float64.
func MarshalInt ¶
MarshalInt returns the JSON version of a Go int.
func MarshalInt8 ¶
MarshalInt8 returns the JSON version of a Go int8.
func MarshalInt16 ¶
MarshalInt16 returns the JSON version of a Go int16.
func MarshalInt32 ¶
MarshalInt32 returns the JSON version of a Go int32.
func MarshalInt64 ¶
MarshalInt64 returns the JSON version of a Go uint64.
func MarshalSlice ¶
MarshalSlice returns the JSON version of a Go slice.
func MarshalString ¶
MarshalString returns the JSON version of a Go string.
func MarshalTextMarshaler ¶
func MarshalTextMarshaler(textMarshaler encoding.TextMarshaler) ([]byte, error)
MarshalTextMarshaler returns the JSON version of the successful result from encoding.TextMarshaler..
func MarshalUint ¶
MarshalUint returns the JSON version of a Go uint.
func MarshalUint8 ¶
MarshalUint8 returns the JSON version of a Go uint8.
func MarshalUint16 ¶
MarshalUint16 returns the JSON version of a Go uint16.
func MarshalUint32 ¶
MarshalUint32 returns the JSON version of a Go uint32.
func MarshalUint64 ¶
MarshalUint64 returns the JSON version of a Go uint64.
func MergeAndMarshal ¶
Marshal return the JSON version of 'value'.
func NormalizeNumberBytes ¶
NormalizeNumberBytes normalizes a JSON number. It expands scientific notation into plain decimal form (e.g., "3.7e-5" becomes "0.000037"), strips leading '+', unnecessary leading zeros, trailing zeros after a decimal point, and a trailing decimal point. A negative sign is preserved when appropriate.
NormalizeNumberBytes is similar to NormalizeNumberString except it accepts and returns a []byte (rather than a string).
See also: Number.
func NormalizeNumberString ¶
NormalizeNumberString normalizes a JSON number. It expands scientific notation into plain decimal form (e.g., "3.7e-5" becomes "0.000037"), strips leading '+', unnecessary leading zeros, trailing zeros after a decimal point, and a trailing decimal point. A negative sign is preserved when appropriate.
NormalizeNumberString is similar to NormalizeNumberBytes except it accepts and returns a string (rather than a []byte).
See also: Number.
func ObstructedUnmarshal ¶
ObstructedUnmarshal deserializes JSON data strictly. Returns an error if unknown JSON fields are encountered.
It is equivalent to calling DefaultUsher.ObstructedUnmarshal().
func StringModifierFunc ¶
StringModifierFunc is the modifier that, in the DefaultUsher, is the implementation behind the "string" modifier.
For example:
struct {
// ...
Banana int `json:"banana,string"`
// ...
}
func Unmarshal ¶
Unmarshal deserializes JSON data into the value pointed to by dst. Unknown JSON fields are silently ignored.
It is equivalent to calling DefaultUsher.Unmarshal().
func UnmarshalBool ¶
UnmarshalBool JSON-unmarshals a JSON bool into a Go bool.
func UnmarshalInt ¶
func UnmarshalInt8 ¶
func UnmarshalInt16 ¶
func UnmarshalInt32 ¶
func UnmarshalInt64 ¶
func UnmarshalString ¶
UnmarshalString JSON-unmarshals a JSON string into a Go string.
func UnmarshalUint ¶
func UnmarshalUint8 ¶
func UnmarshalUint16 ¶
func UnmarshalUint32 ¶
func UnmarshalUint64 ¶
func UnobstructedUnmarshal ¶
UnobstructedUnmarshal deserializes JSON data permissively. It allows trailing commas, comments, and leading plus-signs. Unknown JSON fields are silently ignored.
It is equivalent to calling DefaultUsher.UnobstructedUnmarshal().
func UnobstructedUnmarshalBool ¶
UnobstructedUnmarshal JSON-unmarshals a JSON bool into a Go bool. It ignore the letter-casing of "true" and "false", (also) accepts "t" and "f", and (also) accepts "1" and "0".
Types ¶
type Const ¶
type Const[T any] struct{}
Const is used to include a constant field in a struct.
For example:
type AcitivtyLink struct {
HRef string `json:"href"`
MediaType string `json:"mediatype"`
Rel string `json:"rel"`
Rev string `json:"rev"`
Type json.Const[string] `json:"type" json.value:"Link"` // <----------
}
Here is another example:
type Manitoban struct {
GivenName string `json:"given-name"`
AdditionalNames []string `json:"additional-names,omitempty"`
FamilyName string `json:"family-name"`
HomeCountry json.Const[string] `json:"home-country" json.value:"Canada"` // <----------
HomeProvince json.Const[string] `json:"home-province" json.value:"Manitoba"` // <----------
HomeCity string `json:"home-city"`
}
func (Const[T]) DecodeFromString ¶
See the Constantizer interface documentation for details.
func (Const[T]) JSONConst ¶
func (Const[T]) JSONConst()
See the Constantizer interface documentation for details on why this exists.
type ConstMismatchError ¶
ConstMismatchError is returned when a JSON value does not match the expected Const[T] value declared in the json.value struct tag.
func (ConstMismatchError) Error ¶
func (e ConstMismatchError) Error() string
type Constantizer ¶
type Constantizer interface {
// JSONConst is a dummy-method used (along with the method DecodeFromString) to specify that something is of inteface type json.Constantizer.
//
// An implementation of it will look similar to this:
//
// func (receiver MyType) JSONConst() {
// // nothing here
// }
JSONConst()
// DecodeFromString decodes the value of a string to the underlying type.
//
// The value will come from the value of the struct tag: "json.value".
//
//
// For example:
//
// type MyType struct {
//
// // ...
//
// MyField json.Const[uint64] `json:"myfield" json.value:"123"`
//
// // ...
//
// }
DecodeFromString(string) (any, error)
}
type Emptier ¶
type Emptier interface {
IsEmpty() bool
}
Emptier works with the `omitempty` struct-tag, recognized by Marshal and Usher.Marshal.
For example, a custom type might look like:
type MyType struct {
// ...
}
func (receiver MyType) IsEmpty() bool {
// ...
}
And it might be used similar to the following:
var MyStruct struct {
Apple string `json:"apple,omitempty"`
Banana MyType `json:"banana,omitempty"` // <---------
Cherry int `json:"cherry"`
}
// ...
var value MyStruct // = ...
bytes, err := json.Marshal(value)
An alternative to Emptier is Nothinger, which is more commonly used with optional-types (i.e., optiona-types).
type ErrorEmpty ¶
type ErrorEmpty interface {
error
ErrorEmpty()
}
ErrorEmpty is a special type of error.
ErrorEmpty is used with the "omitempty" struct-field tag option.
If a (custom) type's MarshalJSON() function returns an error of type ErrorEmpty, and a field of that type has the "omitempty" struct-field tag option then, that field will be omitted in the marshaled-JSON.
You can, of course, create your own (custom) error type that fits this ErrorEmpty interface, or you can use the ErrEmpty to create that ErrorEmpty error for you.
type ModifierFunc ¶
ModifierFunc is the type of a modifer.
Modifers as used to modify the the a marshaled struct-field.
type Nothinger ¶
type Nothinger interface {
IsNothing() bool
}
Nothinger is similar to Emptier, but is more commonly used with optional-types (i.e., optiona-types).
I.e., an optional-type has "something" in it or "nothing" in it. (Some optional-types call this "some" and "none".) A optional-type might have a method IsNothing()bool to communicate whether it has "nothing" or "something" in it.
If the optional-type has such a IsNothing()bool method, then Marshal and Usher.Marshal make use of that method for the purposes of `omitempty`.
If you type in not an optional-type, then it should probably instead implement Emptier.
type Number ¶
type Number struct {
// contains filtered or unexported fields
}
Number represents a JSON number. It stores it as a string to avoid precision loss that comes from using float64.
See also: NormalizeNumberString.
func MustParseNumberString ¶
func ParseNumberString ¶
ParseNumberString returns a Number with the value of a JSON number. The second return value is false if jsonNumber is not a valid JSON number.
func (Number) Float64 ¶
Float64 returns the number as a float64. The second return value is false if the number cannot be represented as a float64.
func (Number) Int64 ¶
Int64 returns the number as an int64. The second return value is false if the number cannot be represented as an int64.
func (Number) MarshalJSON ¶
MarshalJSON returns the JSON encoding of the number.
func (Number) String ¶
String returns the string representation of the number.
String makes Number fit the fmt.Stringer interface.
func (Number) Uint64 ¶
Uint64 returns the number as a uint64. The second return value is false if the number cannot be represented as a uint64.
func (*Number) UnmarshalJSON ¶
UnmarshalJSON sets the number from a JSON number literal. Returns an error if data is not a valid JSON number.
UnmarshalJSON makes Number fit the Unmarshaler interface.
type OmitAlways ¶
type OmitAlways interface {
JSONOmitAlways()
}
A field in a struct and a value in a map that fits this interface (by having the method JSONOmitAlways()) will always be omitted from the result JSON.
type UnknownFieldError ¶
UnknownFieldError is returned in obstructed mode when a JSON key does not match any struct field.
func (UnknownFieldError) Error ¶
func (e UnknownFieldError) Error() string
type UnmarshalErrors ¶
type UnmarshalErrors struct {
Errors []error
}
UnmarshalErrors collects multiple non-fatal errors from a single Unmarshal operation (e.g., multiple Const[T] mismatches).
func (UnmarshalErrors) Error ¶
func (e UnmarshalErrors) Error() string
func (UnmarshalErrors) Unwrap ¶
func (e UnmarshalErrors) Unwrap() []error
type Unmarshaler ¶
Unmarshaler is something that can unmarshal itself from JSON.
type Usher ¶
type Usher struct {
// contains filtered or unexported fields
}
Usher marshals a Go type into JSON.
If you want the "string" modifier you will need to call json.Usher.ImplantModifier() to add it:
var jsonUsher json.Usher
jsonUsher.ImplantModifier("string", json.StringModifierFunc, json.BareModifierFunc)
(Note that BareModifierFunc is also included because it is the inverse-operation to StringModifierFunc.)
You can also add your own modifiers:
var jsonUsher json.Usher
jsonUsher.ImplantModifier("digest", digestFunc, nil)
var ( // DefaultUsher is the Usher that the json.Marshal() function uses. // // Implanting a modifier into DefaultUsher into DefaultUsher modifies how the json.Marshal() behaves. DefaultUsher Usher )
func (*Usher) ImplantModifier ¶
func (receiver *Usher) ImplantModifier(name string, marshalFn ModifierFunc, unmarshalFn ModifierFunc)
func (*Usher) Marshal ¶
Marshal returns the JSON version of 'value'.
omitempty
For Go structs, if a field in the struct includes the struct-tag `omitempty`, then — Marshal will NOT include its in the resulting JSON if its Go value is empty.
For example, consider:
type MyStruct struct {
Once string
Twice string `json:"twice,omitempty"` // <---------
Thrice string `json:"thrice"`
Fource string `json:",omitempty"` // <---------
}
Note that field `Twice` and field `Fource` both have `omitempty` in their struct-tags. So, if their values are empty, then the resulting JSON will omit them.
For example, this:
var value MyStruct
Would (conceptually) result in:
{
"Once": "",
"thrice": ""
}
And, for example, this:
var value = MyStruct{
Once: "",
Twice: "",
Thrice: "",
Fource: ""
}
Would also (conceptually) result in:
{
"Once": "",
"thrice": ""
}
And also, for example, this:
var value = MyStruct{
Once: "first",
Twice: "second",
Thrice: "third",
Fource: "fourth"
}
Would (conceptually) result in:
{
"Once": "first",
"twice": "second",
"thrice": "third",
"Fource": "forth"
}
Custom types can also make use of Emptier or Nothinger to specify when they are empty. For example:
type MyStruct struct {
// ...
}
func (receiver MyStruct) IsEmpty() bool {
// ...
}
Marshal will call IsEmpty, if a custom type has it, to check whether the custom type is `empty` or not, for the purposes of `omitempty`.
func (*Usher) MergeAndMarshal ¶
func (*Usher) ObstructedUnmarshal ¶
ObstructedUnmarshal deserializes JSON data strictly. Returns an error if unknown JSON fields are encountered.
Source Files
¶
- baremodifierfunc.go
- compact.go
- const.go
- defaultusher.go
- emptier.go
- errempty.go
- errors.go
- jsonpath.go
- marshal.go
- marshalbool.go
- marshaler.go
- marshalfloat32.go
- marshalfloat64.go
- marshalint.go
- marshalint16.go
- marshalint32.go
- marshalint64.go
- marshalint8.go
- marshalslice.go
- marshalstring.go
- marshaltextmarshaler.go
- marshaluint.go
- marshaluint16.go
- marshaluint32.go
- marshaluint64.go
- marshaluint8.go
- mergeandmarshal.go
- modifierfunc.go
- modifierpair.go
- normalizenumber.go
- nothinger.go
- number.go
- omitalways.go
- parsetag.go
- scanner.go
- stringmodifierfunc.go
- structfields.go
- trim.go
- unmarshal.go
- unmarshalbool.go
- unmarshaler.go
- unmarshalerrors.go
- unmarshalint.go
- unmarshalmode.go
- unmarshalstring.go
- unmarshaluint.go
- unquote.go
- usher.go
- usher_marshal.go
- usher_marshalmap.go
- usher_marshalstruct.go
- usher_mergeandmarshal.go
- usher_unmarshal.go
- usher_unmarshalmap.go
- usher_unmarshalslice.go
- usher_unmarshalstruct.go