Documentation
¶
Overview ¶
Package json provides a Loader type that can be used in conjunction with the parent config package to create a config.Loader to load values from JSON encoded objects.
Example ¶
input := `{ "string": "foo bar", "bool": false, "int": 12345678, "float": 1234.5678, "nested1": { "nested2": { "nested3": "..." } }, "nope": null }` inputReader := strings.NewReader(input) loader := config.NewReaderFuncLoader( (&Loader{}).LoadReader, inputReader, ) c := config.New() _, err := c.MergeLoaders(loader) if err != nil { fmt.Println(err) return } fmt.Println(c.GetStringOk("string")) fmt.Println(c.GetBoolOk("bool")) fmt.Println(c.GetInt64Ok("int")) fmt.Println(c.GetFloat64Ok("float")) fmt.Println(c.GetStringOk("nested1.nested2.nested3")) fmt.Println(c.GetOk("nope")) fmt.Println() fmt.Println(c.GetInt64Ok("bool")) fmt.Println(c.GetOk("not present"))
Output: foo bar true false true 12345678 true 1234.5678 true ... true <nil> true 0 false <nil> false
Example (KeyPartTransform) ¶
input := `{ "foo": { "bar": "value" } }` inputReader := strings.NewReader(input) jsonLoader := &Loader{ KeyPartTransform: strings.ToUpper, } loader := config.NewReaderFuncLoader( jsonLoader.LoadReader, inputReader, ) c := config.New() _, err := c.MergeLoaders(loader) if err != nil { fmt.Println(err) return } fmt.Println(c.GetStringOk("FOO.BAR")) _, ok := c.GetOk("foo.bar") fmt.Println(ok)
Output: value true false
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Loader ¶
type Loader struct { //KeyPrefix is a Key that all Keys found in the JSON must start with in order //to be included in the resulting config.Values. //Notice that an empty KeyPrefix means all Keys are matched. KeyPrefix config.Key //KeySuffix is a Key that all Keys found in the JSON must end with in order //to be included in the resulting config.Values. //Notice that an empty KeySuffix means all Keys are matched. KeySuffix config.Key //DiscardNull tells Loader whether or not to include JSON nulls as nil //in the resulting Values. //The zero value means all nulls are indeed included. DiscardNull bool //NumberAsString tells Loader whether to insert JSON numbers as their //string representations or to parse them into float64 or int64 representations //via encoding/json.Number. //The zero value means all numbers will be parsed into float64 and int64 types. // //If NumberAsString is false, when parsing a JSON number the following logic //is used. //If encoding/json.Number.Int64() returns an error, then Float64()'s result //is inserted. //If their is no error, then the int64 value is inserted. NumberAsString bool //KeyPartTransform is an optional function that is called (if not nil) //on each individual key part found in the JSON. The returned response from //this function is then used to create the resulting Key. // //See the examples for use of this function. KeyPartTransform func(string) string }
Loader is a collection of settings that can be used with config.NewReaderFuncLoader() in order to create a config.Loader that parses JSON objects. Loader itself is not a config.Loader. The empty valued Loader has sane defaults where all key, values found within the JSON are included in the resulting Values, JSON nulls are inserted at nil, and JSON numbers are converted to float64 and int64 equivalents. See the individual fields for overriding this behaviour. See the package examples for use with the config package.
func (*Loader) LoadBytes ¶
LoadBytes uses l's settings and returns the parsed Values and possible error from decoding in. It is sugar for l.LoadReader(bytes.NewReader(in)).
func (*Loader) LoadReader ¶
LoadReader uses l's settings and a encoding/json.Decoder to parse Values from in. If the call to encoding/json.Decoder.Decode() returns an error, then that error is returned with nil *Values. in must represent a JSON encoded object. Any other type will error.
Notice that LoadReader is a config.ReaderFuncLoader and it is used in this manner in the examples.