-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix in YAML to JSON involving boolean conversion (#579)
- Loading branch information
Showing
3 changed files
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/dev/impl/DevToys/Helpers/JsonYaml/Core/BooleanYamlTypeResolver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#nullable enable | ||
|
||
using System; | ||
using YamlDotNet.Core; | ||
using YamlDotNet.Core.Events; | ||
using YamlDotNet.Serialization; | ||
|
||
namespace DevToys.Helpers.JsonYaml.Core | ||
{ | ||
internal class BooleanYamlTypeResolver : INodeTypeResolver | ||
{ | ||
public bool Resolve(NodeEvent? nodeEvent, ref Type currentType) | ||
{ | ||
if (nodeEvent is Scalar scalar) | ||
{ | ||
// avoid unnecessary parsing attempts | ||
bool couldBeBoolean | ||
= scalar.Style is ScalarStyle.Plain | ||
&& (string.Equals(scalar.Value, bool.FalseString, StringComparison.OrdinalIgnoreCase) || string.Equals(scalar.Value, bool.TrueString, StringComparison.OrdinalIgnoreCase)); | ||
|
||
if (couldBeBoolean && bool.TryParse(scalar.Value, out _)) | ||
{ | ||
currentType = typeof(bool); | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters