1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-21 01:27:03 -05:00
v2fly/infra/conf/json/toml.go
Shelikhoo f7ba3d0341
fix test break for TOML JSON convertion
Based on 352072d51a/cmd/tomljson/main.go (L56)

Commit 0f7345ba76 updated toml library version, however, the library have changed its check logic for input. This issue is now fixed.
2021-10-21 23:30:35 +01:00

37 lines
667 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
}