本文整理汇总了Golang中encoding/json.Compact函数的典型用法代码示例。如果您正苦于以下问题:Golang Compact函数的具体用法?Golang Compact怎么用?Golang Compact使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Compact函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: equaljson
func equaljson(p, q []byte) bool {
cp := bytes.NewBuffer([]byte{})
if err := json.Compact(cp, p); err != nil {
log.Printf("unable to compact cp json for equaljson: %+v", err)
return false
}
cq := bytes.NewBuffer([]byte{})
if err := json.Compact(cq, q); err != nil {
log.Printf("unable to compact cq json for equaljson: %+v", err)
return false
}
if len(cp.Bytes()) != len(cq.Bytes()) {
return false
}
cpb, cqb := cp.Bytes(), cq.Bytes()
for i, b := range cpb {
if b != cqb[i] {
return false
}
}
return true
}
开发者ID:wkharold,项目名称:uber-apps,代码行数:29,代码来源:main_test.go
示例2: processJson
func processJson(filename string, src []byte, opt *JsonFmtOption) ([]byte, error) {
if opt.Compact {
var out bytes.Buffer
err := json.Compact(&out, src)
if err != nil {
return nil, err
}
return out.Bytes(), nil
} else {
var out bytes.Buffer
var err error
if opt.IndentTab {
err = json.Indent(&out, src, "", "\t")
} else {
var indent string
for i := 0; i < opt.TabWidth; i++ {
indent += " "
}
err = json.Indent(&out, src, "", indent)
}
if err != nil {
return nil, err
}
return out.Bytes(), nil
}
return src, nil
}
开发者ID:ewindfall,项目名称:go-liteide-tools,代码行数:27,代码来源:jsonfmt.go
示例3: pathRolesWrite
func pathRolesWrite(
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
var buf bytes.Buffer
uip, err := useInlinePolicy(d)
if err != nil {
return nil, err
}
if uip {
if err := json.Compact(&buf, []byte(d.Get("policy").(string))); err != nil {
return logical.ErrorResponse(fmt.Sprintf(
"Error compacting policy: %s", err)), nil
}
// Write the policy into storage
err := req.Storage.Put(&logical.StorageEntry{
Key: "policy/" + d.Get("name").(string),
Value: buf.Bytes(),
})
if err != nil {
return nil, err
}
} else {
// Write the arn ref into storage
err := req.Storage.Put(&logical.StorageEntry{
Key: "policy/" + d.Get("name").(string),
Value: []byte(d.Get("arn").(string)),
})
if err != nil {
return nil, err
}
}
return nil, nil
}
开发者ID:GauntletWizard,项目名称:vault,代码行数:35,代码来源:path_roles.go
示例4: readFiles
func readFiles(files ...string) []byte {
lfs := len(files)
chContent := make(chan []byte, lfs)
for _, file := range files {
go func(chContent chan []byte, configPath string) {
chContent <- format(configPath)
}(chContent, file)
}
bytess := make([][]byte, 0, lfs)
for i := 1; i <= lfs; i++ {
content := <-chContent
if len(content) != 0 {
bytess = append(bytess, content)
}
}
buf := bytes.NewBufferString(`{`)
buf.Write(bytes.Join(bytess, []byte(`,`)))
buf.WriteString(`}`)
var contentBuf bytes.Buffer
err := json.Compact(&contentBuf, buf.Bytes())
if err != nil {
log.Debug("<readFiles> jsonData: ", buf.String())
log.Fatal("<readFiles> error: ", err)
}
return contentBuf.Bytes()
}
开发者ID:golangers,项目名称:config,代码行数:33,代码来源:config.go
示例5: prepAndPostEvent
func prepAndPostEvent(eventFile string, preFunc PreFunc) (err error) {
rawEvent, err := ioutil.ReadFile(eventFile)
if err != nil {
return err
}
event := &Event{}
err = json.Unmarshal(rawEvent, &event)
if err != nil {
return err
}
preFunc(event)
rawEvent, err = json.Marshal(event)
if err != nil {
return err
}
buffer := new(bytes.Buffer)
err = json.Compact(buffer, rawEvent)
if err != nil {
return err
}
http.Post(pushUrl, "application/json", buffer)
return nil
}
开发者ID:th3architect,项目名称:go-machine-service,代码行数:26,代码来源:listener_test.go
示例6: compactJSON
func compactJSON(js []byte) []byte {
buf := new(bytes.Buffer)
if err := json.Compact(buf, js); err != nil {
return nil
}
return buf.Bytes()
}
开发者ID:hooklift,项目名称:terraform,代码行数:7,代码来源:utilities.go
示例7: testJSONMarshal
func testJSONMarshal(t *testing.T, v interface{}, expect string) {
j, err := json.Marshal(v)
if err != nil {
t.Errorf("Unable to marshal JSON for %v", v)
}
w := new(bytes.Buffer)
err = json.Compact(w, []byte(expect))
if err != nil {
t.Errorf("String is not valid json: %s", expect)
}
if w.String() != string(j) {
t.Errorf("json.Marshal(%q) returned %s, expect %s", v, j, w)
}
u := reflect.ValueOf(v).Interface()
if err := json.Unmarshal([]byte(expect), u); err != nil {
t.Errorf("Unable to unmarshal JSON for %v", expect)
}
if !reflect.DeepEqual(v, u) {
t.Errorf("json.Unmarshal(%q) returned %s, expect %s", expect, u, v)
}
}
开发者ID:juniorrobot,项目名称:gotrimet,代码行数:25,代码来源:trimet_test.go
示例8: jsonFromArg
// Return json supplied in the argument, or look for a file by the name given.
// Is the name is "STDIN", read the json from stdin
func jsonFromArg(arg string) ([]byte, error) {
var jsonArg []byte
var err error
arg = strings.TrimSpace(arg)
// assume that an opening bracket mean the json is given directly
if strings.HasPrefix(arg, "{") {
jsonArg = []byte(arg)
} else if arg == "STDIN" {
jsonArg, err = ioutil.ReadAll(os.Stdin)
if err != nil {
return nil, err
}
} else {
// all else fails, look for a file
jsonArg, err = ioutil.ReadFile(arg)
if err != nil {
return nil, err
}
}
// verify the json by compacting it
buf := bytes.NewBuffer(nil)
err = json.Compact(buf, jsonArg)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
开发者ID:zombor,项目名称:galaxy,代码行数:33,代码来源:stacks.go
示例9: parseSecurityOpts
// takes a local seccomp daemon, reads the file contents for sending to the daemon
func parseSecurityOpts(securityOpts []string) ([]string, error) {
for key, opt := range securityOpts {
con := strings.SplitN(opt, "=", 2)
if len(con) == 1 && con[0] != "no-new-privileges" {
if strings.Index(opt, ":") != -1 {
con = strings.SplitN(opt, ":", 2)
} else {
return securityOpts, fmt.Errorf("Invalid --security-opt: %q", opt)
}
}
if con[0] == "seccomp" && con[1] != "unconfined" {
f, err := ioutil.ReadFile(con[1])
if err != nil {
return securityOpts, fmt.Errorf("opening seccomp profile (%s) failed: %v", con[1], err)
}
b := bytes.NewBuffer(nil)
if err := json.Compact(b, f); err != nil {
return securityOpts, fmt.Errorf("compacting json for seccomp profile (%s) failed: %v", con[1], err)
}
securityOpts[key] = fmt.Sprintf("seccomp=%s", b.Bytes())
}
}
return securityOpts, nil
}
开发者ID:mefellows,项目名称:parity,代码行数:26,代码来源:parse.go
示例10: UnmarshalJSON
// UnmarshalJSON deserializes a JSON-serialized CardDescription
func (c *CardDescription) UnmarshalJSON(data []byte) error {
// Compact the JSON to make it easier to process below
buffer := bytes.NewBuffer([]byte{})
err := json.Compact(buffer, data)
if err != nil {
return err
}
data = buffer.Bytes()
// Since Description can be either a string value or an object, we
// must check and deserialize appropriately
if data[0] == 123 { // == }
obj := make(map[string]string)
err = json.Unmarshal(data, &obj)
if err != nil {
return err
}
c.Format = obj["format"]
c.Value = obj["value"]
} else {
c.Format = ""
err = json.Unmarshal(data, &c.Value)
}
if err != nil {
return err
}
return nil
}
开发者ID:shkw,项目名称:hipchat-go,代码行数:34,代码来源:room.go
示例11: generateQuery
func generateQuery(queryString, appLogsIdentifier, appLogsValue string, timestamp time.Time, from int) []byte {
query := `{
"fields": ["@timestamp", "message", "` + appLogsIdentifier + `"],
"query": {
"wildcard": {
"message": "` + queryString + `"
}
},
"filter": {
"bool": {
"must": [
{"term": {"` + appLogsIdentifier + `": "` + appLogsValue + `"}},
{"range": {"@timestamp": {"gt": "` + fmt.Sprintf("%04d-%02d-%02dT%02d:%02d:%02dZ", timestamp.Year(), timestamp.Month(), timestamp.Day(), timestamp.Hour(), timestamp.Minute(), timestamp.Second()) + `"}}}
]
}
},
"sort": {
"@timestamp": {
"order": "asc"
},
"message": {
"order": "asc"
}
},
"from": ` + fmt.Sprintf("%d", from) + `,
"size": ` + fmt.Sprintf("%d", size) + `
}`
var buf bytes.Buffer
json.Compact(&buf, []byte(query))
return buf.Bytes()
}
开发者ID:catalyzeio,项目名称:cli,代码行数:31,代码来源:logs.go
示例12: HandleListing
func HandleListing(writer http.ResponseWriter, request *http.Request, dir_path string) {
info, err := ioutil.ReadDir(dir_path)
if err != nil {
writer.WriteHeader(http.StatusNotFound)
return
}
fmt.Fprint(writer, "{\"files\":[")
for i, item := range info {
if i != 0 {
fmt.Fprint(writer, ",")
}
b, err := json.Marshal(FsEntry{
path: dir_path + item.Name(),
mod_time: item.ModTime(),
size: item.Size(),
entries: nil,
})
if err != nil {
writer.WriteHeader(http.StatusInternalServerError)
return
}
var buf bytes.Buffer
if err := json.Compact(&buf, b); err != nil {
writer.WriteHeader(http.StatusInternalServerError)
return
}
buf.WriteTo(writer)
}
fmt.Fprint(writer, "]}")
}
开发者ID:archshift,项目名称:fido,代码行数:33,代码来源:fido.go
示例13: iterateExecFile
func iterateExecFile(name string) (testCaseIterator chan testCase) {
name = locateFile(name)
fd, err := os.OpenFile(name, os.O_RDONLY, 0)
if err != nil {
panic(fmt.Sprintf("Could not open file %s", name))
}
testCaseIterator = make(chan testCase)
go func() {
defer close(testCaseIterator)
r := bufio.NewReader(fd)
lineno := 0
for {
binput, err := r.ReadBytes('\n')
if err != nil {
if err != io.EOF {
fmt.Printf("Line: %d\n", lineno)
panic(fmt.Errorf("Error reading file %s: %s", name, err.Error()))
}
break
}
lineno++
input := string(binput)
if input == "" || input == "\n" || input[0] == '#' || strings.HasPrefix(input, "Length:") {
//fmt.Printf("%s\n", input)
continue
}
err = json.Unmarshal(binput, &input)
if err != nil {
fmt.Printf("Line: %d, input: %s\n", lineno, binput)
panic(err)
}
input = strings.Trim(input, "\"")
var output []byte
for {
l, err := r.ReadBytes('\n')
lineno++
if err != nil {
fmt.Printf("Line: %d\n", lineno)
panic(fmt.Errorf("Error reading file %s: %s", name, err.Error()))
}
output = append(output, l...)
if l[0] == '}' {
output = output[:len(output)-1]
b := bytes.NewBuffer(make([]byte, 0, 64))
if err := json.Compact(b, output); err == nil {
output = b.Bytes()
}
break
}
if l[0] == '"' {
output = output[1 : len(output)-2]
break
}
}
testCaseIterator <- testCase{name, lineno, input, string(output)}
}
}()
return testCaseIterator
}
开发者ID:chinna1986,项目名称:vitess,代码行数:60,代码来源:plan_test.go
示例14: Jsonize
// Jsonize returns raw response on one line with no extra space.
func (f Formatter) Jsonize(input io.Reader) string {
var s bytes.Buffer
b, e := ioutil.ReadAll(input)
Check(e == nil, "failed to read input", e)
json.Compact(&s, b)
return s.String()
}
开发者ID:mesos-utility,项目名称:marathonctl,代码行数:8,代码来源:format.go
示例15: testJSONMarshal
// Helper function to test that a value is marshalled to JSON as expected.
func testJSONMarshal(t *testing.T, v interface{}, want string) {
j, err := json.Marshal(v)
if err != nil {
t.Errorf("Unable to marshal JSON for %v", v)
}
w := new(bytes.Buffer)
err = json.Compact(w, []byte(want))
if err != nil {
t.Errorf("String is not valid json: %s", want)
}
if w.String() != string(j) {
t.Errorf("json.Marshal(%q) returned %s, want %s", v, j, w)
}
// now go the other direction and make sure things unmarshal as expected
u := reflect.ValueOf(v).Interface()
if err := json.Unmarshal([]byte(want), u); err != nil {
t.Errorf("Unable to unmarshal JSON for %v", want)
}
if !reflect.DeepEqual(v, u) {
t.Errorf("json.Unmarshal(%q) returned %s, want %s", want, u, v)
}
}
开发者ID:MustWin,项目名称:go-github,代码行数:27,代码来源:github_test.go
示例16: NewRequestMaker
func NewRequestMaker(data Data) (reqMaker *RequestMaker, err error) {
u, err := url.Parse(data.Url)
if err != nil {
return
}
var contentType string
var body string
switch data.Enctype {
case "x_www":
contentType = "application/x-www-form-urlencoded"
querys := u.Query()
forms := make(url.Values)
for _, arg := range data.Args {
switch arg.Method {
case "GET":
querys.Add(arg.Key, arg.Value)
case "POST":
forms.Add(arg.Key, arg.Value)
}
}
u.RawQuery = querys.Encode()
body = forms.Encode()
case "json":
contentType = "text/json"
content := strings.TrimSpace(data.JsonContent)
if len(content) == 0 {
break
}
buffer := new(bytes.Buffer)
err = json.Compact(buffer, []byte(content))
if err != nil {
err = errors.ErrJsonCompact.NewMessageSpf(err)
return
}
body = buffer.String()
case "plain":
contentType = "text/plain"
body = data.PlainContent
}
reqMaker = &RequestMaker{
Method: data.Method,
Url: u,
ContentType: contentType,
Body: body,
Headers: data.Headers,
}
return
}
开发者ID:292388900,项目名称:http-api-tester,代码行数:60,代码来源:struct.go
示例17: TestTasks
func TestTasks(t *testing.T) {
for _, tst := range tt {
req, err := http.NewRequest(tst.method, tst.req, strings.NewReader(tst.payload))
if err != nil {
t.Error(err)
}
w := httptest.NewRecorder()
tst.hfn(tst.ctx, w, req)
if w.Code != tst.rc {
t.Errorf("%s: Response Code mismatch: expected %d, got %d", tst.description, tst.rc, w.Code)
continue
}
if len(tst.body) == 0 {
continue
}
if equaljson([]byte(w.Body.String()), []byte(tst.body)) == false {
body := bytes.NewBuffer([]byte{})
json.Compact(body, []byte(tst.body))
t.Errorf("%s: Body mismatch:\nexpected %s\ngot %s", tst.description, string(body.Bytes()), w.Body.String())
continue
}
}
}
开发者ID:wkharold,项目名称:uber-apps,代码行数:27,代码来源:main_test.go
示例18: mustMinifyJSON
func mustMinifyJSON(input string) string {
output := bytes.NewBuffer(nil)
if err := json.Compact(output, []byte(input)); err != nil {
panic(err)
}
return output.String()
}
开发者ID:nonbeing,项目名称:awsconsoleauth,代码行数:7,代码来源:policy.go
示例19: RenderStackTemplate
func (c Cluster) RenderStackTemplate(opts StackTemplateOptions) ([]byte, error) {
stackConfig, err := c.stackConfig(opts, true)
if err != nil {
return nil, err
}
rendered, err := execute(opts.StackTemplateTmplFile, stackConfig, false)
if err != nil {
return nil, err
}
//Use unmarshal function to do syntax validation
renderedBytes := []byte(rendered)
var jsonHolder map[string]interface{}
if err := json.Unmarshal(renderedBytes, &jsonHolder); err != nil {
syntaxError, ok := err.(*json.SyntaxError)
if ok {
contextString := getContextString(renderedBytes, int(syntaxError.Offset), 3)
return nil, fmt.Errorf("%v:\njson syntax error (offset=%d), in this region:\n-------\n%s\n-------\n", err, syntaxError.Offset, contextString)
}
return nil, err
}
// minify JSON
var buff bytes.Buffer
if err := json.Compact(&buff, renderedBytes); err != nil {
return nil, err
}
return buff.Bytes(), nil
}
开发者ID:whereisaaron,项目名称:coreos-kubernetes,代码行数:30,代码来源:config.go
示例20: UpdateRecord
func (db *simpleJSONDB) UpdateRecord(id uint32, data string) error {
var jsonBuffer bytes.Buffer
if err := json.Compact(&jsonBuffer, []byte(data)); err != nil {
return err
}
record := &core.Record{ID: id, Data: jsonBuffer.Bytes()}
return actions.Update(db.index, db.buffer, record)
}
开发者ID:fgrehm,项目名称:pucrs-simple-json-db,代码行数:8,代码来源:simple_json_db.go
注:本文中的encoding/json.Compact函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论