Golang yaml.v2 marshals an array as a sequence










0














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?










share|improve this question























  • 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















0














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?










share|improve this question























  • 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













0












0








0







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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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
















  • 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












3 Answers
3






active

oldest

votes


















1














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






share|improve this answer






















  • 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


















0














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.






share|improve this answer




























    0














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






    share|improve this answer




















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



      );













      draft saved

      draft discarded


















      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









      1














      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






      share|improve this answer






















      • 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















      1














      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






      share|improve this answer






















      • 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













      1












      1








      1






      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






      share|improve this answer














      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







      share|improve this answer














      share|improve this answer



      share|improve this answer








      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
















      • 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













      0














      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.






      share|improve this answer

























        0














        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.






        share|improve this answer























          0












          0








          0






          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.






          share|improve this answer












          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 15:45









          Flimzy

          37.2k96496




          37.2k96496





















              0














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






              share|improve this answer

























                0














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






                share|improve this answer























                  0












                  0








                  0






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






                  share|improve this answer












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







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 11 at 15:56









                  iHelos

                  614




                  614



























                      draft saved

                      draft discarded
















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      Use pre created SQLite database for Android project in kotlin

                      Darth Vader #20

                      Ondo