orderedmapjson

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: May 12, 2025 License: MIT Imports: 5 Imported by: 0

README

orderedmapjson

orderedmapjson is a package that extends the orderedmap package by adding support for JSON marshalling and unmarshalling. Keys are required to be strings, as per the JSON specification.

Installation

go get github.com/GitRowin/orderedmapjson

Usage

The library provides three types: TypedOrderedMap, AnyOrderedMap and AnyOrderedMapSlice.

TypedOrderedMap

TypedOrderedMap allows you to specify the value type of the map.

m := orderedmapjson.NewTypedOrderedMap[int]()

m.Set("q", 1)
m.Set("w", 2)
m.Set("e", 3)
m.Set("r", 4)
m.Set("t", 5)
m.Set("y", 6)

b, err := json.Marshal(m)

if err != nil {
    panic(err)
}

fmt.Println(string(b)) // Output: {"q":1,"w":2,"e":3,"r":4,"t":5,"y":6}

AnyOrderedMap

AnyOrderedMap is similar to TypedOrderedMap[any], with one key difference: it unmarshals JSON objects at any level as AnyOrderedMap[any] rather than map[string]any. This ensures that the order of nested objects is preserved too.

const input = `{"foo":"bar","obj":{"2":2,"3":"3","1":1},"baz":[1, "2", 3, null]}`

var m *orderedmapjson.AnyOrderedMap
if err := json.Unmarshal([]byte(input), &m); err != nil {
    panic(err)
}

if obj, ok := m.Get("obj"); ok {
    fmt.Printf("%T %v\n", obj, obj) // Output: *orderedmapjson.AnyOrderedMap {2:2,3:3,1:1}
}

if baz, ok := m.Get("baz"); ok {
    fmt.Println(baz.([]any)[2]) // Output: 3
}

AnyOrderedMapSlice

For unmarshalling JSON arrays. Like AnyOrderedMap, it unmarshals JSON objects at any level as AnyOrderedMap[any] rather than map[string]any.

const input = `["foo",1,{"3":"3","1":"1","2":"2"}]`

var values AnyOrderedMapSlice
if err := json.Unmarshal([]byte(input), &values); err != nil {
    panic(err)
}

fmt.Println(values[2]) // Output: {3:3,1:1,2:2}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AnyOrderedMap

type AnyOrderedMap struct {
	// contains filtered or unexported fields
}

func NewAnyOrderedMap

func NewAnyOrderedMap() *AnyOrderedMap

func NewAnyOrderedMapWithCapacity added in v0.2.0

func NewAnyOrderedMapWithCapacity(capacity int) *AnyOrderedMap

func (*AnyOrderedMap) Copy added in v0.2.0

func (m *AnyOrderedMap) Copy() *AnyOrderedMap

func (AnyOrderedMap) MarshalJSON

func (m AnyOrderedMap) MarshalJSON() ([]byte, error)

func (AnyOrderedMap) SetEscapeHTML

func (m AnyOrderedMap) SetEscapeHTML(on bool)

func (AnyOrderedMap) String

func (m AnyOrderedMap) String() string

func (*AnyOrderedMap) UnmarshalJSON

func (m *AnyOrderedMap) UnmarshalJSON(b []byte) error

type AnyOrderedMapSlice added in v0.5.0

type AnyOrderedMapSlice []any

func (*AnyOrderedMapSlice) UnmarshalJSON added in v0.5.0

func (s *AnyOrderedMapSlice) UnmarshalJSON(b []byte) error

type TypedOrderedMap

type TypedOrderedMap[V any] struct {
	// contains filtered or unexported fields
}

func NewTypedOrderedMap

func NewTypedOrderedMap[V any]() *TypedOrderedMap[V]

func NewTypedOrderedMapWithCapacity added in v0.2.0

func NewTypedOrderedMapWithCapacity[V any](capacity int) *TypedOrderedMap[V]

func (*TypedOrderedMap[V]) Copy added in v0.2.0

func (m *TypedOrderedMap[V]) Copy() *TypedOrderedMap[V]

func (TypedOrderedMap) MarshalJSON

func (m TypedOrderedMap) MarshalJSON() ([]byte, error)

func (TypedOrderedMap) SetEscapeHTML

func (m TypedOrderedMap) SetEscapeHTML(on bool)

func (TypedOrderedMap) String

func (m TypedOrderedMap) String() string

func (*TypedOrderedMap[V]) UnmarshalJSON

func (m *TypedOrderedMap[V]) UnmarshalJSON(b []byte) error

Jump to

Keyboard shortcuts

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