Documentation
¶
Overview ¶
Package orderedobject provides an ordered JSON object that preserves key insertion order during JSON marshaling and unmarshaling.
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
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrExpectedObjectStart is returned when the next JSON token is not '{'. ErrExpectedObjectStart = errors.New("expected object start") // ErrExpectedStringKey is returned when the next JSON token is not a string key. ErrExpectedStringKey = errors.New("expected string key") )
Functions ¶
This section is empty.
Types ¶
type Entry ¶
type Entry[V any] struct { // Key is the object member name. Key string // Value is the value associated with Key. Value V }
Entry holds one key-value pair in an Object.
type Object ¶
type Object[V any] struct { // contains filtered or unexported fields }
Object stores key-value pairs in insertion order.
func FromJSON ¶
FromJSON decodes data into an Object, preserving key order from the input. FromJSON returns an error if data is not valid JSON or does not encode an object.
func FromMap ¶
FromMap returns an Object containing the entries in m. The resulting entry order matches Go's randomized map iteration order.
func (*Object[V]) Delete ¶
Delete removes key and returns o. Delete is a no-op if key is not present.
func (*Object[V]) Get ¶
Get returns the value for key and whether key is present. If key is not present, Get returns the zero value of V and false.
func (*Object[V]) MarshalJSON ¶
MarshalJSON returns the JSON encoding of o.
func (*Object[V]) MarshalJSONTo ¶
MarshalJSONTo writes the JSON encoding of o to enc. Nested map values are encoded with deterministic key order.
func (*Object[V]) Set ¶
Set stores value under key and returns o. If key already exists, Set updates its value without changing its position.
func (*Object[V]) ToJSON ¶
ToJSON returns the JSON encoding of o.
Example ¶
package main
import (
"fmt"
"github.com/kaptinlin/orderedobject"
)
func main() {
person := orderedobject.NewObject[any]().
Set("name", "Alice").
Set("age", 30).
Set("city", "New York")
data, err := person.ToJSON()
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(data))
}
Output: {"name":"Alice","age":30,"city":"New York"}
func (*Object[V]) ToMap ¶
ToMap returns a new map containing o's entries. The returned map does not preserve insertion order.
func (*Object[V]) UnmarshalJSON ¶
UnmarshalJSON decodes a JSON object into o.
func (*Object[V]) UnmarshalJSONFrom ¶
UnmarshalJSONFrom decodes a JSON object from dec into o. UnmarshalJSONFrom replaces any existing entries in o.
type OrderedMarshaler ¶ added in v0.1.2
type OrderedMarshaler interface {
// MarshalJSONTo writes the JSON encoding of the value to enc.
MarshalJSONTo(enc *jsontext.Encoder) error
}
OrderedMarshaler marshals a value to JSON while preserving key order.