Documentation
¶
Overview ¶
Package orderedobject provides an ordered JSON object implementation designed to work with github.com/go-json-experiment/json.
Index ¶
- Variables
- type Entry
- type Object
- func (object *Object[V]) Clone() *Object[V]
- func (object *Object[V]) Delete(key string) *Object[V]
- func (object *Object[V]) Entries() []Entry[V]
- func (object *Object[V]) ForEach(fn func(key string, value V))
- func (object *Object[V]) Get(key string) (V, bool)
- func (object *Object[V]) Has(key string) bool
- func (object *Object[V]) Keys() []string
- func (object *Object[V]) Length() int
- func (object *Object[V]) MarshalJSON() ([]byte, error)
- func (object *Object[V]) MarshalJSONTo(enc *jsontext.Encoder) error
- func (object *Object[V]) Set(key string, value V) *Object[V]
- func (object *Object[V]) ToJSON() ([]byte, error)
- func (object *Object[V]) ToMap() map[string]V
- func (object *Object[V]) UnmarshalJSON(data []byte) error
- func (object *Object[V]) UnmarshalJSONFrom(dec *jsontext.Decoder) error
- func (object *Object[V]) Values() []V
- type OrderedMarshaler
Constants ¶
This section is empty.
Variables ¶
var ( // ErrExpectedObjectStart is returned when the JSON token is not an object start 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 a JSON string into an ordered object. The order of keys will be preserved as in the JSON string.
func FromMap ¶
FromMap creates an ordered object from a map. The order of the keys will be determined by the map iteration order.
func (*Object[V]) Delete ¶
Delete removes a key-value pair from the ordered object. If the key does not exist, it does nothing. Returns the object for chaining.
func (*Object[V]) ForEach ¶
ForEach executes a function for each key-value pair in the ordered object.
func (*Object[V]) Get ¶
Get returns the value for a key and whether the key exists. If the key does not exist, it returns the zero value and false.
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 sets the value for a key in the ordered object. If the key already exists, its value is updated. Otherwise, the key-value pair is appended to the end. Returns the object for chaining.
func (*Object[V]) ToJSON ¶
ToJSON converts the ordered object to a JSON byte slice. This is a convenience method that internally uses json.Marshal.
func (*Object[V]) ToMap ¶
ToMap converts the ordered object to a standard Go map. The returned map will not preserve the 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 objects that can marshal themselves to JSON while preserving key order.