jsonmap

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2018 License: BSD-3-Clause Imports: 6 Imported by: 7

README

Map for Json/Struct/Key-Value-Database

Build Status Go Report Card GoDoc

Install

  1. go get github.com/chai2010/jsonmap
  2. go run hello.go

Example

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
}

BUGS

Report bugs to chaishushan@gmail.com.

Thanks!

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

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 NewJsonMapFromKV(values map[string]interface{}, keySep string) JsonMap

func NewJsonMapFromStruct

func NewJsonMapFromStruct(v interface{}) JsonMap

func (JsonMap) DelValue

func (m JsonMap) DelValue(key string, subKeys ...string)

func (JsonMap) DelValues

func (m JsonMap) DelValues(keys ...[]string)

func (JsonMap) GetValue

func (m JsonMap) GetValue(key string, subKeys ...string) (value interface{}, ok bool)

func (JsonMap) Keys

func (m JsonMap) Keys(keySep string) []string

func (JsonMap) SetValue

func (m JsonMap) SetValue(value interface{}, key string, subKeys ...string)

func (JsonMap) SetValuesFromKV

func (m JsonMap) SetValuesFromKV(values map[string]interface{}, keySep string)

func (JsonMap) SetValuesFromStruct

func (m JsonMap) SetValuesFromStruct(v interface{})

func (JsonMap) ToFlatMap

func (m JsonMap) ToFlatMap(keySep string) map[string]interface{}

func (JsonMap) ToMapString

func (m JsonMap) ToMapString(keySep string) map[string]string

func (JsonMap) ToStruct

func (m JsonMap) ToStruct(output interface{}) error

Jump to

Keyboard shortcuts

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