Documentation
¶
Overview ¶
Package jsonmpa provides map for Json/Struct/Key-Value-Database.
Example (SetAndGet) ¶
package main
import (
"fmt"
"github.com/chai2010/jsonmap"
)
func main() {
var jsonMap = jsonmap.JsonMap{
"a": map[string]interface{}{
"b": map[string]interface{}{
"c": "value-abc",
},
},
}
jsonMap.SetValue("value-xyz", "x", "y", "z")
v1, ok1 := jsonMap.GetValue("a", "b", "c")
fmt.Println(v1, ok1)
v2, ok2 := jsonMap.GetValue("x", "y", "z")
fmt.Println(v2, ok2)
v3, ok3 := jsonMap.GetValue("1", "2", "3")
fmt.Println(v3, ok3)
// replace x/y/z with x/y
jsonMap.SetValue("value-xy", "x", "y")
v4, ok4 := jsonMap.GetValue("x", "y", "z")
fmt.Println(v4, ok4)
v5, ok5 := jsonMap.GetValue("x", "y")
fmt.Println(v5, ok5)
}
Output: value-abc true value-xyz true <nil> false <nil> false value-xy true
Example (StructToMapString) ¶
package main
import (
"fmt"
"sort"
"github.com/chai2010/jsonmap"
)
func main() {
// https://github.com/chai2010/diffbot-go-client/blob/master/article.go
type Image struct {
Url string `json:"url"`
PixelHeight int `json:"pixelHeight"`
PixelWidth int `json:"pixelWidth"`
}
type Article struct {
Url string `json:"url"`
Meta map[string]interface{} `json:"meta,omitempty"` // Returned with fields.
Tags string `json:"tags,omitempty"` // Returned with fields.
Image Image `json:"image"`
}
article := Article{
Url: "https://github.com/chai2010",
Meta: map[string]interface{}{
"c": 123,
"d": 3.14,
"e": true,
},
Tags: "aa,bb",
Image: Image{
Url: "a.png",
PixelHeight: 100,
PixelWidth: 200,
},
}
var jsonMap = jsonmap.NewJsonMapFromStruct(article)
m := jsonMap.ToMapString("/")
var keys []string
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
fmt.Println("/article"+k, m[k])
}
}
Output: /article/image/pixelHeight 100 /article/image/pixelWidth 200 /article/image/url a.png /article/meta/c 123 /article/meta/d 3.14 /article/meta/e true /article/tags aa,bb /article/url https://github.com/chai2010
Example (ToMapString) ¶
package main
import (
"fmt"
"sort"
"github.com/chai2010/jsonmap"
)
func main() {
var jsonMap = jsonmap.JsonMap{
"a": map[string]interface{}{
"sub-a": "value-sub-a",
},
"b": map[string]interface{}{
"sub-b": "value-sub-b",
},
"c": 123,
"d": 3.14,
"e": true,
"x": map[string]interface{}{
"a": map[string]interface{}{
"sub-a": "value-sub-a",
},
"b": map[string]interface{}{
"sub-b": "value-sub-b",
},
"c": 123,
"d": 3.14,
"e": true,
"z": map[string]interface{}{
"zz": "value-zz",
},
},
}
m := jsonMap.ToMapString("/")
var keys []string
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
fmt.Println(k, m[k])
}
}
Output: /a/sub-a value-sub-a /b/sub-b value-sub-b /c 123 /d 3.14 /e true /x/a/sub-a value-sub-a /x/b/sub-b value-sub-b /x/c 123 /x/d 3.14 /x/e true /x/z/zz value-zz
Index ¶
- type JsonMap
- func (m JsonMap) DelValue(key string, subKeys ...string)
- func (m JsonMap) DelValues(keys ...[]string)
- func (m JsonMap) GetValue(key string, subKeys ...string) (value interface{}, ok bool)
- func (m JsonMap) Keys(keySep string) []string
- func (m JsonMap) SetValue(value interface{}, key string, subKeys ...string)
- func (m JsonMap) SetValuesFromKV(values map[string]interface{}, keySep string)
- func (m JsonMap) SetValuesFromStruct(v interface{})
- func (m JsonMap) ToFlatMap(keySep string) map[string]interface{}
- func (m JsonMap) ToMapString(keySep string) map[string]string
- func (m JsonMap) ToStruct(output interface{}) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type JsonMap ¶
type JsonMap map[string]interface{}
func NewJsonMap ¶
func NewJsonMap() JsonMap
func NewJsonMapFromKV ¶
func NewJsonMapFromStruct ¶
func NewJsonMapFromStruct(v interface{}) JsonMap
func (JsonMap) SetValuesFromKV ¶
func (JsonMap) SetValuesFromStruct ¶
func (m JsonMap) SetValuesFromStruct(v interface{})
Click to show internal directories.
Click to hide internal directories.