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

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

示例1: Regenerate

// Regenerate user's auth tokens.
func Regenerate(c *client.Client, username string, all bool) (string, error) {
    var reqBody []byte
    var err error

    if all == true {
        reqBody, err = json.Marshal(api.AuthRegenerateRequest{All: all})
    } else if username != "" {
        reqBody, err = json.Marshal(api.AuthRegenerateRequest{Name: username})
    }

    if err != nil {
        return "", err
    }

    body, err := c.BasicRequest("POST", "/v1/auth/tokens/", reqBody)

    if err != nil {
        return "", err
    }

    if all == true {
        return "", nil
    }

    token := api.AuthRegenerateResponse{}
    if err = json.Unmarshal([]byte(body), &token); err != nil {
        return "", err
    }

    return token.Token, nil
}

开发者ID:CodeJuan,项目名称:deis,代码行数:32,代码来源:auth.go

示例2: Get

func (i *Incident) Get(w http.ResponseWriter, r *http.Request) {
    w.Header().Add("content-type", "application/json")
    vars := mux.Vars(r)
    id, ok := vars["id"]
    if !ok {
        http.Error(w, "Must append incident id", http.StatusBadRequest)
        return
    }
    index := i.pipeline.GetIndex()

    // if the id is "*", fetch all outstanding incidents
    if id == "*" {
        all := index.ListIncidents()
        all = reduceStatusAbove(event.WARNING, all)
        buff, err := json.Marshal(makeKV(all))
        if err != nil {
            logrus.Error(err)
            http.Error(w, err.Error(), http.StatusInternalServerError)
            return
        }
        w.Write(buff)
        return
    }

    // write out the found incident. The value will be null if nothing was found
    in := index.GetIncident([]byte(id))
    buff, err := json.Marshal(in)
    if err != nil {
        logrus.Error(err)
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }

    w.Write(buff)
}

开发者ID:postfix,项目名称:bangarang,代码行数:35,代码来源:incident.go

示例3: SendNoLog

func (b *Broker) SendNoLog(resp Response) error {
    b.wLck.Lock()
    defer b.wLck.Unlock()

    if resp.Data == nil {
        resp.Data = M{}
    }

    s, err := json.Marshal(resp)
    if err != nil {
        // if there is a token, it means the client is waiting for a response
        // so respond with the json error. cause of json encode failure includes: non-utf8 string
        if resp.Token == "" {
            return err
        }

        s, err = json.Marshal(M{
            "error": "margo broker: cannot encode json response: " + err.Error(),
        })
        if err != nil {
            return err
        }
    }

    // the only expected write failure are due to broken pipes
    // which usually means the client has gone away so just ignore the error
    b.w.Write(s)
    b.w.Write([]byte{'\n'})
    return nil
}

开发者ID:ntcong,项目名称:sublime-text-2-config,代码行数:30,代码来源:broker.go

示例4: GetTagsListV2Handler

func GetTagsListV2Handler(ctx *macaron.Context, log *logs.BeeLogger) (int, []byte) {
    namespace := ctx.Params(":namespace")
    repository := ctx.Params(":repository")

    r := new(models.Repository)
    if has, _, err := r.Has(namespace, repository); err != nil || has == false {
        log.Error("[REGISTRY API V2] Repository not found: %v", repository)

        result, _ := json.Marshal(map[string]string{"message": "Repository not found"})
        return http.StatusNotFound, result
    }

    data := map[string]interface{}{}
    tags := []string{}

    data["name"] = fmt.Sprintf("%s/%s", namespace, repository)

    for _, value := range r.Tags {
        t := new(models.Tag)
        if err := t.GetByKey(value); err != nil {
            log.Error("[REGISTRY API V2] Tag not found: %v", err.Error())

            result, _ := json.Marshal(map[string]string{"message": "Tag not found"})
            return http.StatusNotFound, result
        }

        tags = append(tags, t.Name)
    }

    data["tags"] = tags

    result, _ := json.Marshal(data)
    return http.StatusOK, result
}

开发者ID:CodeJuan,项目名称:dockyard,代码行数:34,代码来源:manifests.go

示例5: writeSummaries

// writeSummaries retrieves status summaries from the supplied
// NodeStatusRecorder and persists them to the cockroach data store.
func (s *Server) writeSummaries() error {
    nodeStatus, storeStatuses := s.recorder.GetStatusSummaries()
    if nodeStatus != nil {
        key := keys.NodeStatusKey(int32(nodeStatus.Desc.NodeID))
        if err := s.db.Put(key, nodeStatus); err != nil {
            return err
        }
        if log.V(1) {
            statusJSON, err := json.Marshal(nodeStatus)
            if err != nil {
                log.Errorf("error marshaling nodeStatus to json: %s", err)
            }
            log.Infof("node %d status: %s", nodeStatus.Desc.NodeID, statusJSON)
        }
    }

    for _, ss := range storeStatuses {
        key := keys.StoreStatusKey(int32(ss.Desc.StoreID))
        if err := s.db.Put(key, &ss); err != nil {
            return err
        }
        if log.V(1) {
            statusJSON, err := json.Marshal(&ss)
            if err != nil {
                log.Errorf("error marshaling storeStatus to json: %s", err)
            }
            log.Infof("store %d status: %s", ss.Desc.StoreID, statusJSON)
        }
    }
    return nil
}

开发者ID:gechong,项目名称:cockroach,代码行数:33,代码来源:server.go

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