Documentation
¶
Overview ¶
Package orderedjson holds JSON objects that keep their keys in the order they were read or set, the way a JavaScript object does. MCP schemas and tool arguments cross the client unchanged, so their key order must survive a merge such as `{...schema, type: "object"}`.
Index ¶
- func IsObject(data []byte) bool
- func Map(value any) (map[string]any, bool)
- func Marshal(value any) ([]byte, error)
- func MarshalArray(items []any, first ...string) ([]byte, error)
- func MarshalMap(members map[string]any, first ...string) ([]byte, error)
- func UnmarshalFields(data []byte, v any, members ...string) error
- func Value(raw []byte) any
- type Object
- func (o *Object) Clone() *Object
- func (o *Object) Delete(key string)
- func (o *Object) Get(key string) (json.RawMessage, bool)
- func (o *Object) Has(key string) bool
- func (o *Object) Keys() []string
- func (o *Object) Len() int
- func (o *Object) MarshalJSON() ([]byte, error)
- func (o *Object) Set(key string, value json.RawMessage)
- func (o *Object) SetValue(key string, value any) error
- func (o *Object) UnmarshalJSON(data []byte) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Map ¶
Map returns the members of a JSON object value, whether it is held as a map or as raw JSON.
func Marshal ¶
Marshal is json.Marshal without HTML escaping. A MarshalJSON method returns text the encoder that called it then writes as it is or HTML-escapes as its own setting says, so text a method produces must not be escaped already: Pi's JSON.stringify never escapes `<`, `>` or `&`.
func MarshalArray ¶
MarshalArray writes items as a JSON array in which each map item is written by MarshalMap with first; other items are written as encoding/json writes them. A nil slice is `[]`.
func MarshalMap ¶
MarshalMap writes a map's members with the keys of first in that order when present, then the rest sorted: the order a JavaScript object built member by member has when its creator is known.
func UnmarshalFields ¶
UnmarshalFields decodes data into v, a pointer to a struct, like encoding/json, then replaces each named member of an `any` field with Value of its raw JSON, so an object an extension supplied is not reordered by a Go map. A named member absent from data leaves its field as it is. v must not have an UnmarshalJSON method that calls UnmarshalFields: decode into a local alias type.
func Value ¶
Value and UnmarshalFields use the shared host/Go-SDK codec, which keeps an unmatched UTF-16 unit as the WTF-8 representation (extensions/sdk/json/README.md); a raw container keeps a lone surrogate as its lowercase escape, as JSON.stringify writes it.
Value decodes one JSON value a JavaScript object would hold. A container stays a json.RawMessage holding JSON.stringify(JSON.parse(raw)): the member order it was written in (integer-like keys first, ascending, as a JavaScript object orders them), and numbers and strings in the form JSON.stringify writes, whichever SDK encoded it (Python writes `1.0` and `\u00e9`, the Go SDK `\u003c`). A scalar decodes as encoding/json decodes it into an `any`. Absent, empty, null and invalid input is nil. The result does not alias raw.
Types ¶
type Object ¶
type Object struct {
// contains filtered or unexported fields
}
Object is a JSON object with insertion-ordered keys. Values stay raw, so numbers and nested objects keep their exact text.
func Parse ¶
Parse decodes data as a JSON object. It returns an error for any other JSON value, including null.
func (*Object) Get ¶
func (o *Object) Get(key string) (json.RawMessage, bool)
Get returns the raw value of key.
func (*Object) MarshalJSON ¶
MarshalJSON writes the keys in order.
func (*Object) Set ¶
func (o *Object) Set(key string, value json.RawMessage)
Set replaces the value of an existing key in place or appends a new key.
func (*Object) UnmarshalJSON ¶
UnmarshalJSON reads an object and keeps its key order. A repeated key keeps its first position and its last value, as JSON.parse does.