Converting Toml format to Json Format in Golang
I would just use some popular toml library to parse toml to a struct and then use the standard library to marhshal it to JSON.
The below code makes use of https://github.com/BurntSushi/toml.
Note, no error handling for brevity.
type Config struct {
Age int `json:"age,omitempty"`
Cats []string `json:"cats,omitempty"`
Pi float64 `json:"pi,omitempty"`
Perfection []int `json:"perfection,omitempty"`
DOB time.Time `json:"dob,omitempty"`
}
var tomlData = `
Age = 25
Cats = [ "Cauchy", "Plato" ]
Pi = 3.14
Perfection = [ 6, 28, 496, 8128 ]
DOB = 1987-07-05T05:45:00Z`
func main() {
var conf Config
toml.Decode(tomlData, &conf)
j, _ := json.MarshalIndent(conf, "", " ")
fmt.Println(string(j))
}
Output:
{
"age": 25,
"cats": [
"Cauchy",
"Plato"
],
"pi": 3.14,
"perfection": [
6,
28,
496,
8128
],
"dob": "1987-07-05T05:45:00Z"
}
https://play.golang.com/p/91JqFjkJIXI