omap

package
Version: v0.0.0-...-dc1123b Latest Latest
Warning

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

Go to latest
Published: May 21, 2018 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type OrderedMap

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

An OrderedMap keeps track of the order keys are placed in it

Rather than be recursive, the values are of type json.RawMessage and thus can be decoded separately

func NewOrderedMap

func NewOrderedMap() *OrderedMap

NewOrderedMap returns a new OrderedMap

func (*OrderedMap) Get

func (o *OrderedMap) Get(k string) json.RawMessage

Get returns the json value associated with a key

Example
jsonData := `{
		"z": 1,
		"a": 2
	}`

m := NewOrderedMap()
err := json.Unmarshal([]byte(jsonData), &m)
if err != nil {
	fmt.Println("error:", err)
}
var val int
err = json.Unmarshal(m.Get("z"), &val)
if err != nil {
	fmt.Println("error:", err)
}

fmt.Println(val)
Output:

1

func (*OrderedMap) MarshalJSON

func (o *OrderedMap) MarshalJSON() ([]byte, error)

func (*OrderedMap) Set

func (o *OrderedMap) Set(k string, v json.RawMessage)

Set sets the value of a key

If the key already exists in the map it will retain its position otherwise it will be placed at the end

Example
jsonData := `{
		"z": 1,
		"a": 2
	}`

m := NewOrderedMap()
err := json.Unmarshal([]byte(jsonData), &m)
if err != nil {
	fmt.Println("error:", err)
}
newVal := 3
newData, err := json.Marshal(&newVal)
if err != nil {
	fmt.Println("error:", err)
}
m.Set("z", newData)
data, err := json.Marshal(&m)
if err != nil {
	fmt.Println("error:", err)
}
fmt.Println(string(data))
Output:

{"z":3,"a":2}

func (*OrderedMap) UnmarshalJSON

func (o *OrderedMap) UnmarshalJSON(b []byte) error

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL