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

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

示例1: SendHeartbeat

// SendHeartbeat is used to notify the namenode of a valid connection
// on a periodic basis
func SendHeartbeat(encoder *json.Encoder) {
    p := new(Packet)
    p.SRC = id
    p.DST = "NN"
    p.CMD = HB
    encoder.Encode(p)
}

开发者ID:sjarvie,项目名称:GoDFS,代码行数:9,代码来源:datanode.go

示例2: ServeHTTP

func (m *MemberTrashListHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
    var memberlist []*membersys.MemberWithKey
    var enc *json.Encoder
    var err error

    if !m.auth.IsAuthenticatedScope(req, m.admingroup) {
        rw.WriteHeader(http.StatusUnauthorized)
        return
    }

    memberlist, err = m.database.EnumerateTrashedMembers(
        req.FormValue("start"), m.pagesize)
    if err != nil {
        log.Print("Error enumerating trashed members: ", err)
        rw.WriteHeader(http.StatusInternalServerError)
        rw.Write([]byte("Error enumerating trashed members: " + err.Error()))
        return
    }

    rw.Header().Set("Content-Type", "application/json; encoding=utf8")
    enc = json.NewEncoder(rw)
    if err = enc.Encode(memberlist); err != nil {
        log.Print("Error JSON encoding member list: ", err)
        rw.WriteHeader(http.StatusInternalServerError)
        rw.Write([]byte("Error encoding result: " + err.Error()))
        return
    }
}

开发者ID:starshipfactory,项目名称:membersys,代码行数:28,代码来源:trash.go

示例3: HandleResponse

// HandleResponse delegates actions to perform based on the
// contents of a recieved Packet, and encodes a response
func HandleResponse(p Packet, encoder *json.Encoder) {
    r := new(Packet)
    r.SRC = id
    r.DST = p.SRC

    switch p.CMD {
    case ACK:
        return
    case LIST:
        list := GetBlockHeaders()
        r.Headers = make([]BlockHeader, len(list))

        for i, b := range list {
            r.Headers[i] = b
        }
        r.CMD = LIST
    case BLOCK:
        r.CMD = BLOCKACK
        WriteBlock(p.Data)
        r.Headers = make([]BlockHeader, 0, 2)
        r.Headers = append(r.Headers, p.Data.Header)

    case RETRIEVEBLOCK:
        fmt.Println("retrieving block from ", p.Headers[0])
        b := BlockFromHeader(p.Headers[0])
        r.CMD = BLOCK
        r.Data = b
    }
    encoder.Encode(*r)
}

开发者ID:sjarvie,项目名称:GoDFS,代码行数:32,代码来源:datanode.go

示例4: authenticateAndAuthorize

// authenticate request and authorize action
func authenticateAndAuthorize(action Action, urlVars map[string]string, params url.Values, resp http.ResponseWriter, enc *json.Encoder) (ok bool) {
    authenticated, client, errorMessage := g.Authenticate(params)
    if !authenticated {
        log.Println("unauthenticated request:\n\tURL parameters:", params, "\n\terror message:", errorMessage)

        resp.WriteHeader(http.StatusUnauthorized)
        err := enc.Encode(apiResponse{errorMessage, "", nil})
        if err != nil {
            log.Println(err)
        }

        return
    }

    kind := urlVars["kind"]
    id := urlVars["id"]
    authorized, errorMessage := g.Authorize(client, action, urlVars)
    if !authorized {
        log.Println("unauthorized request:\n\tclient:", client, "\n\taction:", action, "kind:", kind, "id:", id, "\n\terror message", errorMessage)

        resp.WriteHeader(http.StatusForbidden)
        err := enc.Encode(apiResponse{errorMessage, "", nil})
        if err != nil {
            log.Println(err)
        }

        return
    }

    ok = true
    return
}

开发者ID:namsral,项目名称:crudapi,代码行数:33,代码来源:api.go

示例5: create

func create(storage Storage, resp http.ResponseWriter, vars map[string]string, enc *json.Encoder, dec *json.Decoder) {
    var resource map[string]interface{}
    err := dec.Decode(&resource)

    if err != nil {
        log.Println(err)

        resp.WriteHeader(http.StatusBadRequest)
        err = enc.Encode(apiResponse{"malformed json", "", nil})
        if err != nil {
            log.Println(err)
        }

        return
    }

    // set in storage
    id, stoResp := storage.Create(vars["collection"], resource)

    // write response
    resp.WriteHeader(stoResp.StatusCode())
    err = enc.Encode(apiResponse{stoResp.Error(), id, nil})
    if err != nil {
        log.Println(err)
    }
}

开发者ID:sauerbraten,项目名称:crudapi,代码行数:26,代码来源:api.go

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