當前位置: 首頁>>代碼示例>>Golang>>正文


Golang yaml.JSONToYAML函數代碼示例

本文整理匯總了Golang中github.com/ghodss/yaml.JSONToYAML函數的典型用法代碼示例。如果您正苦於以下問題:Golang JSONToYAML函數的具體用法?Golang JSONToYAML怎麽用?Golang JSONToYAML使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了JSONToYAML函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: EncodeToStream

// EncodeToStream serializes the provided object to the given writer. Overrides is ignored.
func (s *Serializer) EncodeToStream(obj runtime.Object, w io.Writer, overrides ...unversioned.GroupVersion) error {
	if s.yaml {
		json, err := json.Marshal(obj)
		if err != nil {
			return err
		}
		data, err := yaml.JSONToYAML(json)
		if err != nil {
			return err
		}
		_, err = w.Write(data)
		return err
	}

	if s.pretty {
		data, err := json.MarshalIndent(obj, "", "  ")
		if err != nil {
			return err
		}
		_, err = w.Write(data)
		return err
	}
	encoder := json.NewEncoder(w)
	return encoder.Encode(obj)
}
開發者ID:Clarifai,項目名稱:kubernetes,代碼行數:26,代碼來源:json.go

示例2: JSONToYAMLOrDie

func JSONToYAMLOrDie(in []byte) []byte {
	data, err := yaml.JSONToYAML(in)
	if err != nil {
		panic(err)
	}
	return data
}
開發者ID:hortonworks,項目名稱:kubernetes-yarn,代碼行數:7,代碼來源:builder_test.go

示例3: testObjects

func testObjects(t *testing.T, list *api.List, fixtureFilename string) {
	filename := filepath.Join("../../../../test/fixtures/bootstrappolicy", fixtureFilename)
	expectedYAML, err := ioutil.ReadFile(filename)
	if err != nil {
		t.Fatal(err)
	}

	if err := runtime.EncodeList(api.Codecs.LegacyCodec(v1.SchemeGroupVersion), list.Items); err != nil {
		t.Fatal(err)
	}

	jsonData, err := runtime.Encode(api.Codecs.LegacyCodec(v1.SchemeGroupVersion), list)
	if err != nil {
		t.Fatal(err)
	}
	yamlData, err := yaml.JSONToYAML(jsonData)
	if err != nil {
		t.Fatal(err)
	}
	if string(yamlData) != string(expectedYAML) {
		fmt.Println("Bootstrap policy data does not match the test fixture in " + filename)
		fmt.Println("If the change is expected, update the fixture with this bootstrap policy data:")
		fmt.Println("-----------------------------------")
		fmt.Println(string(yamlData))
		fmt.Println("-----------------------------------")
		t.Errorf("Diff between bootstrap data and fixture data in %s:\n-------------\n%s", filename, util.StringDiff(string(yamlData), string(expectedYAML)))
	}
}
開發者ID:rrati,項目名稱:origin,代碼行數:28,代碼來源:policy_test.go

示例4: jsonToYAML

func jsonToYAML(j []byte) []byte {
	y, err := yaml.JSONToYAML(j)
	if err != nil {
		panic(fmt.Sprintf("json to yaml failed: %v", err))
	}
	return y
}
開發者ID:SivagnanamCiena,項目名稱:calico-kubernetes,代碼行數:7,代碼來源:patch_test.go

示例5: jsonToYAML

func jsonToYAML(j []byte) ([]byte, error) {
	y, err := yaml.JSONToYAML(j)
	if err != nil {
		return nil, fmt.Errorf("json to yaml failed: %v\n%v\n", err, j)
	}

	return y, nil
}
開發者ID:40a,項目名稱:bootkube,代碼行數:8,代碼來源:patch_test.go

示例6: json2Yaml

func json2Yaml(json []byte) (s string) {
	if y, err := yaml.JSONToYAML(json); err != nil {
		s = err.Error()
	} else {
		s = string(y)
	}
	return
}
開發者ID:ujanssen,項目名稱:learning-go-lang,代碼行數:8,代碼來源:json2yaml.go

示例7: Write

// Write serializes the config to yaml.
// Encapsulates serialization without assuming the destination is a file.
func Write(config clientcmdapi.Config) ([]byte, error) {
	json, err := clientcmdlatest.Codec.Encode(&config)
	if err != nil {
		return nil, err
	}
	content, err := yaml.JSONToYAML(json)
	if err != nil {
		return nil, err
	}
	return content, nil
}
開發者ID:brorhie,項目名稱:panamax-kubernetes-adapter-go,代碼行數:13,代碼來源:loader.go

示例8: JsonToYaml

func JsonToYaml(inputBytes []byte) []byte {
	buffer, err := yaml.JSONToYAML(inputBytes)

	if err != nil {
		fmt.Println("JSON -> YAML convert error.")
		fmt.Printf("err: %v\n", err)
		os.Exit(1)
	}

	return buffer
}
開發者ID:y13i,項目名稱:j2y,代碼行數:11,代碼來源:json_to_yaml.go

示例9: writeYAML

func writeYAML(obj runtime.Object) ([]byte, error) {
	json, err := Codec.Encode(obj)
	if err != nil {
		return nil, err
	}

	content, err := yaml.JSONToYAML(json)
	if err != nil {
		return nil, err
	}
	return content, err
}
開發者ID:Vitogee,項目名稱:origin,代碼行數:12,代碼來源:types_test.go

示例10: ExampleMergingSomeWithConflict

func ExampleMergingSomeWithConflict() {
	commandLineFile, _ := ioutil.TempFile("", "")
	defer os.Remove(commandLineFile.Name())
	envVarFile, _ := ioutil.TempFile("", "")
	defer os.Remove(envVarFile.Name())

	WriteToFile(testConfigAlfa, commandLineFile.Name())
	WriteToFile(testConfigConflictAlfa, envVarFile.Name())

	loadingRules := ClientConfigLoadingRules{
		CommandLinePath: commandLineFile.Name(),
		EnvVarPath:      envVarFile.Name(),
	}

	mergedConfig, err := loadingRules.Load()

	json, err := clientcmdlatest.Codec.Encode(mergedConfig)
	if err != nil {
		fmt.Printf("Unexpected error: %v", err)
	}
	output, err := yaml.JSONToYAML(json)
	if err != nil {
		fmt.Printf("Unexpected error: %v", err)
	}

	fmt.Printf("%v", string(output))
	// Output:
	// apiVersion: v1
	// clusters:
	// - cluster:
	//     server: http://cow.org:8080
	//   name: cow-cluster
	// - cluster:
	//     insecure-skip-tls-verify: true
	//     server: http://donkey.org:8080
	//   name: donkey-cluster
	// contexts:
	// - context:
	//     cluster: cow-cluster
	//     namespace: hammer-ns
	//     user: red-user
	//   name: federal-context
	// current-context: federal-context
	// kind: Config
	// preferences: {}
	// users:
	// - name: red-user
	//   user:
	//     token: red-token
	// - name: yellow-user
	//   user:
	//     token: yellow-token
}
開發者ID:brorhie,項目名稱:panamax-kubernetes-adapter-go,代碼行數:53,代碼來源:loader_test.go

示例11: writeYAML

// writeYAML writes the specified object to a byte array as yaml.
func writeYAML(obj runtime.Object) ([]byte, error) {
	json, err := runtime.Encode(api.Codecs.LegacyCodec(), obj)
	if err != nil {
		return nil, err
	}

	content, err := yaml.JSONToYAML(json)
	if err != nil {
		return nil, err
	}
	return content, err
}
開發者ID:kubernetes,項目名稱:kubernetes,代碼行數:13,代碼來源:config.go

示例12: writeYAML

func writeYAML(obj runtime.Object) ([]byte, error) {

	json, err := runtime.Encode(serializer.NewCodecFactory(internal.Scheme).LegacyCodec(v1.SchemeGroupVersion), obj)
	if err != nil {
		return nil, err
	}

	content, err := yaml.JSONToYAML(json)
	if err != nil {
		return nil, err
	}
	return content, err
}
開發者ID:richm,項目名稱:origin,代碼行數:13,代碼來源:types_test.go

示例13: CmdShow

// CmdShow implements the 'show' command
func CmdShow(c *cli.Context) {
	utils.CheckRequiredFlags(c, []string{"kubeware"})

	kubeware := os.Getenv("KDEPLOY_KUBEWARE")
	localKubePath, err := fetchers.Fetch(kubeware)
	if err != nil {
		log.Fatal(fmt.Errorf("Could not fetch kubeware: '%s' (%v)", kubeware, err))
	}

	log.Debugf("Going to parse kubeware in %s", localKubePath)

	metadata := template.ParseMetadata(localKubePath)
	defaults, err := metadata.AttributeDefaults()
	utils.CheckError(err)

	log.Debugf("Building attributes")
	attributes := template.BuildAttributes(os.Getenv("KDEPLOY_ATTRIBUTE"), defaults)

	log.Debugf("Parsing services")
	servicesSpecs, err := metadata.ParseServices(attributes)
	utils.CheckError(err)

	log.Debugf("Parsing controllers")
	controllersSpecs, err := metadata.ParseControllers(attributes)
	utils.CheckError(err)

	// print resolved resources
	for _, s := range servicesSpecs {
		y, err := gyml.JSONToYAML([]byte(s))
		utils.CheckError(err)
		fmt.Println(string(y))
	}
	for _, c := range controllersSpecs {
		y, err := gyml.JSONToYAML([]byte(c))
		utils.CheckError(err)
		fmt.Println(string(y))
	}
}
開發者ID:flexiant,項目名稱:kdeploy,代碼行數:39,代碼來源:show.go

示例14: main

//go:generate goversion -major=1 -minor=0 -patch=0
func main() {

	bytes, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		log.Panicln(err)
	}

	json, err := yaml.JSONToYAML(bytes)
	if err != nil {
		log.Panicln(err)
	}

	print(string(json))
}
開發者ID:dearing,項目名稱:tools,代碼行數:15,代碼來源:main.go

示例15: Example_noMergingOnExplicitPaths

func Example_noMergingOnExplicitPaths() {
	commandLineFile, _ := ioutil.TempFile("", "")
	defer os.Remove(commandLineFile.Name())
	envVarFile, _ := ioutil.TempFile("", "")
	defer os.Remove(envVarFile.Name())

	WriteToFile(testConfigAlfa, commandLineFile.Name())
	WriteToFile(testConfigConflictAlfa, envVarFile.Name())

	loadingRules := ClientConfigLoadingRules{
		ExplicitPath: commandLineFile.Name(),
		Precedence:   []string{envVarFile.Name()},
	}

	mergedConfig, err := loadingRules.Load()

	json, err := runtime.Encode(clientcmdlatest.Codec, mergedConfig)
	if err != nil {
		fmt.Printf("Unexpected error: %v", err)
	}
	output, err := yaml.JSONToYAML(json)
	if err != nil {
		fmt.Printf("Unexpected error: %v", err)
	}

	fmt.Printf("%v", string(output))
	// Output:
	// apiVersion: v1
	// clusters:
	// - cluster:
	//     server: http://cow.org:8080
	//   name: cow-cluster
	// contexts:
	// - context:
	//     cluster: cow-cluster
	//     namespace: hammer-ns
	//     user: red-user
	//   name: federal-context
	// current-context: ""
	// kind: Config
	// preferences: {}
	// users:
	// - name: red-user
	//   user:
	//     token: red-token
}
開發者ID:ttysteale,項目名稱:kubernetes-api,代碼行數:46,代碼來源:loader_test.go


注:本文中的github.com/ghodss/yaml.JSONToYAML函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。