Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encoding/json: Wrong marshal output on embeded structure with the custom marshaller #10939

Closed
fzerorubigd opened this issue May 23, 2015 · 1 comment

Comments

Projects
None yet
3 participants
@fzerorubigd
Copy link

commented May 23, 2015

Having an embeded struct with custom marshalJSON function, make the json.Marshal to use that marshal function in the base structure.

package main

import (
    "encoding/json"
    "fmt"
)


type h0 struct {
    M []string
}

func (a h0) MarshalJSON() ([]byte, error) {
    return json.Marshal(a.M)
}

type h1 struct {
    h0 `json:"hedaer"`
    A string
    B string
}

func main() {

    v := h1{}
    v.A = "A" 
    v.B = "B"
    v.M = append(v.M, "1", "2", "3")


    a, err := json.Marshal(v)
    if err != nil {
        panic(err)
    }

    fmt.Println(string(a))
}

Output must be :

{"hedaer":["1","2","3"],"A":"A","B":"B"}

But is :

["1","2","3"]

in playground : https://play.golang.org/p/619v_hL5hh

the reason is obvious, since the function invoked on the outer structure, but I think json library must ignore this kind of marshaller functions.
I test this against the latest version of go (1.4.2) in linux, and also the git version of go .
Any other information is required?

@minux minux closed this May 23, 2015

@minux

This comment has been minimized.

Copy link
Member

commented May 23, 2015

@golang golang locked and limited conversation to collaborators Jun 25, 2016

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.