orderedjson

package
v0.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 5, 2026 License: MIT Imports: 8 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsObject

func IsObject(data []byte) bool

IsObject reports whether data is a JSON object.

func Map

func Map(value any) (map[string]any, bool)

Map returns the members of a JSON object value, whether it is held as a map or as raw JSON.

func Marshal

func Marshal(value any) ([]byte, error)

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

func MarshalArray(items []any, first ...string) ([]byte, error)

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

func MarshalMap(members map[string]any, first ...string) ([]byte, error)

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

func UnmarshalFields(data []byte, v any, members ...string) error

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

func Value(raw []byte) any

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 New

func New() *Object

New returns an empty object.

func Parse

func Parse(data []byte) (*Object, error)

Parse decodes data as a JSON object. It returns an error for any other JSON value, including null.

func (*Object) Clone

func (o *Object) Clone() *Object

Clone returns a copy that shares no key storage.

func (*Object) Delete

func (o *Object) Delete(key string)

Delete removes key.

func (*Object) Get

func (o *Object) Get(key string) (json.RawMessage, bool)

Get returns the raw value of key.

func (*Object) Has

func (o *Object) Has(key string) bool

Has reports whether key is present, even with a null value.

func (*Object) Keys

func (o *Object) Keys() []string

Keys returns the keys in order.

func (*Object) Len

func (o *Object) Len() int

Len is the number of keys.

func (*Object) MarshalJSON

func (o *Object) MarshalJSON() ([]byte, error)

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) SetValue

func (o *Object) SetValue(key string, value any) error

SetValue marshals value and sets it under key.

func (*Object) UnmarshalJSON

func (o *Object) UnmarshalJSON(data []byte) error

UnmarshalJSON reads an object and keeps its key order. A repeated key keeps its first position and its last value, as JSON.parse does.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL