Json 为类型 json.RawMessage 调用 MarshalJSON 时出错

Json error calling MarshalJSON for type json.RawMessage

我在尝试编组此结构时遇到以下错误

json: error calling MarshalJSON for type json.RawMessage: unexpected end of JSON input

对于下面结构的对象

type Chart struct {
    ID          int             `json:"id,omitempty" db:"id"`
    Name        string          `json:"name,omitempty" db:"name"`
    Type        string          `json:"type,omitempty" db:"type"`
    DashboardID int             `json:"dashboard_id,omitempty"`
    SourceType  string          `json:"source_type,omitempty" db:"source_type"`
    Data        json.RawMessage `json:"graph_data,ommitempty"`
}

func main() {
    chart := Chart{}
    chart.ID = 1
    chart.Name = "Jishnu"
    str, err := json.Marshal(chart)
    fmt.Println(err)
}

已通过将 Chart.Data 设为指针来修复

 Data        *json.RawMessage `json:"data,ommitempty"`

Go 1.8(目前为 rc3)将正确处理指针和 non-pointer json.RawMessage.

的编组

修复提交:https://github.com/golang/go/commit/1625da24106b610f89ff7a67a11581df95f8e234