mirror of https://github.com/dnomd343/ClearDNS
dnomd343
2 years ago
3 changed files with 73 additions and 0 deletions
@ -0,0 +1,8 @@ |
|||
module toJSON |
|||
|
|||
go 1.18 |
|||
|
|||
require ( |
|||
github.com/BurntSushi/toml v1.2.0 |
|||
gopkg.in/yaml.v3 v3.0.1 |
|||
) |
@ -0,0 +1,6 @@ |
|||
github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0= |
|||
github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= |
|||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= |
|||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= |
|||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= |
|||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
@ -0,0 +1,59 @@ |
|||
package main |
|||
|
|||
import ( |
|||
"encoding/json" |
|||
"fmt" |
|||
"github.com/BurntSushi/toml" |
|||
"gopkg.in/yaml.v3" |
|||
) |
|||
|
|||
var jsonTest = ` |
|||
{ |
|||
"field1": "test", |
|||
"field2": { |
|||
"field3": "value", |
|||
"field4": [ |
|||
42, |
|||
1024 |
|||
] |
|||
} |
|||
} |
|||
` |
|||
|
|||
var yamlTest = ` |
|||
field1: test |
|||
field2: |
|||
field3: value |
|||
field4: [42, 1024] |
|||
` |
|||
|
|||
var tomlTest = ` |
|||
field1 = "test" |
|||
|
|||
[field2] |
|||
field3 = "value" |
|||
field4 = [ 42, 1_024 ] |
|||
` |
|||
|
|||
func parser(raw string) (interface{}, interface{}) { |
|||
var object interface{} |
|||
if err := json.Unmarshal([]byte(raw), &object); err == nil { // try json
|
|||
return object, nil // json format
|
|||
} |
|||
if err := toml.Unmarshal([]byte(raw), &object); err == nil { // try toml
|
|||
return object, nil // toml format
|
|||
} |
|||
if err := yaml.Unmarshal([]byte(raw), &object); err == nil { // try yaml
|
|||
return object, nil // yaml format
|
|||
} |
|||
return nil, nil // parser error
|
|||
} |
|||
|
|||
func main() { |
|||
//if object, err := parser(jsonTest); err == nil {
|
|||
//if object, err := parser(yamlTest); err == nil {
|
|||
if object, err := parser(tomlTest); err == nil { |
|||
b, _ := json.Marshal(object) |
|||
fmt.Println(string(b)) |
|||
} |
|||
} |
Loading…
Reference in new issue