![]() |
VOOZH | about |
dotnet add package LiteYaml --version 0.1.1
NuGet\Install-Package LiteYaml -Version 0.1.1
<PackageReference Include="LiteYaml" Version="0.1.1" />
<PackageVersion Include="LiteYaml" Version="0.1.1" />Directory.Packages.props
<PackageReference Include="LiteYaml" />Project file
paket add LiteYaml --version 0.1.1
#r "nuget: LiteYaml, 0.1.1"
#:package LiteYaml@0.1.1
#addin nuget:?package=LiteYaml&version=0.1.1Install as a Cake Addin
#tool nuget:?package=LiteYaml&version=0.1.1Install as a Cake Tool
Lightweight yaml parser and emitter stripped from VYaml
YamlParser struct provides access to the complete meta-information of yaml.
YamlParser.Read() reads through to the next syntax on yaml. (If end of stream then return false.)YamlParser.ParseEventType indicates the state of the currently read yaml parsing result.YamlParser.GetScalarAs* families take the result of converting a scalar at the current position to a specified type.YamlParser.TryGetScalarAs* families return true and take a result if the current position is a scalar and of the specified type.YamlParser.ReadScalarAs* families is similar to GetScalarAs*, but advances the present position to after the scalar read.YamlParser.TryGetTag(out Tag tag)YamlParser.TryGetCurrentAnchor(out Anchor anchor)Basic example:
var parser = YamlParser.FromBytes(utf8Bytes);
// YAML contains more than one `Document`.
// Here we skip to before first document content.
parser.SkipAfter(ParseEventType.DocumentStart);
// Scanning...
while (parser.Read())
{
// If the current syntax is Scalar,
if (parser.CurrentEventType == ParseEventType.Scalar)
{
var intValue = parser.GetScalarAsInt32();
var stringValue = parser.GetScalarAsString();
// ...
if (parser.TryGetCurrentTag(out var tag))
{
// Check for the tag...
}
if (parser.TryGetCurrentAnchor(out var anchor))
{
// Check for the anchor...
}
}
// If the current syntax is Sequence (Like a list in yaml)
else if (parser.CurrentEventType == ParseEventType.SequenceStart)
{
// We can check for the tag...
// We can check for the anchor...
parser.Read(); // Skip SequenceStart
// Read to end of sequence
while (!parser.End && parser.CurrentEventType != ParseEventType.SequenceEnd)
{
// A sequence element may be a scalar or other...
if (parser.CurrentEventType = ParseEventType.Scalar)
{
// ...
}
// ...
// ...
else
{
// We can skip current element. (It could be a scalar, or alias, sequence, mapping...)
parser.SkipCurrentNode();
}
}
parser.Read(); // Skip SequenceEnd.
}
// If the current syntax is Mapping (like a Dictionary in yaml)
else if (parser.CurrentEventType == ParseEventType.MappingStart)
{
// We can check for the tag...
// We can check for the anchor...
parser.Read(); // Skip MappingStart
// Read to end of mapping
while (parser.CurrentEventType != ParseEventType.MappingEnd)
{
// After Mapping start, key and value appear alternately.
var key = parser.ReadScalarAsString(); // if key is scalar
var value = parser.ReadScalarAsString(); // if value is scalar
// Or we can skip current key/value. (It could be a scalar, or alias, sequence, mapping...)
// parser.SkipCurrentNode(); // skip key
// parser.SkipCurrentNode(); // skip value
}
parser.Read(); // Skip MappingEnd.
}
// Alias
else if (parser.CurrentEventType == ParseEventType.Alias)
{
// If Alias is used, the previous anchors must be holded somewhere.
// In the High level Deserialize API, `YamlDeserializationContext` does exactly this.
}
}
See test code for more information.
The above test covers various patterns for the order of ParsingEvent.
Utf8YamlEmitter struct provides to write YAML formatted string.
Basic usage:
var buffer = new ArrayBufferWriter();
var emitter = new Utf8YamlEmitter(buffer); // It needs buffer implemented `IBufferWriter<byte>`
emitter.BeginMapping(); // Mapping is a collection like Dictionary in YAML
{
emitter.WriteString("key1");
emitter.WriteString("value-1");
emitter.WriteString("key2");
emitter.WriteInt32(222);
emitter.WriteString("key3");
emitter.WriteFloat(3.333f);
}
emitter.EndMapping();
// If you want to expand a string in memory, you can do this.
System.Text.Encoding.UTF8.GetString(buffer.WrittenSpan);
key1: value-1
key2: 222
key3: 3.333
By default, WriteString() automatically determines the format of a scalar.
Multi-line strings are automatically format as a literal scalar:
emitter.WriteString("Hello,\nWorld!\n");
|
Hello,
World!
Special characters contained strings are automatically quoted.
emitter.WriteString("&aaaaa ");
"&aaaaa "
Or you can specify the style explicitly:
emitter.WriteString("aaaaaaa", ScalarStyle.Literal);
|-
aaaaaaaa
e.g:
emitter.BeginSequence();
{
emitter.BeginSequence(SequenceStyle.Flow);
{
emitter.WriteInt32(100);
emitter.WriteString("&hoge");
emitter.WriteString("bra");
}
emitter.EndSequence();
emitter.BeginMapping();
{
emitter.WriteString("key1");
emitter.WriteString("item1");
emitter.WriteString("key2");
emitter.BeginSequence();
{
emitter.WriteString("nested-item1")
emitter.WriteString("nested-item2")
emitter.BeginMapping();
{
emitter.WriteString("nested-key1")
emitter.WriteInt32(100)
}
emitter.EndMapping();
}
emitter.EndSequence();
}
emitter.EndMapping();
}
emitter.EndMapping();
- [100, "&hoge", bra]
- key1: item1
key2:
- nested-item1
- nested-item2
- nested-key1: 100
The following is the default implicit type interpretation.
Basically, it follows YAML Core Schema. https://yaml.org/spec/1.2.2/#103-core-schema
| Support | Regular expression | Resolved to type |
|---|---|---|
| ✅ | null \| Null \| NULL \| ~ |
null |
| ✅ | /* Empty */ |
null |
| ✅ | true \| True \| TRUE \| false \| False \| FALSE |
boolean |
| ✅ | [-+]? [0-9]+ |
int (Base 10) |
| ✅ | 0o [0-7]+ |
int (Base 8) |
| ✅ | 0x [0-9a-fA-F]+ |
int (Base 16) |
| ✅ | [-+]? ( \. [0-9]+ \| [0-9]+ ( \. [0-9]* )? ) ( [eE] [-+]? [0-9]+ )? |
float |
| ✅ | [-+]? ( \.inf \| \.Inf \| \.INF ) |
float (Infinity) |
| ✅ | \.nan \| \.NaN \| \.NAN |
float (Not a number) |
Following is the results of the test for the examples from the yaml spec page.
LiteYaml is a stripped back version of VYaml with minor updates.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 is compatible. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 netstandard2.1 is compatible. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 2 NuGet packages that depend on LiteYaml:
| Package | Downloads |
|---|---|
|
BymlLibrary
Modern BYML IO library written in managed C#. Supports versions 2 to 7. |
|
|
Nindot.BymlLibrary
Package Description |
This package is not used by any popular GitHub repositories.