dongliang9682 2016-08-26 10:47
浏览 81
已采纳

没有调用MarshalJSON

I'm trying to customize the output of MarshalJSON, using the interface:

func (m *RawMessage) MarshalJSON() ([]byte, error)

I followed that tutorial: http://choly.ca/post/go-json-marshalling/

My purpose is removing replace one of the fields with true/false (if set or not), so I ended up writing that function:

func (u *Edition) MarshalJSON() ([]byte, error) {
    var vaultValue bool
    vaultValue = true
    var onlineValue bool
    vaultValue = false
    fmt.Println("here")
    if u.Vault == nil {
        vaultValue = false
    }
    if u.Online == nil {
        onlineValue = false
    }
    type AliasEdition Edition
    return json.Marshal(&struct {
        Vault  bool `json:"vault,omitempty"`
        Online bool `json:"online,omitempty"`
        *AliasEdition
    }{
        Vault:        vaultValue,
        Online:       onlineValue,
        AliasEdition: (*Alias)(u),
    })
}

The JSON is created from a map with the following instruction:

json.NewEncoder(w).Encode(EditionsMap)

Obviously EditionsMap is a Map of Editions structures:

var EditionsMap map[string]datamodel.Edition

The problem is that the MarshalJSON function apparently is never called.

Probably I'm doing something wrong, but I cannot understand what is the problem, my understanding is that I just need to implement that function in order to get it called.

  • 写回答

1条回答 默认 最新

  • drbd65446 2016-08-26 11:08
    关注

    This is because you declared the Edition.MarshalJSON() method with pointer receiver:

    func (u *Edition) MarshalJSON() ([]byte, error)
    

    And you try to marshal non-pointer values (your map contains datamodel.Edition values):

    var EditionsMap map[string]datamodel.Edition
    // ...
    json.NewEncoder(w).Encode(EditionsMap)
    

    Methods with pointer receiver are not part of the method set of the corresponding non-pointer type. The method set of type datamodel.Edition does not contain the method MarshalJSON().

    Spec: Method sets:

    A type may have a method set associated with it. The method set of an interface type is its interface. The method set of any other type T consists of all methods declared with receiver type T. The method set of the corresponding pointer type *T is the set of all methods declared with receiver *T or T (that is, it also contains the method set of T).

    Try to marshal pointer values, define your map to contain pointers:

    var EditionsMap map[string]*datamodel.Edition
    // ...
    if err := json.NewEncoder(w).Encode(EditionsMap); err != nil {
        panic(err) // HANDLE error somehow, do not omit it like in your example!
    }
    

    Values of the pointer type *Edition does have a method MarshalJSON() which will be called properly by the json package. Try a working example of this on the Go Playground.

    Another option would be to define the Edition.MarshalJSON() method with value receiver:

    func (u Edition) MarshalJSON() ([]byte, error)
    

    And this way it would work no matter if you marshal pointer or non-pointer values, as the methods with value receiver are part of the method set of both the Edition type and the corresponding *Edition pointer type. Try a working example of this variant on the Go Playground.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥50 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗