本文整理匯總了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)
}
示例2: JSONToYAMLOrDie
func JSONToYAMLOrDie(in []byte) []byte {
data, err := yaml.JSONToYAML(in)
if err != nil {
panic(err)
}
return data
}
示例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)))
}
}
示例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
}
示例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
}
示例6: json2Yaml
func json2Yaml(json []byte) (s string) {
if y, err := yaml.JSONToYAML(json); err != nil {
s = err.Error()
} else {
s = string(y)
}
return
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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))
}
}
示例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))
}
示例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
}