2021-01-01 09:18:47 -05:00
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
2021-10-21 18:30:35 -04:00
|
|
|
"bytes"
|
2021-01-01 09:18:47 -05:00
|
|
|
"encoding/json"
|
2021-10-21 18:30:35 -04:00
|
|
|
"io"
|
2021-01-01 09:18:47 -05:00
|
|
|
|
|
|
|
"github.com/pelletier/go-toml"
|
|
|
|
)
|
|
|
|
|
|
|
|
// FromTOML convert toml to json
|
|
|
|
func FromTOML(v []byte) ([]byte, error) {
|
2021-10-21 18:30:35 -04:00
|
|
|
tomlReader := bytes.NewReader(v)
|
|
|
|
jsonStr, err := jsonFromTomlReader(tomlReader)
|
|
|
|
if err != nil {
|
2021-01-01 09:18:47 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-10-21 18:30:35 -04:00
|
|
|
return []byte(jsonStr), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func jsonFromTomlReader(r io.Reader) (string, error) {
|
|
|
|
tree, err := toml.LoadReader(r)
|
2021-01-01 09:18:47 -05:00
|
|
|
if err != nil {
|
2021-10-21 18:30:35 -04:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return mapToJSON(tree)
|
|
|
|
}
|
|
|
|
|
|
|
|
func mapToJSON(tree *toml.Tree) (string, error) {
|
|
|
|
treeMap := tree.ToMap()
|
|
|
|
bytes, err := json.MarshalIndent(treeMap, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2021-01-01 09:18:47 -05:00
|
|
|
}
|
2021-11-27 04:16:41 -05:00
|
|
|
return string(bytes), nil
|
2021-01-01 09:18:47 -05:00
|
|
|
}
|