Skip to content

Commit 9e14aba

Browse files
Fix bug with scalar values (#7) (#8)
Co-authored-by: Gilles TOURREAU <gilles.tourreau@pos-informatique.fr> Co-authored-by: Gilles TOURREAU <gilles.tourreau@pos-informatique.fr>
1 parent b52b790 commit 9e14aba

File tree

4 files changed

+108
-8
lines changed

4 files changed

+108
-8
lines changed

MSBuild.Yaml.Tests/YamlToJsonTest.cs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,13 @@ public void Execute_WithIndentation()
4949
var yaml = @"
5050
object:
5151
- value: 'v1'
52+
- booleanValue: false
53+
- decimalValue: 11.22
54+
- stringValue: ""Blabla""
55+
- stringValue2: 'Blabla'
56+
- nullValue:
5257
- innerObject:
53-
innerValue: 1234
58+
innerIntValue: 1234
5459
";
5560

5661
File.WriteAllText(@"sub\folder\yaml.yaml", yaml);
@@ -78,7 +83,33 @@ public void Execute_WithIndentation()
7883

7984
var json = File.ReadAllText(@"sub\folder\json.json");
8085

81-
json.Should().Be("{\r\n \"object\": [\r\n {\r\n \"value\": \"v1\"\r\n },\r\n {\r\n \"innerObject\": null,\r\n \"innerValue\": \"1234\"\r\n }\r\n ]\r\n}");
86+
json.Should().Be(@"{
87+
""object"": [
88+
{
89+
""value"": ""v1""
90+
},
91+
{
92+
""booleanValue"": false
93+
},
94+
{
95+
""decimalValue"": 11.22
96+
},
97+
{
98+
""stringValue"": ""Blabla""
99+
},
100+
{
101+
""stringValue2"": ""Blabla""
102+
},
103+
{
104+
""nullValue"": null
105+
},
106+
{
107+
""innerObject"": {
108+
""innerIntValue"": 1234
109+
}
110+
}
111+
]
112+
}");
82113

83114
buildEngine.Verify(b => b.LogMessageEvent(It.IsAny<BuildMessageEventArgs>()));
84115
}
@@ -96,8 +127,13 @@ public void Execute_WithNoIndentation()
96127
var yaml = @"
97128
object:
98129
- value: 'v1'
130+
- booleanValue: false
131+
- decimalValue: 11.22
132+
- stringValue: ""Blabla""
133+
- stringValue2: 'Blabla'
134+
- nullValue:
99135
- innerObject:
100-
innerValue: 1234
136+
innerIntValue: 1234
101137
";
102138

103139
File.WriteAllText(@"sub\folder\yaml.yaml", yaml);
@@ -125,7 +161,7 @@ public void Execute_WithNoIndentation()
125161

126162
var json = File.ReadAllText(@"sub\folder\json.json");
127163

128-
json.Should().Be("{\"object\":[{\"value\":\"v1\"},{\"innerObject\":null,\"innerValue\":\"1234\"}]}");
164+
json.Should().Be(@"{""object"":[{""value"":""v1""},{""booleanValue"":false},{""decimalValue"":11.22},{""stringValue"":""Blabla""},{""stringValue2"":""Blabla""},{""nullValue"":null},{""innerObject"":{""innerIntValue"":1234}}]}");
129165

130166
buildEngine.Verify(b => b.LogMessageEvent(It.IsAny<BuildMessageEventArgs>()));
131167
}

MSBuild.Yaml/MSBuild.Yaml.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
<RepositoryUrl>https://github.com/PosInformatique/PosInformatique.MSBuild.Yaml.git</RepositoryUrl>
1616
<RepositoryType>git</RepositoryType>
1717
<PackageTags>msbuild yaml json converter</PackageTags>
18-
<PackageReleaseNotes>1.0.0 - Initial version</PackageReleaseNotes>
18+
<PackageReleaseNotes>1.0.1 - Fix bugs with scalar values as boolean, integer, decimal and null.
19+
1.0.0 - Initial version</PackageReleaseNotes>
1920

2021
<!-- NuGet packaging options -->
2122
<BuildOutputTargetFolder>tasks</BuildOutputTargetFolder>
2223
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
2324
<NoPackageAnalysis>true</NoPackageAnalysis>
25+
<Version>1.0.1</Version>
2426

2527
</PropertyGroup>
2628

MSBuild.Yaml/YamlToJson.cs

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,25 @@
66
namespace PosInformatique.MSBuild.Yaml
77
{
88
using System;
9+
using System.Globalization;
910
using System.IO;
1011
using Microsoft.Build.Framework;
1112
using Newtonsoft.Json;
13+
using YamlDotNet.Core;
14+
using YamlDotNet.Core.Events;
1215
using YamlDotNet.Serialization;
1316

1417
/// <summary>
1518
/// MSBuild task which convert a YAML file into a JSON file.
1619
/// </summary>
1720
public sealed class YamlToJson : Microsoft.Build.Utilities.Task
1821
{
22+
/// <summary>
23+
/// YAML deserializer.
24+
/// </summary>
25+
private static readonly IDeserializer yamlDeserializer = new DeserializerBuilder()
26+
.WithNodeDeserializer(new ScalarNodeDeserializer()).Build();
27+
1928
/// <summary>
2029
/// Initializes a new instance of the <see cref="YamlToJson"/> class.
2130
/// </summary>
@@ -73,8 +82,6 @@ public override bool Execute()
7382
this.Log.LogMessage(MessageImportance.Normal, MsBuildYamlResources.LogConvertingYamlFile, this.YamlInputFile, this.JsonOutputFile, MsBuildYamlResources.WithoutIndentation);
7483
}
7584

76-
var yamlDeserializer = new Deserializer();
77-
7885
using (var inputReader = new StreamReader(this.YamlInputFile))
7986
{
8087
var yamlContent = yamlDeserializer.Deserialize(inputReader);
@@ -94,5 +101,60 @@ public override bool Execute()
94101

95102
return true;
96103
}
104+
105+
/// <summary>
106+
/// Implementation of the <see cref="INodeDeserializer"/> which allows to
107+
/// parse the scalar values:
108+
/// - Boolean
109+
/// - Integer
110+
/// - Decimal
111+
/// - Empty values to null
112+
/// </summary>
113+
private sealed class ScalarNodeDeserializer : INodeDeserializer
114+
{
115+
/// <summary>
116+
/// <see cref="YamlDotNet.Serialization.NodeDeserializers.ScalarNodeDeserializer"/> used to retrieve
117+
/// the default behavior for the not supported scalar values.
118+
/// </summary>
119+
private static readonly INodeDeserializer defaultNodeDeserializer = new YamlDotNet.Serialization.NodeDeserializers.ScalarNodeDeserializer();
120+
121+
/// <inheritdoc />
122+
public bool Deserialize(IParser reader, Type expectedType, Func<IParser, Type, object> nestedObjectDeserializer, out object value)
123+
{
124+
if (reader.Accept(out Scalar scalar))
125+
{
126+
if (string.IsNullOrEmpty(scalar.Value))
127+
{
128+
value = null;
129+
reader.MoveNext();
130+
131+
return true;
132+
}
133+
else if (bool.TryParse(scalar.Value, out bool booleanValue))
134+
{
135+
value = booleanValue;
136+
reader.MoveNext();
137+
138+
return true;
139+
}
140+
else if (int.TryParse(scalar.Value, NumberStyles.Any, CultureInfo.InvariantCulture, out int integerValue))
141+
{
142+
value = integerValue;
143+
reader.MoveNext();
144+
145+
return true;
146+
}
147+
else if (decimal.TryParse(scalar.Value, NumberStyles.Any, CultureInfo.InvariantCulture , out decimal decimalValue))
148+
{
149+
value = decimalValue;
150+
reader.MoveNext();
151+
152+
return true;
153+
}
154+
}
155+
156+
return defaultNodeDeserializer.Deserialize(reader, expectedType, nestedObjectDeserializer, out value);
157+
}
158+
}
97159
}
98160
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ To declare YAML files to generate automatically before compilation:
5454
- Install the [PosInformatique.MSBuild.Yaml](https://www.nuget.org/packages/PosInformatique.MSBuild.Yaml/)
5555
package using the following command line in the Visual Studio Package Manager console:
5656
```powershell
57-
Install-Package PosInformatique.AspNetCore.Server.AspNet
57+
Install-Package PosInformatique.MSBuild.Yaml
5858
```
5959

6060
- Updates the `.xxproj` file by declaring the YAML files in the `<YamlToJsonFile>`

0 commit comments

Comments
 (0)