Last active
August 29, 2015 13:57
-
-
Save ryanuber/9498296 to your computer and use it in GitHub Desktop.
goyaml parsing JSON text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"github.com/go-yaml/yaml" | |
) | |
var json_data = ` | |
{ | |
"node_name": "abc.cisco.com", | |
"tags": { | |
"role": "webserver", | |
"datacenter": "us-east" | |
} | |
} | |
` | |
var yaml_data = ` | |
node_name: abc.cisco.com | |
tags: | |
role: webserver | |
datacenter: us-east | |
` | |
type ParsedConfig struct{ | |
NodeName string `yaml:"node_name"` | |
Tags map[string]string `yaml:"tags"` | |
EventHandlers []string `yaml:"event_handlers"` | |
} | |
func main() { | |
var j, y ParsedConfig | |
err := yaml.Unmarshal([]byte(json_data), &j) | |
if err != nil { | |
log.Fatalf("error: %v", err) | |
} | |
err = yaml.Unmarshal([]byte(yaml_data), &y) | |
if err != nil { | |
log.Fatalf("error: %v", err) | |
} | |
if fmt.Sprintf("%#v", j) != fmt.Sprintf("%#v", y) { | |
log.Fatalf("not equivalent") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment