I think this might be a problem with YamlDotNet but that does seem somewhat unlikely given that it's quite a well used library so figured I'd ask.
I have this bit of yaml:
Methods:
- Name: "AddPgfPlotWithAxes"
Arguments:
- Name: "axisType"
Type: "AxisType"
- Name: "options"
Type: "AxisOptions?"
CanTransitionTo: &AxisSettingsCircular
- "AddPlot"
- "SetXLabel"
- "SetYLabel"
- "SetXMin"
- "SetYMin"
- "SetXMax"
- "SetYMax"
- "SetMinorXTickNumber"
- "SetMinorYTickNumber"
- "SetXTicks"
- "SetYTicks"
- "SetGrid"
- Name: "SetXLabel"
Arguments:
- Name: "label"
Type: "string?"
CanTransitionTo:
<<: *AxisSettingsCircular
I want to deserialize it onto this class:
internal record FluidApiMethodDefinition
{
public required string Name { get; init; }
public string? ReturnType { get; init; }
public List<string> CanTransitionTo { get; init; } = new();
public List<FluidApiArgumentDefinition> Arguments { get; init; } = new();
}
I think I should be able to with this snippet of code:
IDeserializer deserializer = new DeserializerBuilder().WithNamingConvention(NullNamingConvention.Instance).Build();
FluidApiMethodDefinition methodDefinition = deserializer.Deserialize<FluidApiMethodDefinition[]>(_rawYml);
However, I get this error:
(Line: 44, Col: 7, Idx: 980) - (Line: 44, Col: 7, Idx: 980): Expected 'SequenceStart', got 'MappingStart' (at Line: 44, Col: 7, Idx: 980).
Line 44 in this case corresponds to the line with <<:
on it, I've just not included the full yaml, or the nesting (Methods
) exists inside another class) for brevity.
I've verified that the yaml is valid and parses as I'd expect but maybe I'm doing something else wrong here that's YamlDotNet specific?
The syntax I was trying to use is actually not supported according to the YAML spec. You can merge mappings, not sequences.
However, what I was wanting to achieve can be achieved with:
Methods:
- Name: "AddPgfPlotWithAxes"
Arguments:
- Name: "axisType"
Type: "AxisType"
- Name: "options"
Type: "AxisOptions?"
CanTransitionTo: &AxisSettingsCircular
- "AddPlot"
- "SetXLabel"
- "SetYLabel"
- "SetXMin"
- "SetYMin"
- "SetXMax"
- "SetYMax"
- "SetMinorXTickNumber"
- "SetMinorYTickNumber"
- "SetXTicks"
- "SetYTicks"
- "SetGrid"
- Name: "SetXLabel"
Arguments:
- Name: "label"
Type: "string?"
CanTransitionTo: *AxisSettingsCircular