YamlToJsonConverter
Namespace: Meshmakers.Octo.Communication.Contracts.Serialization
Converts a YAML document into the equivalent , preserving scalar types so that a document is interpreted identically whether it arrives as YAML or as the equivalent JSON.
public static class YamlToJsonConverter
Inheritance Object → YamlToJsonConverter
Remarks:
This lives in the shared contracts assembly because pipeline definitions are authored in
YAML but validated against a JSON Schema, and every party that does so — the
Communication Controller's deploy-time validator and the MCP server's
validate_pipeline_definition tool — has to reach the same verdict on the same
document. Both previously hand-rolled the conversion and both got it wrong the same way
(AB#5240).
The obvious shortcut — Deserialize<object?> followed by a JSON-compatible
re-serialization — is what was wrong: untyped YamlDotNet deserialization yields every
scalar as a String, so numbers and booleans reach the schema as strings
and fail its number, integer and boolean types. This converter walks
the representation model instead, where the scalar style and tag needed to resolve the
type are still intact.
Scalars are resolved per the YAML 1.2 core schema, restricted to the types JSON itself
has: null, boolean, integer, float, string. A quoted, literal or folded scalar is always
a string, as is any scalar carrying an explicit !!str tag. Anything a plain scalar
cannot be resolved to losslessly stays a string, so the result is always a valid JSON
document that says what the author wrote.
The YAML 1.1 booleans (yes, no, on, off) are deliberately NOT
recognised: the runtime pipeline deserializer
(YamlPipelineConfigurationSerializer, which binds to typed properties) does not
accept them for a bool property either, so treating them as strings keeps
validation aligned with what actually loads.
Methods
ToJsonNode(String)
Parses and converts it to a .
public static JsonNode ToJsonNode(string yaml)
Parameters
yaml String
The YAML document.
Returns
JsonNode
The converted node, or null for input that holds no document at all (empty or
comment-only) or whose root resolves to YAML null.
Exceptions
T:YamlDotNet.Core.YamlException
The input is not well-formed YAML.
NotSupportedException
The input holds more than one document, nests deeper than 256 levels, or uses a mapping
key that is not a scalar and therefore has no JSON equivalent.
ToJsonNodeAutoDetect(String)
Converts a definition that may be either YAML or JSON. A document opening with {
or [ is tried as JSON first and re-read as YAML if that fails, because those
characters also open a YAML flow collection.
public static JsonNode ToJsonNodeAutoDetect(string definition)
Parameters
definition String
The definition document, in either format.
Returns
JsonNode
The converted node, or null when the input is empty or resolves to null.
Exceptions
T:System.Text.Json.JsonException
The input was detected as JSON but is not well-formed.
T:YamlDotNet.Core.YamlException
The input was detected as YAML but is not well-formed.
NotSupportedException
The YAML input has a shape with no JSON equivalent — see YamlToJsonConverter.ToJsonNode(String).