1
0
mirror of https://github.com/jinzhu/configor.git synced 2020-06-03 17:06:22 +00:00
configor/unmarshal_json_1_10.go
Reuben Pereira f78b6dc2e1 Add support for the ErrorOnUnmatchedKeys field for json files
* Also improve the tests for ErrorOnUnmatchedKeys with toml and yaml files
2018-05-01 09:14:03 +05:30

29 lines
637 B
Go

// +build go1.10
package configor
import (
"encoding/json"
"io"
"strings"
)
// unmarshalJSON unmarshals the given data into the config interface.
// If the errorOnUnmatchedKeys boolean is true, an error will be returned if there
// are keys in the data that do not match fields in the config interface.
func unmarshalJSON(data []byte, config interface{}, errorOnUnmatchedKeys bool) error {
reader := strings.NewReader(string(data))
decoder := json.NewDecoder(reader)
if errorOnUnmatchedKeys {
decoder.DisallowUnknownFields()
}
err := decoder.Decode(config)
if err != nil && err != io.EOF {
return err
}
return nil
}