Yaml parses json, too; no need to try both.

pull/6/head
Daniel Smith 2014-06-12 17:11:02 -07:00
parent 2abfd95d6b
commit 867daadbdb
1 changed files with 1 additions and 23 deletions

View File

@ -24,29 +24,7 @@ func ToWireFormat(data []byte, storage string) ([]byte, error) {
return nil, fmt.Errorf("unknown storage type: %v", storage)
}
// Try parsing json
json_out, json_err := tryJSON(data, reflect.New(prototypeType).Interface())
if json_err == nil {
return json_out, json_err
}
// Try parsing yaml
yaml_out, yaml_err := tryYAML(data, reflect.New(prototypeType).Interface())
if yaml_err != nil {
return nil, fmt.Errorf("Could not parse input as json (error: %v) or yaml (error: %v", json_err, yaml_err)
}
return yaml_out, yaml_err
}
func tryJSON(data []byte, obj interface{}) ([]byte, error) {
err := json.Unmarshal(data, obj)
if err != nil {
return nil, err
}
return json.Marshal(obj)
}
func tryYAML(data []byte, obj interface{}) ([]byte, error) {
obj := reflect.New(prototypeType).Interface()
err := yaml.Unmarshal(data, obj)
if err != nil {
return nil, err