mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-22 01:57:12 -05:00
dce8764fd7
* Lint: replace golint with revive * Lint: fix lint
37 lines
664 B
Go
37 lines
664 B
Go
package json
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"io"
|
|
|
|
"github.com/pelletier/go-toml"
|
|
)
|
|
|
|
// FromTOML convert toml to json
|
|
func FromTOML(v []byte) ([]byte, error) {
|
|
tomlReader := bytes.NewReader(v)
|
|
jsonStr, err := jsonFromTomlReader(tomlReader)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return []byte(jsonStr), nil
|
|
}
|
|
|
|
func jsonFromTomlReader(r io.Reader) (string, error) {
|
|
tree, err := toml.LoadReader(r)
|
|
if err != nil {
|
|
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
|
|
}
|
|
return string(bytes), nil
|
|
}
|