{"modulePath":"github.com/go-openapi/jsonpointer","version":"v1.0.3","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package jsonpointer\n\nPackage jsonpointer provides a golang implementation for json pointers.\n\n## Constants\n\n```go\nconst (\n\t// ErrPointer is a sentinel error raised by all errors from this package.\n\tErrPointer pointerError = \"JSON pointer error\"\n\n\t// ErrInvalidStart states that a JSON pointer must start with a separator (\"/\").\n\tErrInvalidStart pointerError = `JSON pointer must be empty or start with a \"` + pointerSeparator + `\"`\n\n\t// ErrUnsupportedValueType indicates that a value of the wrong type is being set.\n\tErrUnsupportedValueType pointerError = \"only structs, pointers, maps and slices are supported for setting values\"\n\n\t// ErrDashToken indicates use of the RFC 6901 \"-\" reference token in a context where it cannot be\n\t// resolved.\n\t//\n\t// Per RFC 6901 §4 the \"-\" token refers to the (nonexistent) element after the last array element.\n\t// It may only be used as the terminal token of a [Pointer.Set] against a slice, where it means\n\t// \"append\".\n\t//\n\t// Any other use (get, offset, intermediate traversal, non-slice target) is an error condition that\n\t// wraps this sentinel.\n\tErrDashToken pointerError = `the \"-\" array token cannot be resolved here` //nolint:gosec // G101 false positive: this is a JSON Pointer reference token, not a credential.\n)\n```\n\n## Functions\n\n```go\nfunc Escape(token string) string\n```\nEscape escapes a pointer reference token string.\n\nThe JSONPointer specification defines \"/\" as a separator and \"~\" as an escape prefix.\n\nKeys containing such characters are escaped with the following rules:\n\n  - \"~\" is escaped as \"~0\"\n  - \"/\" is escaped as \"~1\"\n\n```go\nfunc GetForToken(document any, decodedToken string, opts ...Option) (any, reflect.Kind, error)\n```\nGetForToken gets a value for a json pointer token 1 level deep.\n\n```go\nfunc SetDefaultNameProvider(provider NameProvider)\n```\nSetDefaultNameProvider sets the [NameProvider](#NameProvider) as a package-level default.\n\nBy default, the default provider is [jsonname.DefaultJSONNameProvider](/github.com/go-openapi/jsonpointer/jsonname#DefaultJSONNameProvider).\n\nIt is safe to call concurrently with [Pointer.Get](#Pointer.Get), [Pointer.Set](#Pointer.Set), [GetForToken](#GetForToken) and [SetForToken](#SetForToken). The typical usage is to call it once at initialization time.\n\nA nil provider is ignored.\n\n```go\nfunc SetForToken(document any, decodedToken string, value any, opts ...Option) (any, error)\n```\nSetForToken sets a value for a json pointer token 1 level deep.\n\nSee [Pointer.Set](#Pointer.Set) for the mutation contract, in particular the handling of the RFC 6901 \"-\" token on slices.\n\n```go\nfunc Unescape(token string) string\n```\nUnescape unescapes a json pointer reference token string to the original representation.\n\n```go\nfunc UseGoNameProvider()\n```\nUseGoNameProvider sets the [NameProvider](#NameProvider) as a package-level default to the alternative provider [jsonname.GoNameProvider](/github.com/go-openapi/jsonpointer/jsonname#GoNameProvider), that covers a few areas not supported by the default name provider.\n\nThis implementation supports untagged exported fields and embedded types in go struct. It follows strictly the behavior of the JSON standard library regarding field naming conventions.\n\nIt is safe to call concurrently with [Pointer.Get](#Pointer.Get), [Pointer.Set](#Pointer.Set), [GetForToken](#GetForToken) and [SetForToken](#SetForToken). The typical usage is to call it once at initialization time.\n\n## Types\n\n```go\ntype JSONPointable interface {\n\t// JSONLookup returns a value pointed at this (unescaped) key.\n\tJSONLookup(key string) (any, error)\n}\n```\nJSONPointable is an interface for structs to implement, when they need to customize the json pointer process or want to avoid the use of reflection.\n\n```go\ntype JSONSetable interface {\n\t// JSONSet sets the value pointed at the (unescaped) key.\n\t//\n\t// The key may be the RFC 6901 \"-\" token when the pointer targets a slice-like member; see the\n\t// interface documentation for details.\n\tJSONSet(key string, value any) error\n}\n```\nJSONSetable is an interface for structs to implement, when they need to customize the json pointer process or want to avoid the use of reflection.\n\n### Handling of the RFC 6901 \"-\" token {#hdr-Handling_of_the_RFC_6901_____token}\n\nWhen a type implementing JSONSetable is the terminal parent of a [Pointer.Set](#Pointer.Set) call, the library passes the raw reference token to JSONSet without interpretation.\n\nIn particular, the RFC 6901 \"-\" token (which conventionally means \"append\" for arrays, per RFC 6902) is forwarded verbatim as the key argument.\n\nImplementations that model an array-like container are expected to give \"-\" the append semantics; implementations that do not should return an error wrapping [ErrDashToken](#ErrDashToken) (or [ErrPointer](#ErrPointer)) for clarity.\n\nImplementations are responsible for any in-place mutation: the library does not attempt to rebind the result of JSONSet into a parent container.\n\n```go\ntype NameProvider interface {\n\t// GetGoName gets the go name for a json property name\n\tGetGoName(subject any, name string) (string, bool)\n\n\t// GetGoNameForType gets the go name for a given type for a json property name\n\tGetGoNameForType(tpe reflect.Type, name string) (string, bool)\n}\n```\nNameProvider knows how to resolve go struct fields into json names.\n\nThe default provider is brought by [github.com/go-openapi/jsonpointer/jsonname.DefaultJSONNameProvider](/github.com/go-openapi/jsonpointer/jsonname#DefaultJSONNameProvider).\n\n```go\nfunc DefaultNameProvider() NameProvider\n```\nDefaultNameProvider returns the current package-level [NameProvider](#NameProvider).\n\n```go\ntype Option func(*options)\n```\nOption to tune the behavior of a JSON [Pointer](#Pointer).\n\n```go\nfunc WithNameProvider(provider NameProvider) Option\n```\nWithNameProvider injects a custom [NameProvider](#NameProvider) to resolve json names from go struct types.\n\n```go\ntype Pointer struct {\n\t// contains filtered or unexported fields\n}\n```\nPointer is a representation of a json pointer.\n\nUse [Pointer.Get](#Pointer.Get) to retrieve a value or [Pointer.Set](#Pointer.Set) to set a value.\n\nIt works with any go type interpreted as a JSON document, which means:\n\n  - if a type implements [JSONPointable](#JSONPointable), its [JSONPointable.JSONLookup](#JSONPointable.JSONLookup) method is used to resolve [Pointer.Get](#Pointer.Get)\n  - if a type implements [JSONSetable](#JSONSetable), its [JSONSetable.JSONSet](#JSONSetable.JSONSet) method is used to resolve [Pointer.Set](#Pointer.Set)\n  - a go map\\[K]V is interpreted as an object, with K a string type. A named string type is converted; a map keyed by anything else (e.g. map\\[int]V) cannot be addressed by a pointer\n  - a go slice \\[]T is interpreted as an array\n  - a go struct is interpreted as an object, with exported fields interpreted as keys\n  - promoted fields from an embedded struct are traversed, including through an embedded pointer to a struct. Resolving one on a value whose embedded pointer is nil reports an error\n  - scalars (e.g. int, float64 ...), channels, functions and go arrays cannot be traversed\n\nFor struct s resolved by reflection, key mappings honor the conventional struct tag \\`json\\`.\n\nFields that do not specify a \\`json\\` tag, or specify an empty one, or are tagged as \\`json:\"-\"\\` are ignored.\n\n### Limitations {#hdr-Limitations}\n\n  - Unlike go standard marshaling, untagged fields do not default to the go field name and are ignored.\n  - an anonymous field is walked for the fields it promotes, and its own \\`json\\` tag is ignored. An anonymous field that is not a struct (e.g. an embedded named slice) promotes nothing and is unreachable.\n\n```go\nfunc New(jsonPointerString string) (Pointer, error)\n```\nNew creates a new json pointer from its string representation.\n\n```go\nfunc (p *Pointer) DecodedTokens() []string\n```\nDecodedTokens returns the decoded (unescaped) tokens of this JSON pointer.\n\n```go\nfunc (p *Pointer) Get(document any, opts ...Option) (any, reflect.Kind, error)\n```\nGet uses the pointer to retrieve a value from a JSON document.\n\nIt returns the value with its type as a [reflect.Kind](/reflect#Kind) or an error.\n\n```go\nfunc (p *Pointer) IsEmpty() bool\n```\nIsEmpty returns true if this is an empty json pointer.\n\nThis indicates that it points to the root document.\n\n```go\nfunc (p *Pointer) Offset(document string) (int64, error)\n```\nOffset returns the byte offset, in the raw JSON text of document, of the location referenced by this pointer's terminal token.\n\nUnlike [Pointer.Get](#Pointer.Get) and [Pointer.Set](#Pointer.Set), which operate on a decoded Go value, Offset operates directly on the textual JSON source.\n\nIt drives an [encoding/json.Decoder](/encoding/json#Decoder) over the string and stops at the terminal token, returning the position at which the decoder was about to read that token.\n\nIt is primarily intended for tooling that needs to map a pointer back to a region of the original source: reporting line/column for validation or parse diagnostics, extracting a sub-document by slicing the raw bytes, or highlighting the referenced span in an editor.\n\n### Offset semantics {#hdr-Offset_semantics}\n\nThe meaning of the returned offset depends on whether the terminal token addresses an object property or an array element:\n\n  - Object property: the offset points to the first byte of the key (its opening quote character), not to the associated value. For example, pointer \"/foo/bar\" against {\"foo\": {\"bar\": 21}} returns 9, the index of the opening quote of \"bar\".\n  - Array element: the offset points to the first byte of the value at that index. For example, pointer \"/0/1\" against \\[\\[1,2], \\[3,4]] returns 4, the index of the digit 2.\n\n### Errors {#hdr-Errors}\n\nOffset returns an error in any of these cases:\n\n  - document is not syntactically valid JSON;\n  - the structure of document does not match the pointer (e.g. traversing into a scalar, or a token that is neither a valid key nor a valid numeric index);\n  - a referenced key or index does not exist in document;\n  - the pointer's terminal token is the RFC 6901 \"-\" array token, which designates a nonexistent element and therefore has no offset in the source. The returned error wraps [ErrDashToken](#ErrDashToken).\n\nAll errors wrap [ErrPointer](#ErrPointer).\n\n```go\nfunc (p *Pointer) Set(document any, value any, opts ...Option) (any, error)\n```\nSet uses the pointer to set a value from a data type that represent a JSON document.\n\n### Mutation contract {#hdr-Mutation_contract}\n\nSet mutates the provided document in place whenever Go's type system allows it: when document is a map, a pointer, or when the targeted value is reached through an addressable ancestor (e.g. a struct field traversed via a pointer, a slice element).\n\nCallers that rely on this in-place behavior may continue to ignore the returned document.\n\nThe returned document is only load-bearing when Set cannot mutate in place.\n\nThis happens in one specific case: appending to a top-level slice passed by value (e.g. document of type \\[]T rather than \\*\\[]T) via the RFC 6901 \"-\" terminal token. reflect.Append produces a new slice header that the library cannot rebind into the caller's variable; the updated document is returned instead.\n\nPass \\*\\[]T if you want in-place rebind for that case as well.\n\nSee [ErrDashToken](#ErrDashToken) for the semantics of the \"-\" token.\n\n### Setting a null value {#hdr-Setting_a_null_value}\n\nA nil value (what [encoding/json](/encoding/json) decodes a JSON null into) is set as the zero value of the target, provided the target can hold nil: an interface, pointer, map, slice, channel or func.\n\nSetting nil on a target that cannot represent it — a string or an int field, an element of a \\[]int — reports an error wrapping [ErrPointer](#ErrPointer) rather than substituting a zero value, which would erase the difference between a null and a 0.\n\nSetting a map member to nil keeps the member with a nil value. It does not delete it.\n\n```go\nfunc (p *Pointer) String() string\n```\nString representation of a pointer.\n\n","path":"github.com/go-openapi/jsonpointer","name":"jsonpointer","synopsis":"Package jsonpointer provides a golang implementation for json pointers.","isRedistributable":true}
