2020-09-23 16:39:41 +02:00
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2023-08-18 11:27:47 +08:00
|
|
|
|
|
|
|
"github.com/spf13/viper/internal/encoding/codec"
|
2020-09-23 16:39:41 +02:00
|
|
|
)
|
|
|
|
|
2023-08-18 11:27:47 +08:00
|
|
|
// Codec implements the encoding.Codec interface for JSON encoding.
|
2020-09-23 16:39:41 +02:00
|
|
|
type Codec struct{}
|
|
|
|
|
2023-08-18 11:27:47 +08:00
|
|
|
func New(_ ...interface{}) codec.Codec {
|
|
|
|
return &Codec{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*Codec) Encode(v map[string]interface{}) ([]byte, error) {
|
2020-09-23 16:39:41 +02:00
|
|
|
// TODO: expose prefix and indent in the Codec as setting?
|
|
|
|
return json.MarshalIndent(v, "", " ")
|
|
|
|
}
|
|
|
|
|
2023-08-18 11:27:47 +08:00
|
|
|
func (*Codec) Decode(b []byte, v map[string]interface{}) error {
|
2021-07-15 23:47:20 +02:00
|
|
|
return json.Unmarshal(b, &v)
|
2020-09-23 16:39:41 +02:00
|
|
|
}
|