Documentation
¶
Overview ¶
Package jsonapi marshals and unmarshals JSON:API v1.1 formatted JSON. The mapping between JSON:API values and Go values is defined with struct tags, which can be overridden with custom marshalling and unmarshaling functions.
Index ¶
- func MarshalResource(a any, opts ...marshalResourceOpt) ([]byte, error)
- func UnmarshalResource(data []byte, a any) error
- func WithRelationshipLinker(links RelationshipLinker) marshalResourceOpt
- func WithResourceLinker(links ResourceLinker) marshalResourceOpt
- type FieldTypeError
- type IllegalUnmarshalError
- type Link
- type LinkObject
- type LinkUri
- type MarshalError
- type RelationshipLinker
- type ResourceIdentifier
- type ResourceLinker
- type ResourceMarshaler
- type ResourceTypeError
- type ResourceUnmarshaler
- type SelfReferentialPointerError
- type TagError
- type UnmarshalError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MarshalResource ¶
MashalResource returns the JSON:API encoding of resource a.
If a is nil or not a valid Resource type (ie is neither a struct nor a ResourceMarshaler) then a ResourceTypeError is returned.
If a implements ResourceMarshaler, then its [ResourceMarshaler.MarshalJsonApiResource] function is called.
Otherwise, the encoding of each of a's struct fields is defined by the the "jsonapi" key in the field's tag. The following formats are accepted:
- "id,<type-name>[,<opt1>[,<opt2>]]": field is encoded as the resource type and id.
- "attr[,<name>[,<opt1>[,opt2]]]": field is be encoded as an attribute.
- "rel,<name>,<type-name>[,<opt1>[,<opt2>]]": field is encoded as a related resource id.
- "link[,<name>[,<opt1>[,opt2]]]": field is encoded as a link.
- "meta[,<name>[,<opt1>[,opt2]]]": field is encoded as a metadata item.
- "-": field is ignored.
The tag's first element specifies the field's destination in the resource, and must be either "id", "attr", "rel", "meta", "link" or "-". The "id" specification must be followed by a type, a "rel" specification must be followed by a name and type, and the "attr", "meta" and "link" specifications may be followed by a name. A field with no tag defaults to attr, and an empty or unspecified name will default to the field name.
The following options are supported:
"omitempty" specifies that the field should be omitted from the Resource if the field has an empty value, as in the encoding/json package.
"string" signals that a floating point, integer, or boolean type should be encoded as a string. This is useful for using int or UUID fields as resource IDs.
Some struct tag examples:
// Field appears as the `id` field, converted to a string. // Additionally, a `type` field will be added with value `my-type`. Field int `jsonapi:"id,my-type,string"` // Field appears as an attribute with name `my-name`. Field int `jsonapi:"attr,my-name,omitempty"` // Field appears as an attribute with name `my-name`. Field int `json:"my-name"` // Field is excluded from the resource entirely: Field int `jsonapi:"-"` // Field appears as the "id" of a to-one relationship, with name `my-name`, and type `my-type`. Field int `jsonapi:"attr,my-name,my-type,string"` // Field elements appear as the "id" fields of a to-many relationship, with name `my-name`, and type `my-type`. Field []int `jsonapi:"attr,my-name,my-type,string"` // Field appears as an meta item with name `my-name`. Field int `jsonapi:"meta,my-name"` // Field appears as a link with name `my-name`. Field int `jsonapi:"link,my-name"`
Embedded struct fields without a jsonapi tag are marshaled as if their inner exported fields were fields in the outer struct, subject to the same visibility rules defined in the `encoding/json` package. Embedded struct fields with a tag are treated as though they are not embedded.
func UnmarshalResource ¶
UnmarshalResource parses the JSON:API-formatted resource data and stores the result in the value pointed to by a. If a is nil or not a pointer, an IllegalUnmarshalError is returned. If a does not point to a struct type or a ResourceUnmarshaler, a ResourceTypeError is returned.
If a implements ResourceUnmarshaler, then its [ResourceUnmarshaler.UnmarshalJsonApiResource] function is called.
Otherwise, a's struct fields are decoded using struct tag and embedding rules equivalent to those used by MarshalResource.
func WithRelationshipLinker ¶
func WithRelationshipLinker(links RelationshipLinker) marshalResourceOpt
WithRelationshipLinker returns a resource marshaling option that will set links on relationships to those returned by the the supplied RelationshipLinker function.
func WithResourceLinker ¶
func WithResourceLinker(links ResourceLinker) marshalResourceOpt
WithResourceLinker returns a resource marshaling option that will set resource links to those returned by the suppled ResourceLinker function.
Types ¶
type FieldTypeError ¶ added in v0.3.0
A FieldTypeError describes a Go struct field of a type that cannot be marshaled to, or unmarshaled from, JSON:API.
func (*FieldTypeError) Error ¶ added in v0.3.0
func (e *FieldTypeError) Error() string
type IllegalUnmarshalError ¶ added in v0.3.0
An IllegalUnmarshalError describes an illegal input to UnmarshalResource (ie, not a pointer, or nil).
func (*IllegalUnmarshalError) Error ¶ added in v0.3.0
func (e *IllegalUnmarshalError) Error() string
type Link ¶
type Link interface {
// contains filtered or unexported methods
}
The Link interface represents a JSON:API link, ie either a uri reference or an object. It has a single unexported method and so cannot be implemented by code outside of this package.
type LinkObject ¶
type LinkObject struct {
Href string `json:"href,omitempty"`
DescribedBy Link `json:"described_by,omitempty"`
Title string `json:"title,omitempty"`
Type string `json:"type,omitempty"`
HrefLang []string `json:"hreflang,omitempty"`
Meta map[string]any `json:"meta,omitempty"`
}
A LinkObject represents a JSON:API object link.
func (*LinkObject) UnmarshalJSON ¶
func (l *LinkObject) UnmarshalJSON(data []byte) error
type LinkUri ¶
type LinkUri struct {
Uri string
}
A LinkUri represents a JSON:API URI-reference link.
func (LinkUri) MarshalJSON ¶
func (*LinkUri) UnmarshalJSON ¶
type MarshalError ¶ added in v0.3.0
A MarshalError describes an error returned by the underlying JSON marshaling library.
func (*MarshalError) Error ¶ added in v0.3.0
func (e *MarshalError) Error() string
type RelationshipLinker ¶
type RelationshipLinker func(r any, id ResourceIdentifier, rel string, toOne bool, data ...ResourceIdentifier) (map[string]Link, error)
A RelationshipLinker function should return all links for the given relationship, on the given resource. Input r is the value passed to MarshalResource, id is the ResourceIdentifier extracted from r, rel is the relationship name, toOne indicates if the relationship is toOne or toMany, and data contains a ResourceIdentifier for each related resource. Can be used with WithRelationshipLinker to set links that are not present in the struct passed to MarshalResource.
Example:
func MyRelationshipLinker(r any, id ResourceIdentifier, rel string, toOne bool, data ...ResourceIdentifier) (map[string]Link, error) {
return map[string]Link{
"related": LinkUri{Uri: fmt.Sprintf("https://example.com/%s/%s/%s", id.Type, id.Id, rel)},
}, nil
}
type ResourceIdentifier ¶
A ResourceIdentifier represents a JSON:API resource identifier.
type ResourceLinker ¶
type ResourceLinker func(r any, id ResourceIdentifier) (map[string]Link, error)
A ResourceLinker function should return all links for the supplied resource. Input r is the value passed to MarshalResource, and id is the ResourceIdentifier extracted from r. Can be used with WithResourceLinker to set links that are not present in the struct passed to MarshalResource.
Example:
func MyResourceLinker(r any, id ResourceIdentifier) (map[string]Link, error) {
return map[string]Link{
"self": LinkUri{
Uri: fmt.Sprintf("https://example.com/%s/%s", id.Type, id.Id),
},
}, nil
}
type ResourceMarshaler ¶
ResourceMarshaler is the interface implemented by types that can marshal themselves into JSON:API-formatted JSON.
type ResourceTypeError ¶ added in v0.3.0
A ResourceTypeError describes a Go type that cannot be be marshaled to, or unmarshaled from, a JSON:API resource (ie, not a struct, or does not implement ResourceMarshaler or ResourceUnmarshaler).
func (*ResourceTypeError) Error ¶ added in v0.3.0
func (e *ResourceTypeError) Error() string
type ResourceUnmarshaler ¶
ResourceUnmarshaler is the interface implemented by types that can unmarshal themselves from JSON:API-formatted JSON.
type SelfReferentialPointerError ¶ added in v0.3.0
A SelfReferentialPointerError describes a loop of pointers.
func (*SelfReferentialPointerError) Error ¶ added in v0.3.0
func (e *SelfReferentialPointerError) Error() string
type TagError ¶ added in v0.3.0
A TagError describes an unknown or incorrectly formatted jsonapi struct tag.
type UnmarshalError ¶ added in v0.3.0
An UnmarshalError describes an error returned by the underlying JSON unmarshaling library.
func (*UnmarshalError) Error ¶ added in v0.3.0
func (e *UnmarshalError) Error() string