Golang MarshalIndent示例

说明

golang marshalindent示例是从最受好评的开源项目中提取的实现代码,你可以参考下面示例的使用方式。

编程语言: Golang

命名空间/包名称: encoding/json

示例#1
文件: engine_test.go 项目: rudrapranay/influxdb

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)
}

示例#2
文件: testserver.go 项目: sdgdsffdsfff/oct-engine

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))
}

示例#3
文件: planner.go 项目: dumbunny/vitess

// 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)
	}
}

示例#4
文件: config.go 项目: waaghals/go-cjdns

// 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")
}

示例#5
文件: mof_test.go 项目: eswdd/bosun

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)
		}
	}
}

示例#6
文件: stack_test.go 项目: koding/koding

func equal(got, want string) error {
	var v1, v2 interface{}

	if err := json.Unmarshal([]byte(got), &v1); err != nil {
		return err
	}

	if err := json.Unmarshal([]byte(want), &v2); err != nil {
		return err
	}

	stripNondeterministicResources(v1)
	stripNondeterministicResources(v2)

	if !reflect.DeepEqual(v1, v2) {
		p1, err := json.MarshalIndent(v1, "", "\t")
		if err != nil {
			panic(err)
		}

		p2, err := json.MarshalIndent(v2, "", "\t")
		if err != nil {
			panic(err)
		}

		return fmt.Errorf("got:\n%s\nwant:\n%s\n", p1, p2)
	}

	return nil
}

示例#7
文件: TxnManager.go 项目: hongjinqiu/finance

func (o TxnManager) pubTxnCollectionIfNotExist(txnId int, collection string) {
	query := map[string]interface{}{
		"_id": txnId,
		"collections": map[string]interface{}{
			"$nin": []string{collection},
		},
	}
	update := map[string]interface{}{
		"$push": map[string]interface{}{
			"collections": collection,
		},
	}
	updateByte, err := json.MarshalIndent(&update, "", "\t")
	if err != nil {
		panic(err)
	}
	queryByte, err := json.MarshalIndent(&query, "", "\t")
	if err != nil {
		panic(err)
	}
	log.Println("pubTxnCollectionIfNotExist,update Transactions update:" + string(updateByte) + ", query:" + string(queryByte))
	if err := o.DB.C("Transactions").Update(query, update); err != nil {
		if err != mgo.ErrNotFound {
			panic(err)
		}
	}
}

示例#8
文件: schema_test.go 项目: choirudin2210/platform-layer

func TestInterfaceSchema(t *testing.T) {
	var config struct {
		Field interface{}
	}

	schema := Of(config)
	value, _ := json.MarshalIndent(schema, "", "    ")
	t.Log(string(value))

	value, _ = json.MarshalIndent(schema, "\t\t", "    ")
	assert.Equal(t, string(value), `{
		    "schema": {
		        "type": "object",
		        "properties": {
		            "Field": {
		                "type": "string",
		                "required": true,
		                "title": "Field",
		                "propertyOrder": 1
		            }
		        },
		        "required": true,
		        "title": ""
		    }
		}`)
}

示例#9
文件: message.go 项目: Ripleather/starter-go-bot

func ExampleMessage() {
	client := wit.NewClient(os.Getenv("WIT_ACCESS_TOKEN"))

	// Process a text message
	request := &wit.MessageRequest{}
	request.Query = "Hello world"
	result, err := client.Message(request)
	if err != nil {
		log.Println(err)
		os.Exit(-1)
	}
	log.Println(result)
	data, _ := json.MarshalIndent(result, "", "    ")
	log.Println(string(data[:]))

	// Process an audio/wav message
	request = &wit.MessageRequest{}
	request.File = "../audio_sample/helloWorld.wav"
	request.ContentType = "audio/wav;rate=8000"
	result, err = client.AudioMessage(request)
	if err != nil {
		log.Println(err)
		os.Exit(-1)
	}
	log.Println(result)
	data, _ = json.MarshalIndent(result, "", "    ")
	log.Println(string(data[:]))
}

示例#10
文件: inspect_test.go 项目: phusl/machine

func TestCmdInspectFormat(t *testing.T) {
	actual, host := runInspectCommand(t, []string{"test-a"})
	expected, _ := json.MarshalIndent(host, "", "    ")
	assert.Equal(t, string(expected), actual)

	actual, _ = runInspectCommand(t, []string{"--format", "{{.DriverName}}", "test-a"})
	assert.Equal(t, "none", actual)

	actual, _ = runInspectCommand(t, []string{"--format", "{{json .DriverName}}", "test-a"})
	assert.Equal(t, "\"none\"", actual)

	actual, _ = runInspectCommand(t, []string{"--format", "{{prettyjson .Driver}}", "test-a"})
	type ExpectedDriver struct {
		CaCertPath     string
		IPAddress      string
		MachineName    string
		PrivateKeyPath string
		SSHPort        int
		SSHUser        string
		SwarmDiscovery string
		SwarmHost      string
		SwarmMaster    bool
		URL            string
	}
	expected, err := json.MarshalIndent(&ExpectedDriver{MachineName: "test-a", URL: "unix:///var/run/docker.sock"}, "", "    ")
	assert.NoError(t, err)
	assert.Equal(t, string(expected), actual)
}

示例#11
文件: mongoweb.go 项目: hkolasani/goauth

func processRefreshTokenFlow(authRequest mongodb.Document) (responseContent []byte) {

	var err error
	var user mongodb.Document
	var token mongodb.Document
	var accessToken mongodb.Document
	var refreshToken mongodb.Document

	token, err = getToken(authRequest[REFRESH_TOKEN].(string), authRequest[CLIENT_ID].(string), REFRESH_TOKEN_COLLECTION)

	if err == nil {
		//check if user exists
		user, err = getUser(mongodb.Document{ID: token[USER_ID]}) //get user from db
		if err == nil {
			//create tokens
			accessToken, err = generateToken(user, authRequest, ACCESS_TOKEN_COLLECTION)
			if err == nil {
				refreshToken, err = generateToken(user, authRequest, REFRESH_TOKEN_COLLECTION)
			}
		}
	}

	if err != nil {
		mwError := bson.M{"errorCode": "500", "errorMessage": err.Error()}
		responseContent, _ = json.MarshalIndent(mwError, "", "  ")
	} else {
		//success reponse with toekn info.
		tokenInfo := bson.M{ACCESS_TOKEN: accessToken[TOKEN], REFRESH_TOKEN: refreshToken[TOKEN]}
		responseContent, _ = json.MarshalIndent(tokenInfo, "", "  ")
	}

	return responseContent
}

示例#12
文件: lex_test.go 项目: yoshiyaka/mosa

func TestLex(t *testing.T) {
	for _, test := range lexTests {
		for i, _ := range test.ast.Classes {
			test.ast.Classes[i].Filename = "test.manifest"
			normalizeBlock(&test.ast.Classes[i].Block, "test.manifest")
		}
		for i, _ := range test.ast.Defines {
			test.ast.Defines[i].Filename = "test.manifest"
			normalizeBlock(&test.ast.Defines[i].Block, "test.manifest")
		}
		for i, _ := range test.ast.Nodes {
			test.ast.Nodes[i].Filename = "test.manifest"
			normalizeBlock(&test.ast.Nodes[i].Block, "test.manifest")
		}

		ast := NewAST()
		if err := Parse(ast, "test.manifest", strings.NewReader(test.manifest)); err != nil {
			t.Log(test.manifest)
			t.Error(err)
		} else {
			if !equalsAsJson(ast, test.ast) {
				t.Logf("%#v", test.ast)
				t.Logf("%#v", ast)
				js2, _ := json.MarshalIndent(test.ast, "", "  ")
				t.Log(string(js2))
				js, _ := json.MarshalIndent(ast, "", "  ")
				t.Log(string(js))
				t.Error("Expected manifest", test.manifest)
				if ast != nil {
					t.Log("Read manifest", ast.String())
				}
			}
		}
	}
}

示例#13
文件: site.go 项目: CannedYerins/distributed-systems

func (s *Site) logFiles() {
	log := make([]Event, len(s.L))
	i := 0
	for k := range s.L {
		log[i] = k
		i++
	}
	slog, err := json.MarshalIndent(log, "", "\t")
	if err != nil {
		fmt.Printf("%+v\n", err)
	} else {
		ioutil.WriteFile("log.json", slog, 0666)
	}

	cal := make([]Appointment, len(s.V))
	i = 0
	for _, v := range s.V {
		cal[i] = v
		i++
	}
	scal, err := json.MarshalIndent(cal, "", "\t")
	if err != nil {
		fmt.Printf("%+v\n", err)
	} else {
		ioutil.WriteFile("cal.json", scal, 0666)
	}
}

示例#14
文件: actionnode.go 项目: aaijazi/vitess

// ToJSON returns a JSON representation of the object.
func (n *ActionNode) ToJSON() (string, error) {
	data, err := json.MarshalIndent(n, "", "  ")
	if err != nil {
		return "", fmt.Errorf("cannot JSON-marshal node: %v", err)
	}
	result := string(data) + "\n"
	if n.Args == nil {
		result += "{}\n"
	} else {
		data, err := json.MarshalIndent(n.Args, "", "  ")
		if err != nil {
			return "", fmt.Errorf("cannot JSON-marshal node args: %v", err)
		}
		result += string(data) + "\n"
	}
	if n.Reply == nil {
		result += "{}\n"
	} else {
		data, err := json.MarshalIndent(n.Reply, "", "  ")
		if err != nil {
			return "", fmt.Errorf("cannot JSON-marshal node reply: %v", err)
		}
		result += string(data) + "\n"
	}
	return result, nil
}

示例#15
文件: flatten_test.go 项目: peacekeeper/golang-github-linkeddata-gojsonld-dev

func testFlatten(inputFile, outputFile, contextFile, base string,
	compactArrays bool, t *testing.T) {
	inputJson, jsonErr := ReadJSONFromFile(test_dir + inputFile)
	if !isNil(jsonErr) {
		t.Error("Could not open input file")
		return
	}
	outputJson, jsonErr := ReadJSONFromFile(test_dir + outputFile)
	if !isNil(jsonErr) {
		t.Error("Could not open output file")
		return
	}
	contextJson, jsonErr := ReadJSONFromFile(test_dir + contextFile)
	if !isNil(jsonErr) {
		contextJson = nil
	}
	options := &Options{
		Base:           base,
		CompactArrays:  compactArrays,
		ExpandContext:  nil,
		DocumentLoader: NewDocumentLoader(),
	}
	flattenedJson, flattenErr := Flatten(inputJson, contextJson, options)
	if !isNil(flattenErr) {
		t.Error("Compaction failed with error ", flattenErr.Error())
		return
	}
	flattenedString, _ := json.MarshalIndent(flattenedJson, "", "    ")
	outputString, _ := json.MarshalIndent(outputJson, "", "    ")
	if !reflect.DeepEqual(flattenedJson, outputJson) {
		t.Error("Expected:\n", string(outputString), "\nGot:\n",
			string(flattenedString))
	}
}

示例#16
文件: imagedata.go 项目: tyrken/rocker

// PrettyString returns RockerImageData as a printable string
func (data *RockerImageData) PrettyString() string {
	prettyVars, err := json.MarshalIndent(data.Vars, "", "  ")
	if err != nil {
		log.Fatal(err)
	}
	prettyProps, err := json.MarshalIndent(data.Properties, "", "  ")
	if err != nil {
		log.Fatal(err)
	}
	green := color.New(color.FgGreen).SprintfFunc()
	yellow := color.New(color.FgYellow).SprintfFunc()
	sep := "=======================================================\n"

	res := fmt.Sprintf("%s%s\n", green(sep),
		green("Image: %s", data.ImageName.String()))

	if !data.Created.IsZero() {
		res = fmt.Sprintf("%sCreated: %s\n", res, data.Created.Format(time.RFC850))
	}

	if data.Properties != nil {
		res = fmt.Sprintf("%sProperties: %s\n", res, prettyProps)
	}

	if data.Vars != nil {
		res = fmt.Sprintf("%sVars: %s\n", res, prettyVars)
	}

	if data.Rockerfile != "" {
		res = fmt.Sprintf("%s%s\n%s\n%s\n%s", res, yellow("Rockerfile:"), yellow(sep), data.Rockerfile, yellow(sep))
	}

	return res
}

示例#17
文件: doc.go 项目: OpenCoreData/ocdWeb

func UUIDRender(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)

	// call mongo and lookup the redirection to use...
	session, err := services.GetMongoCon()
	if err != nil {
		panic(err)
	}
	defer session.Close()

	// Optional. Switch the session to a monotonic behavior.
	session.SetMode(mgo.Monotonic, true)
	c := session.DB("test").C("schemaorg")
	c2 := session.DB("test").C("csvwmeta")

	// Get the schema.org data
	URI := fmt.Sprintf("http://opencoredata.org/id/dataset/%s", vars["UUID"])
	result := SchemaOrgMetadata{}
	err = c.Find(bson.M{"url": URI}).One(&result)
	if err != nil {
		log.Printf("URL lookup error: %v", err)
	}

	// context setting hack
	// result.Context = ` "opencore": "http://opencoredata.org/voc/1/", "glview": "http://geolink.org/view/1/", "schema": "http://schema.org/"`
	result.Context = "http://schema.org"

	jsonldtext, _ := json.MarshalIndent(result, "", " ") // results as embeddale JSON-LD

	// Get the CSVW  data
	result2 := CSVWMeta{}
	err = c2.Find(bson.M{"url": URI}).One(&result2)
	if err != nil {
		log.Printf("URL lookup error: %v", err)
	}

	// result.Context = ` "opencore": "http://opencoredata.org/voc/1/", "glview": "http://geolink.org/view/1/", "schema": "http://schema.org/"`
	// needs to be:     "@context": ["http://www.w3.org/ns/csvw", {"@language": "en"}],
	result2.Context = "http://www.w3.org/ns/csvw"

	csvwtext, _ := json.MarshalIndent(result2, "", " ") // results as embeddale JSON-LD

	ht, err := template.New("some template").ParseFiles("templates/jrso_dataset_new.html") //open and parse a template text file
	if err != nil {
		log.Printf("template parse failed: %s", err)
	}

	// need a simple function call to extract the "janus" keyword from the keyword string and toLower it and
	// pass it in this struct to use in the data types web component
	measureString := getJanusKeyword(result.Keywords)
	dataForTemplate := TemplateForDoc{Schema: result, CSVW: result2, Schemastring: template.JS(string(jsonldtext)), Csvwstring: template.JS(string(csvwtext)), MeasureType: measureString, UUID: vars["UUID"]}

	err = ht.ExecuteTemplate(w, "T", dataForTemplate) //substitute fields in the template 't', with values from 'user' and write it out to 'w' which implements io.Writer
	if err != nil {
		log.Printf("htemplate execution failed: %s", err)
	}

	// go get the CSVW metadata and inject the whole package and the rendered table

}

示例#18
文件: event_log.go 项目: foomo/shop

func SaveShopEvent(action ActionShop, info *Info, err error, description string) {
	debug.Log("Action", string(action))
	event := NewEvent()
	if info != nil {
		event.Info = info
	}

	event.Info.Caller = trace.WhoCalledMe(4)
	if err != nil {
		event.Type = EventTypeError
		event.Error = err.Error()
	} else {
		event.Type = EventTypeSuccess
	}

	event.Action = string(action)
	event.Info.Description = description

	if !saveShopEventDB(event) {
		jsonBytes, err := json.MarshalIndent(event, "", "	")
		if err != nil {
			log.Println("Could not jsonMarshal event")
		}
		log.Println("Saving Shop Event failed! ", string(jsonBytes))
	}
	jsonBytes, _ := json.MarshalIndent(event, "", "	")
	debug.Log("Saved Shop Event! ", string(jsonBytes))
}

示例#19
文件: apply.go 项目: koding/koding

func Equal(got, want string, fn func(string) string) error {
	var v1, v2 interface{}

	if err := json.Unmarshal([]byte(got), &v1); err != nil {
		return fmt.Errorf(`failed to parse "got" JSON: %s`, err)
	}

	if err := json.Unmarshal([]byte(want), &v2); err != nil {
		return fmt.Errorf(`failed to parse "want" JSON: %s`, err)
	}

	if err := mask(v1, fn); err != nil {
		return err
	}

	if err := mask(v2, fn); err != nil {
		return err
	}

	if !reflect.DeepEqual(v1, v2) {
		p1, err := json.MarshalIndent(v1, "", "\t")
		if err != nil {
			panic(err)
		}

		p2, err := json.MarshalIndent(v2, "", "\t")
		if err != nil {
			panic(err)
		}

		return fmt.Errorf("got:\n%s\nwant:\n%s\n", p1, p2)
	}

	return nil
}

示例#20
文件: fetchall.go 项目: mehulsbhatt/usergrid-go-sdk

func main() {
	client := usergrid.Client{Organization: ORGNAME, Application: APPNAME, Uri: API}
	var objmap interface{}
	var results = make([]interface{}, 0)
	messages := make(chan interface{})
	done := make(chan bool, 1)
	if len(CLIENT_ID) > 0 && len(CLIENT_SECRET) > 0 {
		err := client.OrgLogin(CLIENT_ID, CLIENT_SECRET)
		if err != nil {
			log.Printf(err.Error())
			return
		}
	}
	go func() {
		for {
			select {
			case v := <-messages:
				results = append(results, v)
			case <-time.After(time.Second * 10):
				fmt.Println("Timeout!")
			}
		}
	}()
	client.Get("", nil, usergrid.JSONResponseHandler(&objmap))
	str, _ := json.MarshalIndent(objmap, "", "  ")
	log.Printf("RESPONSE: %s", str)
	go FetchAll(client, ENDPOINT, messages, done, "")
	<-done
	entities, _ := json.MarshalIndent(results, "", "  ")
	fmt.Printf("%s\n", entities)
	log.Printf("Done. %d requests and %d responses", REQUESTS, RESPONSES)
	return
}

示例#21
文件: summoner_test.go 项目: 1lann/go-riot

func TestSummonerByName(t *testing.T) {
	a := NewAccess(testApiKey, testPatch)

	result, err := a.SummonerByName("oce", "onelann")
	if err != nil {
		t.Error(err)
		return
	}

	printOut, err := json.MarshalIndent(result, "", "    ")
	if err != nil {
		t.Error(err)
		return
	}

	result, err = a.SummonerById("oce", result.Id)

	nextPrintOut, err := json.MarshalIndent(result, "", "    ")
	if err != nil {
		t.Error(err)
		return
	}

	if string(nextPrintOut) != string(printOut) {
		t.Error("Non-matching summoner results!")
		return
	}
}

示例#22
文件: accumulator.go 项目: ronaldslc/telegraf

func (a *Accumulator) AssertContainsTaggedFields(
	t *testing.T,
	measurement string,
	fields map[string]interface{},
	tags map[string]string,
) {
	for _, p := range a.Points {
		if !reflect.DeepEqual(tags, p.Tags) {
			continue
		}

		if p.Measurement == measurement {
			if !reflect.DeepEqual(fields, p.Fields) {
				pActual, _ := json.MarshalIndent(p.Fields, "", "  ")
				pExp, _ := json.MarshalIndent(fields, "", "  ")
				msg := fmt.Sprintf("Actual:\n%s\n(%T) \nExpected:\n%s\n(%T)",
					string(pActual), p.Fields, string(pExp), fields)
				assert.Fail(t, msg)
			}
			return
		}
	}
	msg := fmt.Sprintf("unknown measurement %s with tags %v", measurement, tags)
	assert.Fail(t, msg)
}

示例#23
文件: protocol_test.go 项目: hernad/syncthing

func testMarshal(t *testing.T, prefix string, m1, m2 message) bool {
	failed := func(bc []byte) {
		bs, _ := json.MarshalIndent(m1, "", "  ")
		ioutil.WriteFile(prefix+"-1.txt", bs, 0644)
		bs, _ = json.MarshalIndent(m2, "", "  ")
		ioutil.WriteFile(prefix+"-2.txt", bs, 0644)
		if len(bc) > 0 {
			f, _ := os.Create(prefix + "-data.txt")
			fmt.Fprint(f, hex.Dump(bc))
			f.Close()
		}
	}

	buf, err := m1.MarshalXDR()
	if err != nil && strings.Contains(err.Error(), "exceeds size") {
		return true
	}
	if err != nil {
		failed(nil)
		t.Fatal(err)
	}

	err = m2.UnmarshalXDR(buf)
	if err != nil {
		failed(buf)
		t.Fatal(err)
	}

	ok := reflect.DeepEqual(m1, m2)
	if !ok {
		failed(buf)
	}
	return ok
}

示例#24
文件: convenience.go 项目: sstrato/gophercloud

// isJSONEquals is a utility function that implements JSON comparison for AssertJSONEquals and
// CheckJSONEquals.
func isJSONEquals(t *testing.T, expectedJSON string, actual interface{}) bool {
	var parsedExpected, parsedActual interface{}
	err := json.Unmarshal([]byte(expectedJSON), &parsedExpected)
	if err != nil {
		t.Errorf("Unable to parse expected value as JSON: %v", err)
		return false
	}

	jsonActual, err := json.Marshal(actual)
	AssertNoErr(t, err)
	err = json.Unmarshal(jsonActual, &parsedActual)
	AssertNoErr(t, err)

	if !reflect.DeepEqual(parsedExpected, parsedActual) {
		prettyExpected, err := json.MarshalIndent(parsedExpected, "", "  ")
		if err != nil {
			t.Logf("Unable to pretty-print expected JSON: %v\n%s", err, expectedJSON)
		} else {
			// We can't use green() here because %#v prints prettyExpected as a byte array literal, which
			// is... unhelpful. Converting it to a string first leaves "\n" uninterpreted for some reason.
			t.Logf("Expected JSON:\n%s%s%s", greenCode, prettyExpected, resetCode)
		}

		prettyActual, err := json.MarshalIndent(actual, "", "  ")
		if err != nil {
			t.Logf("Unable to pretty-print actual JSON: %v\n%#v", err, actual)
		} else {
			// We can't use yellow() for the same reason.
			t.Logf("Actual JSON:\n%s%s%s", yellowCode, prettyActual, resetCode)
		}

		return false
	}
	return true
}

示例#25
文件: dump_trace.go 项目: philips/earthquake

func dumpJSONAction(i int, act *Action) {
	if act.ActionType != "_JSON" {
		panic(fmt.Errorf("bad action %s", act))
	}
	ev := act.Evt
	actJSONMap := act.ToJSONMap()
	evJSONMap := ev.ToJSONMap()
	fmt.Printf("%d %s for @ %s: %s, %s\n",
		i,
		actJSONMap["class"],
		ev.ArrivedTime.Local().Format(time.UnixDate), ev.EntityId, evJSONMap["class"])
	if actJSONMap["class"] != "AcceptEventAction" {
		// printing AcceptEventAction is too verbose
		actPrettyStr, err := json.MarshalIndent(actJSONMap, "", "\t")
		if err != nil {
			panic(err)
		}
		fmt.Printf("%s\n", actPrettyStr)
	}
	evPrettyStr, err := json.MarshalIndent(evJSONMap, "", "\t")
	if err != nil {
		panic(err)
	}
	fmt.Printf("%s\n", evPrettyStr)
}

示例#26
文件: routes_test.go 项目: crackcomm/renderer

func TestRoutesUnmarshal(t *testing.T) {
	m := make(routesFile)
	err := yaml.Unmarshal([]byte(data), &m)
	if err != nil {
		t.Fatal(err)
	}

	r, err := m.toRoutes()
	if err != nil {
		t.Fatal(err)
	}

	if !reflect.DeepEqual(r, expected) {
		gotbody, e := json.MarshalIndent(r.ToStringMap(), "", "  ")
		if e != nil {
			t.Fatal(e)
		}
		expbody, e := json.MarshalIndent(expected.ToStringMap(), "", "  ")
		if e != nil {
			t.Fatal(e)
		}
		t.Errorf("Got: \n%s\n Expected: \n%s\n", gotbody, expbody)
	}

	t.Logf("Got: %#v", r)

	_, err = r.Construct()
	if err != nil {
		t.Fatal(err)
	}
}

示例#27
文件: peek.go 项目: hanjin8307/circuit

// circuit peek /X1234/hola/charlie
func peek(x *cli.Context) {
	defer func() {
		if r := recover(); r != nil {
			fatalf("error, likely due to missing server or misspelled anchor: %v", r)
		}
	}()
	c := dial(x)
	args := x.Args()
	if len(args) != 1 {
		fatalf("peek needs one anchor argument")
	}
	w, _ := parseGlob(args[0])
	switch t := c.Walk(w).Get().(type) {
	case client.Server:
		buf, _ := json.MarshalIndent(t.Peek(), "", "\t")
		fmt.Println(string(buf))
	case client.Chan:
		buf, _ := json.MarshalIndent(t.Stat(), "", "\t")
		fmt.Println(string(buf))
	case client.Proc:
		buf, _ := json.MarshalIndent(t.Peek(), "", "\t")
		fmt.Println(string(buf))
	case client.Subscription:
		buf, _ := json.MarshalIndent(t.Peek(), "", "\t")
		fmt.Println(string(buf))
	case nil:
		buf, _ := json.MarshalIndent(nil, "", "\t")
		fmt.Println(string(buf))
	default:
		fatalf("unknown element")
	}
}

示例#28
文件: messages.go 项目: ModelRocket/openstorage

func fmtOutput(c *cli.Context, format *Format) {
	jsonOut := c.GlobalBool("json")
	outFd := os.Stdout

	if format.Err != "" {
		outFd = os.Stderr
	}

	if jsonOut {
		b, _ := json.MarshalIndent(format, "", " ")
		fmt.Fprintf(outFd, "%+v\n", string(b))
		return
	}

	if format.Err == "" {
		if format.Result == nil {
			for _, v := range format.UUID {
				fmt.Fprintln(outFd, v)
			}
			return
		}
		b, _ := json.MarshalIndent(format.Result, "", " ")
		fmt.Fprintf(outFd, "%+v\n", string(b))
		return
	}

	if format.Desc != "" {
		fmt.Fprintf(outFd, "%s: %v - %s\n", format.Cmd, format.Err,
			format.Desc)
		return
	}

	fmt.Fprintf(outFd, "%s: %v\n", format.Cmd, format.Err)
}

示例#29
文件: env.go 项目: yingkitw/cli

func (cmd *Env) displaySystemiAndAppProvidedEnvironment(env map[string]interface{}, app map[string]interface{}) {
	var vcapServices string
	var vcapApplication string

	servicesAsMap, ok := env["VCAP_SERVICES"].(map[string]interface{})
	if ok && len(servicesAsMap) > 0 {
		jsonBytes, err := json.MarshalIndent(env, "", " ")
		if err != nil {
			cmd.ui.Failed(err.Error())
		}
		vcapServices = string(jsonBytes)
	}

	applicationAsMap, ok := app["VCAP_APPLICATION"].(map[string]interface{})
	if ok && len(applicationAsMap) > 0 {
		jsonBytes, err := json.MarshalIndent(app, "", " ")
		if err != nil {
			cmd.ui.Failed(err.Error())
		}
		vcapApplication = string(jsonBytes)
	}

	if len(vcapServices) == 0 && len(vcapApplication) == 0 {
		cmd.ui.Say(T("No system-provided env variables have been set"))
		return
	}

	cmd.ui.Say(terminal.EntityNameColor(T("System-Provided:")))

	cmd.ui.Say(vcapServices)
	cmd.ui.Say("")
	cmd.ui.Say(vcapApplication)
}

示例#30
文件: jwt_test.go 项目: nabeken/go-jwx

func TestClaimSet(t *testing.T) {
	c1 := NewClaimSet()
	c1.Set("nonce", "AbCdEfG")
	c1.Set("sub", "[email protected]")
	c1.Set("iat", time.Now().Unix())

	jsonbuf1, err := json.MarshalIndent(c1, "", "  ")
	if !assert.NoError(t, err, "JSON marshal should succeed") {
		return
	}
	t.Logf("%s", jsonbuf1)

	c2 := NewClaimSet()
	if !assert.NoError(t, json.Unmarshal(jsonbuf1, c2), "JSON unmarshal should succeed") {
		return
	}

	jsonbuf2, err := json.MarshalIndent(c2, "", "  ")
	if !assert.NoError(t, err, "JSON marshal should succeed") {
		return
	}
	t.Logf("%s", jsonbuf2)

	if !assert.Equal(t, c1, c2, "Claim sets match") {
		return
	}
}

展开阅读全文