本文整理汇总了Golang中encoding/json.Unmarshaler类的典型用法代码### 示例。如果您正苦于以下问题:Golang Unmarshaler类的具体用法?Golang Unmarshaler怎么用?Golang Unmarshaler使用的例子?那么恭喜您, 这里精选的类代码### 示例或许可以为您提供帮助。

在下文中一共展示了Unmarshaler类的3个代码### 示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码### 示例。

示例1: unmarshalRequest

func unmarshalRequest(r *http.Request, req json.Unmarshaler, w http.ResponseWriter) bool {
    ctype := r.Header.Get("Content-Type")
    if ctype != "application/json" {
        writeError(w, r, httptypes.NewHTTPError(http.StatusUnsupportedMediaType, fmt.Sprintf("Bad Content-Type %s, accept application/json", ctype)))
        return false
    }
    b, err := ioutil.ReadAll(r.Body)
    if err != nil {
        writeError(w, r, httptypes.NewHTTPError(http.StatusBadRequest, err.Error()))
        return false
    }
    if err := req.UnmarshalJSON(b); err != nil {
        writeError(w, r, httptypes.NewHTTPError(http.StatusBadRequest, err.Error()))
        return false
    }
    return true
}

开发者ID:RomainVabre,项目名称:origin,代码行数:17,代码来源:client.go

示例2: unmarshalRequest

func unmarshalRequest(r *http.Request, req json.Unmarshaler, w http.ResponseWriter) bool {
    ctype := r.Header.Get("Content-Type")
    semicolonPosition := strings.Index(ctype, ";")
    if semicolonPosition != -1 {
        ctype = strings.TrimSpace(strings.ToLower(ctype[0:semicolonPosition]))
    }
    if ctype != "application/json" {
        writeError(w, r, httptypes.NewHTTPError(http.StatusUnsupportedMediaType, fmt.Sprintf("Bad Content-Type %s, accept application/json", ctype)))
        return false
    }
    b, err := ioutil.ReadAll(r.Body)
    if err != nil {
        writeError(w, r, httptypes.NewHTTPError(http.StatusBadRequest, err.Error()))
        return false
    }
    if err := req.UnmarshalJSON(b); err != nil {
        writeError(w, r, httptypes.NewHTTPError(http.StatusBadRequest, err.Error()))
        return false
    }
    return true
}

开发者ID:hongchaodeng,项目名称:etcd,代码行数:21,代码来源:client.go

示例3: testExpectedError

func testExpectedError(t *testing.T, expected error, xval string, ff json.Unmarshaler) {
    buf := []byte(`{"X":` + xval + `}`)
    err := ff.UnmarshalJSON(buf)
    require.Error(t, err, "ff[%T] failed to Unmarshal", ff)
    require.IsType(t, expected, err)
}

开发者ID:gooops,项目名称:ffjson,代码行数:6,代码来源:ff_test.go

最后编辑: kuteng  文档更新时间: 2021-08-23 19:14   作者:kuteng