Golang yaml.v2 marshals an array as a sequence
Given the following YAML:
array.test: ["val1", "val2", "val3"]
I Unmarshal it using gopkg.in/yaml.v2 into a map[string]interface
. Then I get a single key whose value is an array of 3 values.
When I then Marshal it again to YAML, the resulting YAML looks like this:
array.test:
- val1
- val2
- val3
The array was actually marshaled as a sequence instead of an array.
This is the entire GoLang code:
func main()
data := `array.test: ["val1", "val2", "val3"]`
conf := make(map[string]interface)
yaml.Unmarshal(byte(data), conf)
data2, _ := yaml.Marshal(conf)
fmt.Printf("%sn", string(data2))
How can I overcome this issue?
go yaml marshalling unmarshalling
add a comment |
Given the following YAML:
array.test: ["val1", "val2", "val3"]
I Unmarshal it using gopkg.in/yaml.v2 into a map[string]interface
. Then I get a single key whose value is an array of 3 values.
When I then Marshal it again to YAML, the resulting YAML looks like this:
array.test:
- val1
- val2
- val3
The array was actually marshaled as a sequence instead of an array.
This is the entire GoLang code:
func main()
data := `array.test: ["val1", "val2", "val3"]`
conf := make(map[string]interface)
yaml.Unmarshal(byte(data), conf)
data2, _ := yaml.Marshal(conf)
fmt.Printf("%sn", string(data2))
How can I overcome this issue?
go yaml marshalling unmarshalling
Both YAML's you show have sequences, and are semantically equivalent. Your first YAML uses a flow-style sequence and the second example uses a block-style sequence. There is no such thing as an array in YAML.
– Anthon
Nov 11 at 19:10
OK so is there any way to force the marshaller to use flow-style, without having to define a specific struct?
– Forepick
Nov 11 at 23:16
No. There are libraries that allow all-flow-style, all-block-style, or outmost-collection-flow-style, but not yaml.v2
– Anthon
Nov 12 at 7:50
Can you recommend one?
– Forepick
Nov 12 at 17:11
add a comment |
Given the following YAML:
array.test: ["val1", "val2", "val3"]
I Unmarshal it using gopkg.in/yaml.v2 into a map[string]interface
. Then I get a single key whose value is an array of 3 values.
When I then Marshal it again to YAML, the resulting YAML looks like this:
array.test:
- val1
- val2
- val3
The array was actually marshaled as a sequence instead of an array.
This is the entire GoLang code:
func main()
data := `array.test: ["val1", "val2", "val3"]`
conf := make(map[string]interface)
yaml.Unmarshal(byte(data), conf)
data2, _ := yaml.Marshal(conf)
fmt.Printf("%sn", string(data2))
How can I overcome this issue?
go yaml marshalling unmarshalling
Given the following YAML:
array.test: ["val1", "val2", "val3"]
I Unmarshal it using gopkg.in/yaml.v2 into a map[string]interface
. Then I get a single key whose value is an array of 3 values.
When I then Marshal it again to YAML, the resulting YAML looks like this:
array.test:
- val1
- val2
- val3
The array was actually marshaled as a sequence instead of an array.
This is the entire GoLang code:
func main()
data := `array.test: ["val1", "val2", "val3"]`
conf := make(map[string]interface)
yaml.Unmarshal(byte(data), conf)
data2, _ := yaml.Marshal(conf)
fmt.Printf("%sn", string(data2))
How can I overcome this issue?
go yaml marshalling unmarshalling
go yaml marshalling unmarshalling
edited Nov 11 at 19:08
Anthon
28.5k1693144
28.5k1693144
asked Nov 11 at 15:38
Forepick
1137
1137
Both YAML's you show have sequences, and are semantically equivalent. Your first YAML uses a flow-style sequence and the second example uses a block-style sequence. There is no such thing as an array in YAML.
– Anthon
Nov 11 at 19:10
OK so is there any way to force the marshaller to use flow-style, without having to define a specific struct?
– Forepick
Nov 11 at 23:16
No. There are libraries that allow all-flow-style, all-block-style, or outmost-collection-flow-style, but not yaml.v2
– Anthon
Nov 12 at 7:50
Can you recommend one?
– Forepick
Nov 12 at 17:11
add a comment |
Both YAML's you show have sequences, and are semantically equivalent. Your first YAML uses a flow-style sequence and the second example uses a block-style sequence. There is no such thing as an array in YAML.
– Anthon
Nov 11 at 19:10
OK so is there any way to force the marshaller to use flow-style, without having to define a specific struct?
– Forepick
Nov 11 at 23:16
No. There are libraries that allow all-flow-style, all-block-style, or outmost-collection-flow-style, but not yaml.v2
– Anthon
Nov 12 at 7:50
Can you recommend one?
– Forepick
Nov 12 at 17:11
Both YAML's you show have sequences, and are semantically equivalent. Your first YAML uses a flow-style sequence and the second example uses a block-style sequence. There is no such thing as an array in YAML.
– Anthon
Nov 11 at 19:10
Both YAML's you show have sequences, and are semantically equivalent. Your first YAML uses a flow-style sequence and the second example uses a block-style sequence. There is no such thing as an array in YAML.
– Anthon
Nov 11 at 19:10
OK so is there any way to force the marshaller to use flow-style, without having to define a specific struct?
– Forepick
Nov 11 at 23:16
OK so is there any way to force the marshaller to use flow-style, without having to define a specific struct?
– Forepick
Nov 11 at 23:16
No. There are libraries that allow all-flow-style, all-block-style, or outmost-collection-flow-style, but not yaml.v2
– Anthon
Nov 12 at 7:50
No. There are libraries that allow all-flow-style, all-block-style, or outmost-collection-flow-style, but not yaml.v2
– Anthon
Nov 12 at 7:50
Can you recommend one?
– Forepick
Nov 12 at 17:11
Can you recommend one?
– Forepick
Nov 12 at 17:11
add a comment |
3 Answers
3
active
oldest
votes
This one helped me in the same case as you.
package main
import (
"fmt"
"log"
"gopkg.in/yaml.v2"
)
var data = `
a: Easy!
b:
c: 2
d.test: ["d1", "d2"]
`
// Note: struct fields must be public in order for unmarshal to
// correctly populate the data.
type T struct
A string
B struct
RenamedC int `yaml:"c"`
DTest string `yaml:"d.test,flow"`
func main()
// if we use struct containing yaml encoding for yaml formated string
t := T
err := yaml.Unmarshal(byte(data), &t)
if err != nil
log.Fatalf("error: %v", err)
fmt.Printf("--- t after unmarshal:n%vnn", t)
d, err := yaml.Marshal(&t)
if err != nil
log.Fatalf("error: %v", err)
fmt.Printf("--- t after marshal:n%snn", string(d))
Ref: https://github.com/go-yaml/yaml
thanks for the answer but this is not my case. I'm trying to marshal a map, not a predefined struct.
– Forepick
Nov 11 at 17:21
@Forepick: As every answer indicates, using a struct is the only way to do what you're asking.
– Flimzy
Nov 12 at 12:28
add a comment |
Use flow
in struct field tag format, to indicate you desire this behavior. But, of course, this requires unmarshaling to a struct, not to a map.
add a comment |
Flow tag allows you to choose the representation of an array in yaml
package main
import (
"fmt"
"gopkg.in/yaml.v2"
)
type Conf struct
Test string `yaml:"array.test,flow"`
func main()
data := `array.test: ["val1", "val2", "val3"]`
var conf Conf
yaml.Unmarshal(byte(data), &conf)
data2, _ := yaml.Marshal(conf)
fmt.Printf("%sn", string(data2))
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250313%2fgolang-yaml-v2-marshals-an-array-as-a-sequence%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
This one helped me in the same case as you.
package main
import (
"fmt"
"log"
"gopkg.in/yaml.v2"
)
var data = `
a: Easy!
b:
c: 2
d.test: ["d1", "d2"]
`
// Note: struct fields must be public in order for unmarshal to
// correctly populate the data.
type T struct
A string
B struct
RenamedC int `yaml:"c"`
DTest string `yaml:"d.test,flow"`
func main()
// if we use struct containing yaml encoding for yaml formated string
t := T
err := yaml.Unmarshal(byte(data), &t)
if err != nil
log.Fatalf("error: %v", err)
fmt.Printf("--- t after unmarshal:n%vnn", t)
d, err := yaml.Marshal(&t)
if err != nil
log.Fatalf("error: %v", err)
fmt.Printf("--- t after marshal:n%snn", string(d))
Ref: https://github.com/go-yaml/yaml
thanks for the answer but this is not my case. I'm trying to marshal a map, not a predefined struct.
– Forepick
Nov 11 at 17:21
@Forepick: As every answer indicates, using a struct is the only way to do what you're asking.
– Flimzy
Nov 12 at 12:28
add a comment |
This one helped me in the same case as you.
package main
import (
"fmt"
"log"
"gopkg.in/yaml.v2"
)
var data = `
a: Easy!
b:
c: 2
d.test: ["d1", "d2"]
`
// Note: struct fields must be public in order for unmarshal to
// correctly populate the data.
type T struct
A string
B struct
RenamedC int `yaml:"c"`
DTest string `yaml:"d.test,flow"`
func main()
// if we use struct containing yaml encoding for yaml formated string
t := T
err := yaml.Unmarshal(byte(data), &t)
if err != nil
log.Fatalf("error: %v", err)
fmt.Printf("--- t after unmarshal:n%vnn", t)
d, err := yaml.Marshal(&t)
if err != nil
log.Fatalf("error: %v", err)
fmt.Printf("--- t after marshal:n%snn", string(d))
Ref: https://github.com/go-yaml/yaml
thanks for the answer but this is not my case. I'm trying to marshal a map, not a predefined struct.
– Forepick
Nov 11 at 17:21
@Forepick: As every answer indicates, using a struct is the only way to do what you're asking.
– Flimzy
Nov 12 at 12:28
add a comment |
This one helped me in the same case as you.
package main
import (
"fmt"
"log"
"gopkg.in/yaml.v2"
)
var data = `
a: Easy!
b:
c: 2
d.test: ["d1", "d2"]
`
// Note: struct fields must be public in order for unmarshal to
// correctly populate the data.
type T struct
A string
B struct
RenamedC int `yaml:"c"`
DTest string `yaml:"d.test,flow"`
func main()
// if we use struct containing yaml encoding for yaml formated string
t := T
err := yaml.Unmarshal(byte(data), &t)
if err != nil
log.Fatalf("error: %v", err)
fmt.Printf("--- t after unmarshal:n%vnn", t)
d, err := yaml.Marshal(&t)
if err != nil
log.Fatalf("error: %v", err)
fmt.Printf("--- t after marshal:n%snn", string(d))
Ref: https://github.com/go-yaml/yaml
This one helped me in the same case as you.
package main
import (
"fmt"
"log"
"gopkg.in/yaml.v2"
)
var data = `
a: Easy!
b:
c: 2
d.test: ["d1", "d2"]
`
// Note: struct fields must be public in order for unmarshal to
// correctly populate the data.
type T struct
A string
B struct
RenamedC int `yaml:"c"`
DTest string `yaml:"d.test,flow"`
func main()
// if we use struct containing yaml encoding for yaml formated string
t := T
err := yaml.Unmarshal(byte(data), &t)
if err != nil
log.Fatalf("error: %v", err)
fmt.Printf("--- t after unmarshal:n%vnn", t)
d, err := yaml.Marshal(&t)
if err != nil
log.Fatalf("error: %v", err)
fmt.Printf("--- t after marshal:n%snn", string(d))
Ref: https://github.com/go-yaml/yaml
edited Nov 11 at 17:20
answered Nov 11 at 16:18
Shudipta Sharma
1,023312
1,023312
thanks for the answer but this is not my case. I'm trying to marshal a map, not a predefined struct.
– Forepick
Nov 11 at 17:21
@Forepick: As every answer indicates, using a struct is the only way to do what you're asking.
– Flimzy
Nov 12 at 12:28
add a comment |
thanks for the answer but this is not my case. I'm trying to marshal a map, not a predefined struct.
– Forepick
Nov 11 at 17:21
@Forepick: As every answer indicates, using a struct is the only way to do what you're asking.
– Flimzy
Nov 12 at 12:28
thanks for the answer but this is not my case. I'm trying to marshal a map, not a predefined struct.
– Forepick
Nov 11 at 17:21
thanks for the answer but this is not my case. I'm trying to marshal a map, not a predefined struct.
– Forepick
Nov 11 at 17:21
@Forepick: As every answer indicates, using a struct is the only way to do what you're asking.
– Flimzy
Nov 12 at 12:28
@Forepick: As every answer indicates, using a struct is the only way to do what you're asking.
– Flimzy
Nov 12 at 12:28
add a comment |
Use flow
in struct field tag format, to indicate you desire this behavior. But, of course, this requires unmarshaling to a struct, not to a map.
add a comment |
Use flow
in struct field tag format, to indicate you desire this behavior. But, of course, this requires unmarshaling to a struct, not to a map.
add a comment |
Use flow
in struct field tag format, to indicate you desire this behavior. But, of course, this requires unmarshaling to a struct, not to a map.
Use flow
in struct field tag format, to indicate you desire this behavior. But, of course, this requires unmarshaling to a struct, not to a map.
answered Nov 11 at 15:45
Flimzy
37.2k96496
37.2k96496
add a comment |
add a comment |
Flow tag allows you to choose the representation of an array in yaml
package main
import (
"fmt"
"gopkg.in/yaml.v2"
)
type Conf struct
Test string `yaml:"array.test,flow"`
func main()
data := `array.test: ["val1", "val2", "val3"]`
var conf Conf
yaml.Unmarshal(byte(data), &conf)
data2, _ := yaml.Marshal(conf)
fmt.Printf("%sn", string(data2))
add a comment |
Flow tag allows you to choose the representation of an array in yaml
package main
import (
"fmt"
"gopkg.in/yaml.v2"
)
type Conf struct
Test string `yaml:"array.test,flow"`
func main()
data := `array.test: ["val1", "val2", "val3"]`
var conf Conf
yaml.Unmarshal(byte(data), &conf)
data2, _ := yaml.Marshal(conf)
fmt.Printf("%sn", string(data2))
add a comment |
Flow tag allows you to choose the representation of an array in yaml
package main
import (
"fmt"
"gopkg.in/yaml.v2"
)
type Conf struct
Test string `yaml:"array.test,flow"`
func main()
data := `array.test: ["val1", "val2", "val3"]`
var conf Conf
yaml.Unmarshal(byte(data), &conf)
data2, _ := yaml.Marshal(conf)
fmt.Printf("%sn", string(data2))
Flow tag allows you to choose the representation of an array in yaml
package main
import (
"fmt"
"gopkg.in/yaml.v2"
)
type Conf struct
Test string `yaml:"array.test,flow"`
func main()
data := `array.test: ["val1", "val2", "val3"]`
var conf Conf
yaml.Unmarshal(byte(data), &conf)
data2, _ := yaml.Marshal(conf)
fmt.Printf("%sn", string(data2))
answered Nov 11 at 15:56
iHelos
614
614
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250313%2fgolang-yaml-v2-marshals-an-array-as-a-sequence%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Both YAML's you show have sequences, and are semantically equivalent. Your first YAML uses a flow-style sequence and the second example uses a block-style sequence. There is no such thing as an array in YAML.
– Anthon
Nov 11 at 19:10
OK so is there any way to force the marshaller to use flow-style, without having to define a specific struct?
– Forepick
Nov 11 at 23:16
No. There are libraries that allow all-flow-style, all-block-style, or outmost-collection-flow-style, but not yaml.v2
– Anthon
Nov 12 at 7:50
Can you recommend one?
– Forepick
Nov 12 at 17:11