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]) All() iter.Seq2[string, V]
- 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]) 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]) Set(key string, value V) *Object[V]
- func (o *Object[V]) ToUnorderedMap() map[string]V
- func (o *Object[V]) UnmarshalJSON(data []byte) error
- func (o *Object[V]) Values() []V
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrDuplicateKey is returned when JSON or entry input contains a repeated key. ErrDuplicateKey = errors.New("duplicate 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 FromEntries ¶ added in v0.2.15
FromEntries returns an Object containing entries in their current 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.
Example ¶
package main
import (
"encoding/json"
"fmt"
"github.com/kaptinlin/orderedobject"
)
func main() {
obj, err := orderedobject.FromJSON[any]([]byte(`{"second":2,"first":1}`))
if err != nil {
fmt.Println(err)
return
}
data, err := json.Marshal(obj)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(data))
}
Output: {"second":2,"first":1}
func FromSortedMap ¶ added in v0.2.15
FromSortedMap returns an Object containing entries from m in lexical key order.
func New ¶ added in v0.2.15
New returns an empty Object.
Example ¶
package main
import (
"encoding/json"
"fmt"
"github.com/kaptinlin/orderedobject"
)
func main() {
person := orderedobject.New[any]().
Set("name", "Alice").
Set("age", 30).
Set("city", "New York")
data, err := json.Marshal(person)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(data))
}
Output: {"name":"Alice","age":30,"city":"New York"}
func (*Object[V]) All ¶ added in v0.2.15
All returns an iterator over entries in insertion order. Mutating o during iteration is unsupported; use Entries for a snapshot.
Example ¶
package main
import (
"fmt"
"github.com/kaptinlin/orderedobject"
)
func main() {
obj := orderedobject.New[int]().Set("first", 1).Set("second", 2)
for key, value := range obj.All() {
fmt.Printf("%s=%d\n", key, value)
break
}
}
Output: first=1
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]) Set ¶
Set stores value under key and returns o. If key already exists, Set updates its value without changing its position.
func (*Object[V]) ToUnorderedMap ¶ added in v0.2.15
ToUnorderedMap returns a new map containing o's entries. The returned map does not preserve insertion order.
Example ¶
package main
import (
"fmt"
"github.com/kaptinlin/orderedobject"
)
func main() {
obj := orderedobject.New[int]().Set("a", 1).Set("b", 2)
plain := obj.ToUnorderedMap()
fmt.Println(plain["a"], len(plain))
}
Output: 1 2
func (*Object[V]) UnmarshalJSON ¶
UnmarshalJSON decodes a JSON object into o.