Documentation
¶
Overview ¶
Package json provide functions for marshalling an unmarshalling types to JSON. These functions are meant to be utilized inside of structs that implement json.Unmarshaler and json.Marshaler interfaces. This package provides the additional functionality of writing fields that are not in the struct when marshalling to a field called AdditionalFields if that field exists and is a map[string]interface{}. When marshalling, if the struct has all the same prerequisites, it will uses the keys in AdditionalFields as extra fields. This package uses encoding/json underneath.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Marshal ¶
Marshal is used to marshal a type into its JSON representation. It wraps the stdlib calls in order to marshal a struct or *struct so that a field called "AdditionalFields" of type map[string]interface{} with "-" used inside struct tag `json:"-"` can be marshalled as if they were fields within the struct.
func MarshalRaw ¶
func MarshalRaw(i interface{}) json.RawMessage
MarshalRaw marshals i into a json.RawMessage. If I cannot be marshalled, this will panic. This is exposed to help test AdditionalField values which are stored as json.RawMessage.
func Unmarshal ¶
Unmarshal unmarshals a []byte representing JSON into i, which must be a *struct. In addition, if the struct has a field called AdditionalFields of type map[string]interface{}, JSON data representing fields not in the struct will be written as key/value pairs to AdditionalFields.
Any panic that escapes the underlying reflect-based decoder (for example "reflect: New of type that may not be allocated in heap") is recovered and returned as an error so that callers are not crashed by malformed or otherwise unexpected input.
IMPORTANT: when Unmarshal returns a non-nil error, the destination i may have been partially populated (the decoder writes fields sequentially and a panic mid-decode does not roll back earlier writes). Callers that need all-or-nothing semantics — particularly those handling untrusted input — must decode into a temporary value and only copy/assign it on success. MSAL's own token-cache callers already follow this pattern (see apps/internal/base/storage.Manager.Unmarshal).
Types ¶
This section is empty.