GuideIntroduction

Coming from System.Text.Json

Styml is System.Text.Json renamed for YAML. What carries over as it is, what YAML adds, and what behaves differently.

Styml is a clone of System.Text.Json from dotnet/runtime at tag v11.0.0, renamed and adapted to YAML 1.2. Its code, its tests and most of its documentation are System.Text.Json's, so code written for System.Text.Json reads the same with Yaml in place of Json:

System.Text.Json
var jsonOptions = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
string json = JsonSerializer.Serialize(service, jsonOptions);
Service? fromJson = JsonSerializer.Deserialize<Service>(json, jsonOptions);

// Styml
var yamlOptions = new YamlSerializerOptions { PropertyNamingPolicy = YamlNamingPolicy.CamelCase };
string yaml = YamlSerializer.Serialize(service, yamlOptions);
Service? fromYaml = YamlSerializer.Deserialize<Service>(yaml, yamlOptions);

NamesLink to this heading

Every public type and member is renamed the same way, and keeps its meaning:

System.Text.Json Styml
System.Text.Json namespace System.Text.Yaml
JsonSerializer, JsonSerializerOptions YamlSerializer, YamlSerializerOptions
Utf8JsonReader, Utf8JsonWriter Utf8YamlReader, Utf8YamlWriter
JsonDocument, JsonElement YamlDocument, YamlElement
JsonNode, JsonObject, JsonArray, JsonValue YamlNode, YamlObject, YamlArray, YamlValue
JsonConverter<T>, JsonConverterFactory YamlConverter<T>, YamlConverterFactory
JsonPropertyName, JsonIgnore, JsonInclude, JsonRequired... YamlPropertyName, YamlIgnore, YamlInclude, YamlRequired...
JsonDerivedType, JsonPolymorphic YamlDerivedType, YamlPolymorphic
JsonTypeInfo, DefaultJsonTypeInfoResolver YamlTypeInfo, DefaultYamlTypeInfoResolver
JsonSerializerContext, [JsonSerializable], [JsonSourceGenerationOptions] YamlSerializerContext, [YamlSerializable], [YamlSourceGenerationOptions]
JsonException YamlException

The package is Styml and its assembly Styml.dll, so an application can use both libraries at once.

JSON is YAMLLink to this heading

YAML 1.2 is a superset of JSON: Styml reads JSON text as it is, and writes JSON with WriteStyle = YamlWriterStyle.JsonCompatible. One serializer can read both formats:

var service = YamlSerializer.Deserialize<Service>("""
    {"Name": "web", "Image": "nginx:1.27", "Ports": [80, 443], "Environment": {"TIER": "front"}}
    """)!;
string yaml = YamlSerializer.Serialize(service);
string json = YamlSerializer.Serialize(service, new YamlSerializerOptions { WriteStyle = YamlWriterStyle.JsonCompatible });
Name: web
Image: nginx:1.27
Ports:
- 80
- 443
Environment:
  TIER: front
---
{"Name":"web","Image":"nginx:1.27","Ports":[80,443],"Environment":{"TIER":"front"}}

What YAML addsLink to this heading

  • Anchors and aliases, read everywhere and written by ReferenceHandler.Preserve in place of $id and $ref, with a bound on alias expansion: References.
  • Tags, tag-based polymorphism and tag mappings, which are explicit allow-lists: Polymorphism and tags.
  • Styles: block and flow collections, five scalar styles, comments written from [YamlComment]: Writing YAML and Scalar styles.
  • Schemas: the YAML 1.2 core schema by default, the JSON and failsafe schemas, and an opt-in YAML 1.1 schema: Schemas and typed reads.
  • Merge keys (<<), collection keys (? [a, b]: v) and streams of documents (---): Reading YAML.

What behaves differentlyLink to this heading

  • The writer writes block YAML by default. WriteStyle = YamlWriterStyle.JsonCompatible writes JSON, which WriteIndented formats as System.Text.Json does. Block YAML is always on several lines.
  • A plain scalar is text until a typed read asks for something else. 12 reads into an int and into a string; into object it is a YamlElement of kind Number. A quoted "12" is a string and doesn't read into an int.
  • Duplicate keys are an error, as YAML requires, unless DuplicateKeyHandling says otherwise.
  • Empty input reads as no value (null) instead of an error, and several top-level values are several documents.
  • Comments are YAML's (#). JSON's // and /* */ are read only when ReadCommentHandling asks for them, and AllowTrailingCommas changes nothing: YAML already accepts a trailing comma in a flow collection.
  • The APIs that were obsolete upstream are removed: IgnoreNullValues (use DefaultIgnoreCondition) and AddContext (use TypeInfoResolver or TypeInfoResolverChain).

docs/feature-parity.md compares Styml with YamlDotNet and SharpYaml, feature by feature, for a migration from those libraries.

Esc to close