14 // Marshal marshals the object into JSON then converts JSON to YAML and returns the
16 func Marshal(o interface{}) ([]byte, error) {
17 j, err := json.Marshal(o)
19 return nil, fmt.Errorf("error marshaling into JSON: %v", err)
22 y, err := JSONToYAML(j)
24 return nil, fmt.Errorf("error converting JSON to YAML: %v", err)
30 // JSONOpt is a decoding option for decoding from JSON format.
31 type JSONOpt func(*json.Decoder) *json.Decoder
33 // Unmarshal converts YAML to JSON then uses JSON to unmarshal into an object,
34 // optionally configuring the behavior of the JSON unmarshal.
35 func Unmarshal(y []byte, o interface{}, opts ...JSONOpt) error {
36 return yamlUnmarshal(y, o, false, opts...)
39 // UnmarshalStrict strictly converts YAML to JSON then uses JSON to unmarshal
40 // into an object, optionally configuring the behavior of the JSON unmarshal.
41 func UnmarshalStrict(y []byte, o interface{}, opts ...JSONOpt) error {
42 return yamlUnmarshal(y, o, true, append(opts, DisallowUnknownFields)...)
45 // yamlUnmarshal unmarshals the given YAML byte stream into the given interface,
46 // optionally performing the unmarshalling strictly
47 func yamlUnmarshal(y []byte, o interface{}, strict bool, opts ...JSONOpt) error {
48 vo := reflect.ValueOf(o)
49 unmarshalFn := yaml.Unmarshal
51 unmarshalFn = yaml.UnmarshalStrict
53 j, err := yamlToJSON(y, &vo, unmarshalFn)
55 return fmt.Errorf("error converting YAML to JSON: %v", err)
58 err = jsonUnmarshal(bytes.NewReader(j), o, opts...)
60 return fmt.Errorf("error unmarshaling JSON: %v", err)
66 // jsonUnmarshal unmarshals the JSON byte stream from the given reader into the
67 // object, optionally applying decoder options prior to decoding. We are not
68 // using json.Unmarshal directly as we want the chance to pass in non-default
70 func jsonUnmarshal(r io.Reader, o interface{}, opts ...JSONOpt) error {
71 d := json.NewDecoder(r)
72 for _, opt := range opts {
75 if err := d.Decode(&o); err != nil {
76 return fmt.Errorf("while decoding JSON: %v", err)
81 // JSONToYAML Converts JSON to YAML.
82 func JSONToYAML(j []byte) ([]byte, error) {
83 // Convert the JSON to an object.
84 var jsonObj interface{}
85 // We are using yaml.Unmarshal here (instead of json.Unmarshal) because the
86 // Go JSON library doesn't try to pick the right number type (int, float,
87 // etc.) when unmarshalling to interface{}, it just picks float64
88 // universally. go-yaml does go through the effort of picking the right
89 // number type, so we can preserve number type throughout this process.
90 err := yaml.Unmarshal(j, &jsonObj)
95 // Marshal this object into YAML.
96 return yaml.Marshal(jsonObj)
99 // YAMLToJSON converts YAML to JSON. Since JSON is a subset of YAML,
100 // passing JSON through this method should be a no-op.
102 // Things YAML can do that are not supported by JSON:
103 // * In YAML you can have binary and null keys in your maps. These are invalid
104 // in JSON. (int and float keys are converted to strings.)
105 // * Binary data in YAML with the !!binary tag is not supported. If you want to
106 // use binary data with this library, encode the data as base64 as usual but do
107 // not use the !!binary tag in your YAML. This will ensure the original base64
108 // encoded data makes it all the way through to the JSON.
110 // For strict decoding of YAML, use YAMLToJSONStrict.
111 func YAMLToJSON(y []byte) ([]byte, error) {
112 return yamlToJSON(y, nil, yaml.Unmarshal)
115 // YAMLToJSONStrict is like YAMLToJSON but enables strict YAML decoding,
116 // returning an error on any duplicate field names.
117 func YAMLToJSONStrict(y []byte) ([]byte, error) {
118 return yamlToJSON(y, nil, yaml.UnmarshalStrict)
121 func yamlToJSON(y []byte, jsonTarget *reflect.Value, yamlUnmarshal func([]byte, interface{}) error) ([]byte, error) {
122 // Convert the YAML to an object.
123 var yamlObj interface{}
124 err := yamlUnmarshal(y, &yamlObj)
129 // YAML objects are not completely compatible with JSON objects (e.g. you
130 // can have non-string keys in YAML). So, convert the YAML-compatible object
131 // to a JSON-compatible object, failing with an error if irrecoverable
132 // incompatibilties happen along the way.
133 jsonObj, err := convertToJSONableObject(yamlObj, jsonTarget)
138 // Convert this object to JSON and return the data.
139 return json.Marshal(jsonObj)
142 func convertToJSONableObject(yamlObj interface{}, jsonTarget *reflect.Value) (interface{}, error) {
145 // Resolve jsonTarget to a concrete value (i.e. not a pointer or an
146 // interface). We pass decodingNull as false because we're not actually
147 // decoding into the value, we're just checking if the ultimate target is a
149 if jsonTarget != nil {
150 ju, tu, pv := indirect(*jsonTarget, false)
151 // We have a JSON or Text Umarshaler at this level, so we can't be trying
152 // to decode into a string.
153 if ju != nil || tu != nil {
160 // If yamlObj is a number or a boolean, check if jsonTarget is a string -
161 // if so, coerce. Else return normal.
162 // If yamlObj is a map or array, find the field that each key is
163 // unmarshaling to, and when you recurse pass the reflect.Value for that
164 // field back into this function.
165 switch typedYAMLObj := yamlObj.(type) {
166 case map[interface{}]interface{}:
167 // JSON does not support arbitrary keys in a map, so we must convert
168 // these keys to strings.
170 // From my reading of go-yaml v2 (specifically the resolve function),
171 // keys can only have the types string, int, int64, float64, binary
172 // (unsupported), or null (unsupported).
173 strMap := make(map[string]interface{})
174 for k, v := range typedYAMLObj {
175 // Resolve the key to a string first.
177 switch typedKey := k.(type) {
181 keyString = strconv.Itoa(typedKey)
183 // go-yaml will only return an int64 as a key if the system
184 // architecture is 32-bit and the key's value is between 32-bit
185 // and 64-bit. Otherwise the key type will simply be int.
186 keyString = strconv.FormatInt(typedKey, 10)
188 // Stolen from go-yaml to use the same conversion to string as
189 // the go-yaml library uses to convert float to string when
191 s := strconv.FormatFloat(typedKey, 'g', -1, 32)
208 return nil, fmt.Errorf("Unsupported map key of type: %s, key: %+#v, value: %+#v",
209 reflect.TypeOf(k), k, v)
212 // jsonTarget should be a struct or a map. If it's a struct, find
213 // the field it's going to map to and pass its reflect.Value. If
214 // it's a map, find the element type of the map and pass the
215 // reflect.Value created from that type. If it's neither, just pass
216 // nil - JSON conversion will error for us if it's a real issue.
217 if jsonTarget != nil {
219 if t.Kind() == reflect.Struct {
220 keyBytes := []byte(keyString)
221 // Find the field that the JSON library would use.
223 fields := cachedTypeFields(t.Type())
224 for i := range fields {
226 if bytes.Equal(ff.nameBytes, keyBytes) {
230 // Do case-insensitive comparison.
231 if f == nil && ff.equalFold(ff.nameBytes, keyBytes) {
236 // Find the reflect.Value of the most preferential
238 jtf := t.Field(f.index[0])
239 strMap[keyString], err = convertToJSONableObject(v, &jtf)
245 } else if t.Kind() == reflect.Map {
246 // Create a zero value of the map's element type to use as
248 jtv := reflect.Zero(t.Type().Elem())
249 strMap[keyString], err = convertToJSONableObject(v, &jtv)
256 strMap[keyString], err = convertToJSONableObject(v, nil)
263 // We need to recurse into arrays in case there are any
264 // map[interface{}]interface{}'s inside and to convert any
265 // numbers to strings.
267 // If jsonTarget is a slice (which it really should be), find the
268 // thing it's going to map to. If it's not a slice, just pass nil
269 // - JSON conversion will error for us if it's a real issue.
270 var jsonSliceElemValue *reflect.Value
271 if jsonTarget != nil {
273 if t.Kind() == reflect.Slice {
274 // By default slices point to nil, but we need a reflect.Value
275 // pointing to a value of the slice type, so we create one here.
276 ev := reflect.Indirect(reflect.New(t.Type().Elem()))
277 jsonSliceElemValue = &ev
281 // Make and use a new array.
282 arr := make([]interface{}, len(typedYAMLObj))
283 for i, v := range typedYAMLObj {
284 arr[i], err = convertToJSONableObject(v, jsonSliceElemValue)
291 // If the target type is a string and the YAML type is a number,
292 // convert the YAML type to a string.
293 if jsonTarget != nil && (*jsonTarget).Kind() == reflect.String {
294 // Based on my reading of go-yaml, it may return int, int64,
295 // float64, or uint64.
297 switch typedVal := typedYAMLObj.(type) {
299 s = strconv.FormatInt(int64(typedVal), 10)
301 s = strconv.FormatInt(typedVal, 10)
303 s = strconv.FormatFloat(typedVal, 'g', -1, 32)
305 s = strconv.FormatUint(typedVal, 10)
314 yamlObj = interface{}(s)