Documentation
¶
Overview ¶
Package orderedobject provides an ordered JSON object that preserves insertion order of keys, designed to work with go-json-experiment/json.
Index ¶
- Variables
- type Entry
- type Object
- func (o *Object[V]) Clone() *Object[V]
- func (o *Object[V]) Delete(key string) *Object[V]
- func (o *Object[V]) Entries() []Entry[V]
- func (o *Object[V]) ForEach(fn func(key string, value V))
- func (o *Object[V]) Get(key string) (V, bool)
- func (o *Object[V]) Has(key string) bool
- func (o *Object[V]) Keys() []string
- func (o *Object[V]) Len() int
- func (o *Object[V]) MarshalJSON() ([]byte, error)
- func (o *Object[V]) MarshalJSONTo(enc *jsontext.Encoder) error
- func (o *Object[V]) Set(key string, value V) *Object[V]
- func (o *Object[V]) ToJSON() ([]byte, error)
- func (o *Object[V]) ToMap() map[string]V
- func (o *Object[V]) UnmarshalJSON(data []byte) error
- func (o *Object[V]) UnmarshalJSONFrom(dec *jsontext.Decoder) error
- func (o *Object[V]) Values() []V
- type OrderedMarshaler
Constants ¶
This section is empty.
Variables ¶
var ( // ErrExpectedObjectStart is returned when the JSON token is not '{'. ErrExpectedObjectStart = errors.New("expected object start") // ErrExpectedStringKey is returned when the JSON token is not a string key. ErrExpectedStringKey = errors.New("expected string key") )
Functions ¶
This section is empty.
Types ¶
type Object ¶
type Object[V any] struct { // contains filtered or unexported fields }
Object is an ordered JSON object that preserves insertion order.
func FromJSON ¶
FromJSON parses JSON data into an ordered object. Key order is preserved as it appears in the JSON input. Returns an error if data is not valid JSON or not an object.
func FromMap ¶
FromMap creates an ordered object from a map. Key order is determined by Go's map iteration order, which is randomized.
func (*Object[V]) Delete ¶
Delete removes the key-value pair associated with key from the ordered object. If key does not exist, this is a no-op. Returns the object to enable method chaining.
func (*Object[V]) Get ¶
Get returns the value associated with key and a boolean indicating whether the key was found. If the key does not exist, returns the zero value for V and false.
func (*Object[V]) Len ¶ added in v0.2.4
Len returns the number of key-value pairs in the ordered object.
func (*Object[V]) MarshalJSON ¶
MarshalJSON encodes the ordered object as JSON.
func (*Object[V]) MarshalJSONTo ¶
MarshalJSONTo encodes the ordered object to a JSON encoder.
func (*Object[V]) Set ¶
Set associates value with key in the ordered object. If key already exists, its value is updated in place without changing position. If key is new, the key-value pair is appended to the end. Returns the object to enable method chaining.
func (*Object[V]) ToMap ¶
ToMap converts the ordered object to a standard Go map. The returned map does not preserve insertion order.
func (*Object[V]) UnmarshalJSON ¶
UnmarshalJSON decodes a JSON object into the ordered object.
func (*Object[V]) UnmarshalJSONFrom ¶
UnmarshalJSONFrom decodes a JSON object from a decoder into the ordered object.
type OrderedMarshaler ¶ added in v0.1.2
OrderedMarshaler is an interface for types that marshal themselves to JSON while preserving key order.