config: github.com/gookit/config/json Index | Examples | Files

package json

import "github.com/gookit/config/json"

Package json use the https://github.com/json-iterator/go for parse json

Code:

config.WithOptions(config.ParseEnv)

// add Decoder and Encoder
config.AddDriver(Driver)

err := config.LoadFiles("testdata/json_base.json")
if err != nil {
    panic(err)
}

fmt.Printf("config data: \n %#v\n", config.Data())

err = config.LoadFiles("testdata/json_other.json")
// config.LoadFiles("testdata/json_base.json", "testdata/json_other.json")
if err != nil {
    panic(err)
}

fmt.Printf("config data: \n %#v\n", config.Data())
fmt.Print("get config example:\n")

name := config.String("name")
fmt.Printf("get string\n - val: %v\n", name)

arr1 := config.Strings("arr1")
fmt.Printf("get array\n - val: %#v\n", arr1)

val0 := config.String("arr1.0")
fmt.Printf("get sub-value by path 'arr.index'\n - val: %#v\n", val0)

map1 := config.StringMap("map1")
fmt.Printf("get map\n - val: %#v\n", map1)

val0 = config.String("map1.key")
fmt.Printf("get sub-value by path 'map.key'\n - val: %#v\n", val0)

// can parse env name(ParseEnv: true)
fmt.Printf("get env 'envKey' val: %s\n", config.String("envKey", ""))
fmt.Printf("get env 'envKey1' val: %s\n", config.String("envKey1", ""))

// set value
_ = config.Set("name", "new name")
name = config.String("name")
fmt.Printf("set string\n - val: %v\n", name)

// if you want export config data
// buf := new(bytes.Buffer)
// _, err = config.DumpTo(buf, config.JSON)
// if err != nil {
// 	panic(err)
// }
// fmt.Printf("export config:\n%s", buf.String())

Index

Examples

Package Files

json.go

Variables

var Decoder config.Decoder = func(data []byte, v interface{}) (err error) {
    if config.JSONAllowComments {
        str := jsonutil.StripComments(string(data))
        return parser.Unmarshal([]byte(str), v)
    }

    return parser.Unmarshal(data, v)
}

Decoder for json

var Driver = &jsonDriver{config.JSON}

Driver for json

var Encoder config.Encoder = parser.Marshal

Encoder for json

Package json imports 3 packages (graph). Updated 2019-08-16. Refresh now. Tools for package owners.