github.com/imannamdari/v2ray-core/v5@v5.0.5/infra/conf/json/toml.go (about)

     1  package json
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"io"
     7  
     8  	"github.com/pelletier/go-toml"
     9  )
    10  
    11  // FromTOML convert toml to json
    12  func FromTOML(v []byte) ([]byte, error) {
    13  	tomlReader := bytes.NewReader(v)
    14  	jsonStr, err := jsonFromTomlReader(tomlReader)
    15  	if err != nil {
    16  		return nil, err
    17  	}
    18  	return []byte(jsonStr), nil
    19  }
    20  
    21  func jsonFromTomlReader(r io.Reader) (string, error) {
    22  	tree, err := toml.LoadReader(r)
    23  	if err != nil {
    24  		return "", err
    25  	}
    26  	return mapToJSON(tree)
    27  }
    28  
    29  func mapToJSON(tree *toml.Tree) (string, error) {
    30  	treeMap := tree.ToMap()
    31  	bytes, err := json.MarshalIndent(treeMap, "", "  ")
    32  	if err != nil {
    33  		return "", err
    34  	}
    35  	return string(bytes), nil
    36  }