2023-03-28 22:48:58 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 22:48:58 +00:00
|
|
|
|
2019-12-06 16:14:56 +00:00
|
|
|
package lib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DecodeJSON is a convenience function to create a JSON decoder
|
|
|
|
// set it up to disallow unknown fields and then decode into the
|
|
|
|
// given value
|
|
|
|
func DecodeJSON(data io.Reader, out interface{}) error {
|
|
|
|
if data == nil {
|
|
|
|
return io.EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder := json.NewDecoder(data)
|
|
|
|
decoder.DisallowUnknownFields()
|
|
|
|
return decoder.Decode(&out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON is a convenience function around calling
|
|
|
|
// DecodeJSON. It will mainly be useful in many of our
|
|
|
|
// UnmarshalJSON methods for structs.
|
|
|
|
func UnmarshalJSON(data []byte, out interface{}) error {
|
|
|
|
return DecodeJSON(bytes.NewReader(data), out)
|
|
|
|
}
|