0

I'm using snakeYaml (snakeyaml-engine-2.1) to serialise a pojo. Atm the serilisation produces the following output

name: "OuterYamlElementName"
label: "OuterYamlElementLabel"
fields:
  label: "outerYamlFieldLabel"
  value: "outerYamlFieldValue"
  isRequired: true
  toggle: "+Test-Outer-Yaml-Field"

however I want the yaml to be generated like so

name: "OuterYamlElementName"
label: "OuterYamlElementLabel"
fields:
  - {label: "outerYamlFieldLabel", value: "outerYamlFieldValue", isRequired: true, toggle: "+Test-Outer-Yaml-Field"}

I know their is ways to customise the output produced via class DumpSettings within snake yaml through setters

setCanonical
setDefaultFlowStyle
etc

however I haven't been able to produce that output as of yet unfortunately. I'd like the output to come condensed as the Yaml produced is relatively big (the example above only includes a snippet hence the ask). In addition the second Yaml sample is deserialised by snake yaml so it should be able to produce the same output I would think. Would somebody know what settings are needed to create the structure as in the second example? Thanks in advance.

bhreinb
  • 181
  • 2
  • 13
  • Can you show us the POJO? –  Apr 22 '20 at 17:25
  • *In addition the second Yaml sample is deserialised by snake yaml so it should be able to produce the same output* -> That's an incorrect assumption, [see this question](https://stackoverflow.com/q/60891174/347964) for details. – flyx Apr 22 '20 at 18:09
  • Hi @flyx thanks for that link...that's an exhaustive but interesting read as to how yaml is parsed and dumped. – bhreinb Apr 23 '20 at 09:02

1 Answers1

0

I could not yet make snakeyaml-engine-2.1 to produce any result either.

However, I tried another YAML library (snakeyaml v.1.10) and it generated the following sample string (however, without double quotes):

!!com.example.demo.so.yaml.FormatYaml$Pojo
field: {label: outerYamlFieldLabel2, required: true, toggle: +Test-Outer-Yaml-Field2,
  value: outerYamlFieldValue2}
fields:
- {label: outerYamlFieldLabel, required: true, toggle: +Test-Outer-Yaml-Field, value: outerYamlFieldValue}
label: OuterYamlElementName
name: OuterYamlElementName
Nowhere Man
  • 19,170
  • 9
  • 17
  • 42
  • Hi thanks for your response...yes I was able to recreate something similiar with `v1.1.14` but was unable to figure from that the `DumpOptions` to re-create that unfortunately :(. I believe `DumpOptions` is v1 and `DumpSettings`is v2. – bhreinb Apr 23 '20 at 08:33