-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
42 lines (37 loc) · 890 Bytes
/
json.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package config
import (
"encoding/json"
"os"
)
// ParseJson reads a JSON configuration from the given string.
func ParseJson(c string) (*Config, error) {
return parseJson([]byte(c))
}
// ParseJsonFile reads a JSON configuration from the given filename.
func ParseJsonFile(filename string) (*Config, error) {
c, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
return parseJson(c)
}
// parseJson performs the real JSON parsing.
func parseJson(c []byte) (*Config, error) {
var out interface{}
var err error
if err = json.Unmarshal(c, &out); err != nil {
return nil, err
}
if out, err = normalizeValue(out); err != nil {
return nil, err
}
return &Config{Root: out}, nil
}
// RenderJson renders a JSON configuration.
func RenderJson(c interface{}) (string, error) {
b, err := json.Marshal(c)
if err != nil {
return "", err
}
return string(b), nil
}