3

With YamlDotNet I'm getting inconsistent serialization. I'm serializing with a basic serializer:

new YamlDotNet.Serialization.SerializerBuilder()
.DisableAliases()
.ConfigureDefaultValuesHandling(DefaultValuesHandling.OmitDefaults)
.Build()
.Serialize(target)

When I serialize a simple anonymous object it works more or less correctly, expanding multi-line strings with the YAML Multiline Guide. It has double line feeds, which seems like a bug similar to that reported in this question, but is otherwise correct.

So if I serialize:

    new {
    A = 1,
    B = "Testing",
    Group1 = new {
        Field1 = "Test 1\r\n\"This\"",
        Field2 = "Test 2\r\n'That'"
    }
}.ToYaml()

I get just about what I expect (other than the double newlines):

A: 1
B: Testing
Group1:
  Field1: >-
    Test 1

    "This"
  Field2: >-
    Test 2

    'That'

So far so good. When I try to serialize a real object that looks like this in JSON:

    {
  "Parameters": {
    "CommitmentSpecIds": {
      "Choices": {
        "1060": "Location"
      }
    },
    "DisplayValue": {
      "Text": "LOC: {{Question.Schedule->LocationName}}"
    },
    "Tooltip": {
      "Text": "Staff: {{Question.Schedule->StaffName}}\nAppointment: {{Question.Schedule->ScheduleDate@d}}"
    }
  }
}

I end up with the lines being double-quoted if they have any newlines, just as they do in JSON.

Parameters:
  CommitmentSpecIds:
    Choices:
      1060: Location
  DisplayValue:
    Text: 'LOC: {{Question.Schedule->LocationName}}'
  Tooltip:
    Text: "Staff: {{Question.Schedule->StaffName}}\nAppointment: {{Question.Schedule->ScheduleDate@d}}"

I've searched a couple hours, but assume I'm missing something simple.

How do I force Folded Block Scalar Style for all strings with newlines.

Wade Hatler
  • 1,785
  • 20
  • 18

0 Answers0