Skip to content

Commit

Permalink
Merge pull request #103 from hadashiA/ku/fix-unintended-skip
Browse files Browse the repository at this point in the history
Allow YamlSerializer to be used in the middle of yaml
  • Loading branch information
hadashiA authored Apr 18, 2024
2 parents d5a0ad6 + 2b63865 commit 1eddcf3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
11 changes: 11 additions & 0 deletions VYaml.Unity/Assets/VYaml/Runtime/Parser/YamlParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,17 @@ public void ReadWithVerify(ParseEventType eventType)
Read();
}

public void SkipHeader()
{
while (CurrentEventType is ParseEventType.StreamStart or ParseEventType.DocumentStart or ParseEventType.Nothing)
{
if (!Read())
{
break;
}
}
}

public void SkipAfter(ParseEventType eventType)
{
while (CurrentEventType != eventType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public static T Deserialize<T>(ref YamlParser parser, YamlSerializerOptions? opt
var contextLocal = GetThreadLocalDeserializationContext(options);
contextLocal.Reset();

parser.SkipAfter(ParseEventType.DocumentStart);
parser.SkipHeader();

var formatter = options.Resolver.GetFormatterWithVerify<T>();
return contextLocal.DeserializeWithAlias(formatter, ref parser);
Expand Down
11 changes: 11 additions & 0 deletions VYaml/Parser/YamlParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,17 @@ public void ReadWithVerify(ParseEventType eventType)
Read();
}

public void SkipHeader()
{
while (CurrentEventType is ParseEventType.StreamStart or ParseEventType.DocumentStart or ParseEventType.Nothing)
{
if (!Read())
{
break;
}
}
}

public void SkipAfter(ParseEventType eventType)
{
while (CurrentEventType != eventType)
Expand Down
2 changes: 1 addition & 1 deletion VYaml/Serialization/YamlSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public static T Deserialize<T>(ref YamlParser parser, YamlSerializerOptions? opt
var contextLocal = GetThreadLocalDeserializationContext(options);
contextLocal.Reset();

parser.SkipAfter(ParseEventType.DocumentStart);
parser.SkipHeader();

var formatter = options.Resolver.GetFormatterWithVerify<T>();
return contextLocal.DeserializeWithAlias(formatter, ref parser);
Expand Down

0 comments on commit 1eddcf3

Please sign in to comment.