Documentation
¶
Index ¶
- 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 MarshalInt(value int) []byte
- func MarshalInt16(value int16) []byte
- func MarshalInt32(value int32) []byte
- func MarshalInt64(value int64) []byte
- func MarshalInt8(value int8) []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 MarshalUint16(value uint16) []byte
- func MarshalUint32(value uint32) []byte
- func MarshalUint64(value uint64) []byte
- func MarshalUint8(value uint8) []byte
- func MergeAndMarshal(values ...any) ([]byte, error)
- func StringModifierFunc(bytes []byte) ([]byte, error)
- func Unmarshal(data []byte, dst any) error
- func UnmarshalString(data []byte, dst *string) error
- type Const
- type Constantizer
- type Emptier
- type ErrorEmpty
- type Marshaler
- type ModifierFunc
- type Nothinger
- type OmitAlways
- type Unmarshaler
- type Usher
Constants ¶
This section is empty.
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 MarshalInt ¶
MarshalInt returns the JSON version of a Go int.
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 MarshalInt8 ¶
MarshalInt8 returns the JSON version of a Go int8.
func MarshalSlice ¶
MarshalString returns the JSON version of a Go string.
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 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 MarshalUint8 ¶
MarshalUint8 returns the JSON version of a Go uint8.
func MergeAndMarshal ¶
Marshal return the JSON version of 'value'.
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 UnmarshalString ¶
MarshalString returns the JSON version of a Go string.
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 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 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 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)
You can also add your own modifiers:
var jsonUsher json.Usher jsonUsher.ImplantModifier("digest", digestFunc)
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, fn 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`.
Source Files
¶
- baremodifierfunc.go
- compact.go
- const.go
- defaultusher.go
- emptier.go
- errempty.go
- errors.go
- marshal.go
- marshalbool.go
- marshaler.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
- nothinger.go
- omitalways.go
- parsetag.go
- stringmodifierfunc.go
- unmarshal.go
- unmarshaler.go
- unmarshalstring.go
- usher.go
- usher_marshal.go
- usher_marshalmap.go
- usher_marshalstruct.go
- usher_mergeandmarshal.go