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

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

示例1: runQueryExtended

func runQueryExtended(engine EngineI, query string, c *C, appendPoints bool, expectedSeries string) {

    var result []*protocol.Series
    err := engine.RunQuery(nil, "", query, func(series *protocol.Series) error {
        if appendPoints && result != nil {
            result[0].Points = append(result[0].Points, series.Points...)
        } else {
            result = append(result, series)
        }
        return nil
    })

    c.Assert(err, IsNil)

    series, err := common.StringToSeriesArray(expectedSeries)
    c.Assert(err, IsNil)

    if !reflect.DeepEqual(result, series) {
        resultData, _ := json.MarshalIndent(result, "", "  ")
        seriesData, _ := json.MarshalIndent(series, "", "  ")

        fmt.Fprintf(os.Stderr,
            "===============\nThe two series aren't equal.\nExpected: %s\nActual: %s\n===============\n",
            seriesData, resultData)
    }

    c.Assert(result, DeepEquals, series)
}

开发者ID:rudrapranay,项目名称:influxdb,代码行数:28,代码来源:engine_test.go

示例2: GetOS

func GetOS(w http.ResponseWriter, r *http.Request) {
    var os_query libocit.OS
    os_query = GetOSQuery(r)

    ids := GetResourceList(os_query)

    var ret libocit.HttpRet
    if len(ids) < 1 {
        ret.Status = "Failed"
        ret.Message = "Cannot find the avaliable OS"
    } else {
        ret.Status = "OK"
        ret.Message = "Find the avaliable OS"
        var oss []libocit.OS
        for index := 0; index < len(ids); index++ {
            oss = append(oss, *(store[ids[index]]))
        }

        data, _ := json.MarshalIndent(oss, "", "\t")
        ret.Data = string(data)
    }

    body, _ := json.MarshalIndent(ret, "", "\t")
    w.Write([]byte(body))
}

开发者ID:sdgdsffdsfff,项目名称:oct-engine,代码行数:25,代码来源:testserver.go

示例3: ServeHTTP

// ServeHTTP shows the current plans in the query cache.
func (plr *Planner) ServeHTTP(response http.ResponseWriter, request *http.Request) {
    if err := acl.CheckAccessHTTP(request, acl.DEBUGGING); err != nil {
        acl.SendError(response, err)
        return
    }
    if request.URL.Path == "/debug/query_plans" {
        keys := plr.plans.Keys()
        response.Header().Set("Content-Type", "text/plain")
        response.Write([]byte(fmt.Sprintf("Length: %d\n", len(keys))))
        for _, v := range keys {
            response.Write([]byte(fmt.Sprintf("%#v\n", v)))
            if plan, ok := plr.plans.Peek(v); ok {
                if b, err := json.MarshalIndent(plan, "", "  "); err != nil {
                    response.Write([]byte(err.Error()))
                } else {
                    response.Write(b)
                }
                response.Write(([]byte)("\n\n"))
            }
        }
    } else if request.URL.Path == "/debug/vschema" {
        response.Header().Set("Content-Type", "application/json; charset=utf-8")
        b, err := json.MarshalIndent(plr.VSchema().Keyspaces, "", " ")
        if err != nil {
            response.Write([]byte(err.Error()))
            return
        }
        buf := bytes.NewBuffer(nil)
        json.HTMLEscape(buf, b)
        response.Write(buf.Bytes())
    } else {
        response.WriteHeader(http.StatusNotFound)
    }
}

开发者ID:dumbunny,项目名称:vitess,代码行数:35,代码来源:planner.go

示例4: SaveConfig

// Saves either of the two config types to the specified file with the specified permissions.
func SaveConfig(fileout string, config interface{}, perms os.FileMode) error {

    //check to see if we got a struct or a map (minimal or extended config, respectively)
    v := reflect.ValueOf(config)
    if v.Kind() == reflect.Struct {
        config := config.(Config)

        //Parse the nicely formatted security section, and set the raw values for JSON marshalling
        newSecurity := make([]interface{}, 0)
        if config.Security.NoFiles {
            newSecurity = append(newSecurity, "nofiles")
        }
        setuser := make(map[string]interface{})
        setuser["setuser"] = config.Security.SetUser
        newSecurity = append(newSecurity, setuser)
        config.RawSecurity = newSecurity

        jsonout, err := json.MarshalIndent(config, "", "    ")
        if err != nil {
            return err
        }
        return ioutil.WriteFile(fileout, jsonout, perms)
    } else if v.Kind() == reflect.Map {
        jsonout, err := json.MarshalIndent(config, "", "    ")
        if err != nil {
            return err
        }
        return ioutil.WriteFile(fileout, jsonout, perms)
    }
    return fmt.Errorf("Something very bad happened")
}

开发者ID:waaghals,项目名称:go-cjdns,代码行数:32,代码来源:config.go

示例5: TestUnmarshal

`
func TestUnmarshal(t *testing.T) {
tests := []struct {
filename string
typ interface{}
expected string
}{
// ASCII
{“ascii.mof”, MSFT_DSCConfigurationStatus{}, asciiMOFExpected},

    // UTF-16, little-endian
    {"utf16-le.mof", MSFT_DSCConfigurationStatus{}, utf16leMOFExpected},
}
for _, test := range tests {
    b, err := ioutil.ReadFile(test.filename)
    if err != nil {
        t.Fatal(err)
    }
    var v interface{}
    if err := Unmarshal(b, &v); err != nil {
        t.Fatal(err)
    }
    s := test.typ
    if err := Unmarshal(b, &s); err != nil {
        t.Fatal(err)
    }
    vb, _ := json.MarshalIndent(&v, "", "  ")
    sb, _ := json.MarshalIndent(&s, "", "  ")
    if string(vb) != test.expected {
        t.Errorf("%v: unexpected interface value", test.filename)
    }
    if string(sb) != test.expected {
        t.Errorf("%v: unexpected struct value", test.filename)
    }
}

}

开发者ID:eswdd,项目名称:bosun,代码行数:35,代码来源:mof_test.go

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