Expose a ToJSON and runtime.YAMLDecoder helper

Enables clients to optionally handle YAML
This commit is contained in:
Clayton Coleman
2015-03-16 23:43:31 -04:00
parent 022c103699
commit 71abc99dbe
2 changed files with 57 additions and 2 deletions

View File

@@ -27,6 +27,17 @@ import (
"github.com/golang/glog"
)
// ToJSON converts a single YAML document into a JSON document
// or returns an error. If the document appears to be JSON the
// YAML decoding path is not used (so that error messages are)
// JSON specific.
func ToJSON(data []byte) ([]byte, error) {
if hasJSONPrefix(data) {
return data, nil
}
return yaml.YAMLToJSON(data)
}
// YAMLToJSONDecoder decodes YAML documents from an io.Reader by
// separating individual documents. It first converts the YAML
// body to JSON, then unmarshals the JSON.
@@ -143,11 +154,19 @@ func (d *YAMLOrJSONDecoder) Decode(into interface{}) error {
func guessJSONStream(r io.Reader, size int) (io.Reader, bool) {
buffer := bufio.NewReaderSize(r, size)
b, _ := buffer.Peek(size)
return buffer, hasPrefix(b, []byte("{"))
return buffer, hasJSONPrefix(b)
}
var jsonPrefix = []byte("{")
// hasJSONPrefix returns true if the provided buffer appears to start with
// a JSON open brace.
func hasJSONPrefix(buf []byte) bool {
return hasPrefix(buf, jsonPrefix)
}
// Return true if the first non-whitespace bytes in buf is
// prefix
// prefix.
func hasPrefix(buf []byte, prefix []byte) bool {
trim := bytes.TrimLeftFunc(buf, unicode.IsSpace)
return bytes.HasPrefix(trim, prefix)