Yaml: MarshalYAML doesn't call MarshalYAML on returned value

Created on 16 Oct 2015  ·  7Comments  ·  Source: go-yaml/yaml

From reading the docs, I'd expect the following example to print "something",
but it prints "y": 99 instead.

package main

import (
    "fmt"
    "log"
    "gopkg.in/yaml.v2"
)

func main() {
    t := T{99}
    data, err := yaml.Marshal(t)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("%s\n", data)
}

type T struct {
    X int
}

func (t T) MarshalYAML() (interface{}, error) {
    return S{t.X}, nil
}

type S struct {
    Y int
}

func (s S) MarshalYAML() (interface{}, error) {
    return "something", nil
}

Most helpful comment

Why MarshalYAML method of Marshaler interface returns interface{} rather than []byte like in encoding/json ?

All 7 comments

Interested in offering a patch for this?

I am interested in getting this fixed too. +1

I'd love to do a patch but there are at least 10 things that I need to do more than this that I know no-one else is ever going to do... Perhaps on a flight sometime, unless I find this bug blocking some other task.

Why MarshalYAML method of Marshaler interface returns interface{} rather than []byte like in encoding/json ?

We ran into this. Is this something you'd consider a bug or just inaccurate documentation?

The expectation sounds reasonable, so tastes more like a bug than desired behavior.

I'm about to release v3 publicly, which is a good chance to get this fixed as it might break compatibility otherwise. I'll look into this.

This is fixed in v3. Will close the issue once v3 is announced.

Was this page helpful?
0 / 5 - 0 ratings