title: Idiomatic way to embed struct with custom MarshalJSON() method
body: <p>Given the following structs:</p>
  <pre><code>type Person struct {
      Name string `json:&quot;name&quot;`
  }
  
  type Employee struct {
      *Person
      JobRole string `json:&quot;jobRole&quot;`
  }
  </code></pre>
  <p>I can easily marshal an Employee to JSON as expected:</p>
  <pre><code>p := Person{&quot;Bob&quot;}
  e := Employee{&amp;p, &quot;Sales&quot;}
  output, _ := json.Marshal(e)
  fmt.Printf(&quot;%s\n&quot;, string(output))
  </code></pre>
  <p>Output:</p>
  <blockquote>
  <p>{&quot;name&quot;:&quot;Bob&quot;,&quot;jobRole&quot;:&quot;Sales&quot;}</p>
  </blockquote>
  <p>But when the embedded struct has a custom <code>MarshalJSON()</code> method...</p>
  <pre><code>func (p *Person) MarshalJSON() ([]byte,error) {
      return json.Marshal(struct{
          Name string `json:&quot;name&quot;`
      }{
          Name: strings.ToUpper(p.Name),
      })
  }
  </code></pre>
  <p>it breaks entirely:</p>
  <pre><code>p := Person{&quot;Bob&quot;}
  e := Employee{&amp;p, &quot;Sales&quot;}
  output, _ := json.Marshal(e)
  fmt.Printf(&quot;%s\n&quot;, string(output))
  </code></pre>
  <p>Now results in:</p>
  <blockquote>
  <p>{&quot;name&quot;:&quot;BOB&quot;}</p>
  </blockquote>
  <p>(Note the conspicuous lack of <code>jobRole</code> field)</p>
  <p>This is easily anticipated... the embedded <code>Person</code> struct implements the <code>MarshalJSON()</code> function, which is being called.</p>
  <p>The trouble is, it's not what I <em>want</em>. What I want would be:</p>
  <blockquote>
  <p>{&quot;name&quot;:&quot;BOB&quot;,&quot;jobRole&quot;:&quot;Sales&quot;}</p>
  </blockquote>
  <p>That is, encode <code>Employee</code>'s fields normally, and defer to <code>Person</code>'s <code>MarshalJSON()</code> method to marshal its fields, and hand back some tidy JSON.</p>
  <p>Now I could add a <code>MarshalJSON()</code> method to <code>Employee</code> as well, but this requires that I know that the embedded type implements <code>MarshalJSON()</code> as well, and either (a) duplicate its logic, or (b) call <code>Person</code>'s <code>MarshalJSON()</code> and somehow manipulate its output to fit where I want it. Either approach seems sloppy, and not very future proof (what if an embedded type I don't control some day adds a custom <code>MarshalJSON()</code> method?)</p>
  <p>Are there any alternatives here that I haven't considered?</p>