Skip to content

Commit f0e5275

Browse files
authored
Merge pull request #2060 from casz/fix/remove-json-output-formatter
add JSON Serializer
2 parents 2f639bd + aad3ee8 commit f0e5275

File tree

8 files changed

+90
-61
lines changed

8 files changed

+90
-61
lines changed

src/GitVersionCore.Tests/JsonVersionBuilderTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using NUnit.Framework;
33
using Shouldly;
4-
using GitVersion.OutputFormatters;
54
using GitVersion.OutputVariables;
65
using GitVersion;
76
using GitVersionCore.Tests.Helpers;
@@ -38,7 +37,7 @@ public void Json()
3837

3938
var variableProvider = sp.GetService<IVariableProvider>();
4039
var variables = variableProvider.GetVariablesFor(semanticVersion, config, false);
41-
var json = JsonOutputFormatter.ToJson(variables);
40+
var json = variables.ToString();
4241
json.ShouldMatchApproved(c => c.SubFolder("Approved"));
4342
}
4443
}

src/GitVersionCore.Tests/VariableProviderTests.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using GitVersion;
22
using GitVersion.Logging;
3-
using GitVersion.OutputFormatters;
43
using GitVersion.OutputVariables;
54
using GitVersion.VersioningModes;
65
using GitVersionCore.Tests.Helpers;
@@ -74,7 +73,7 @@ public void ProvidesVariablesInContinuousDeliveryModeForPreRelease()
7473

7574
var vars = variableProvider.GetVariablesFor(semVer, config, false);
7675

77-
JsonOutputFormatter.ToJson(vars).ShouldMatchApproved(c => c.SubFolder("Approved"));
76+
vars.ToString().ShouldMatchApproved(c => c.SubFolder("Approved"));
7877
}
7978

8079
[Test]
@@ -101,7 +100,7 @@ public void ProvidesVariablesInContinuousDeliveryModeForPreReleaseWithPadding()
101100

102101
var vars = variableProvider.GetVariablesFor(semVer, config, false);
103102

104-
JsonOutputFormatter.ToJson(vars).ShouldMatchApproved(c => c.SubFolder("Approved"));
103+
vars.ToString().ShouldMatchApproved(c => c.SubFolder("Approved"));
105104
}
106105

107106
[Test]
@@ -127,7 +126,7 @@ public void ProvidesVariablesInContinuousDeploymentModeForPreRelease()
127126

128127
var vars = variableProvider.GetVariablesFor(semVer, config, false);
129128

130-
JsonOutputFormatter.ToJson(vars).ShouldMatchApproved(c => c.SubFolder("Approved"));
129+
vars.ToString().ShouldMatchApproved(c => c.SubFolder("Approved"));
131130
}
132131

133132
[Test]
@@ -152,7 +151,7 @@ public void ProvidesVariablesInContinuousDeliveryModeForStable()
152151

153152
var vars = variableProvider.GetVariablesFor(semVer, config, false);
154153

155-
JsonOutputFormatter.ToJson(vars).ShouldMatchApproved(c => c.SubFolder("Approved"));
154+
vars.ToString().ShouldMatchApproved(c => c.SubFolder("Approved"));
156155
}
157156

158157
[Test]
@@ -177,7 +176,7 @@ public void ProvidesVariablesInContinuousDeploymentModeForStable()
177176

178177
var vars = variableProvider.GetVariablesFor(semVer, config, false);
179178

180-
JsonOutputFormatter.ToJson(vars).ShouldMatchApproved(c => c.SubFolder("Approved"));
179+
vars.ToString().ShouldMatchApproved(c => c.SubFolder("Approved"));
181180
}
182181

183182
[Test]
@@ -205,7 +204,7 @@ public void ProvidesVariablesInContinuousDeploymentModeForStableWhenCurrentCommi
205204

206205
var vars = variableProvider.GetVariablesFor(semVer, config, true);
207206

208-
JsonOutputFormatter.ToJson(vars).ShouldMatchApproved(c => c.SubFolder("Approved"));
207+
vars.ToString().ShouldMatchApproved(c => c.SubFolder("Approved"));
209208
}
210209

211210
[Test]
@@ -277,7 +276,7 @@ public void ProvidesVariablesInContinuousDeliveryModeForFeatureBranch()
277276

278277
var vars = variableProvider.GetVariablesFor(semVer, config, false);
279278

280-
JsonOutputFormatter.ToJson(vars).ShouldMatchApproved(c => c.SubFolder("Approved"));
279+
vars.ToString().ShouldMatchApproved(c => c.SubFolder("Approved"));
281280
}
282281

283282
[Test]
@@ -304,7 +303,7 @@ public void ProvidesVariablesInContinuousDeliveryModeForFeatureBranchWithCustomA
304303

305304
var vars = variableProvider.GetVariablesFor(semVer, config, false);
306305

307-
JsonOutputFormatter.ToJson(vars).ShouldMatchApproved(c => c.SubFolder("Approved"));
306+
vars.ToString().ShouldMatchApproved(c => c.SubFolder("Approved"));
308307
}
309308
}
310309
}

src/GitVersionCore/BuildServers/AppVeyor.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using GitVersion.OutputVariables;
44
using GitVersion.Logging;
55
using System.Text;
6+
using GitVersion.Helpers;
67

78
namespace GitVersion.BuildServers
89
{
@@ -22,9 +23,12 @@ public override string GenerateSetVersionMessage(VersionVariables variables)
2223

2324
using var httpClient = GetHttpClient();
2425

25-
var body = $"{{\"version\":\"{variables.FullSemVer}.build.{buildNumber}\"}}";
26+
var body = new
27+
{
28+
version = $"{variables.FullSemVer}.build.{buildNumber}",
29+
};
2630

27-
var stringContent = new StringContent(body, Encoding.UTF8, "application/json");
31+
var stringContent = new StringContent(JsonSerializer.Serialize(body), Encoding.UTF8, "application/json");
2832
var response = httpClient.PutAsync("api/build", stringContent).GetAwaiter().GetResult();
2933
response.EnsureSuccessStatusCode();
3034

@@ -35,9 +39,13 @@ public override string[] GenerateSetParameterMessage(string name, string value)
3539
{
3640
var httpClient = GetHttpClient();
3741

38-
var body = $"{{\"name\": \"GitVersion_{name}\",\"value\": \"{value}\"}}";
42+
var body = new
43+
{
44+
name = $"GitVersion_{name}",
45+
value = $"{value}"
46+
};
3947

40-
var stringContent = new StringContent(body, Encoding.UTF8, "application/json");
48+
var stringContent = new StringContent(JsonSerializer.Serialize(body), Encoding.UTF8, "application/json");
4149
var response = httpClient.PostAsync("api/build/variables", stringContent).GetAwaiter().GetResult();
4250
response.EnsureSuccessStatusCode();
4351

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace GitVersion.Extensions
6+
{
7+
public static class ObjectExtensions
8+
{
9+
internal sealed class ReflectionIgnoreAttribute : Attribute
10+
{
11+
}
12+
13+
public static void Deconstruct<TKey, TValue>(
14+
this KeyValuePair<TKey, TValue> kvp,
15+
out TKey key,
16+
out TValue value)
17+
{
18+
key = kvp.Key;
19+
value = kvp.Value;
20+
}
21+
22+
public static IEnumerable<KeyValuePair<string, string>> GetProperties(this object obj)
23+
{
24+
var type = typeof(string);
25+
return obj.GetType().GetProperties()
26+
.Where(p => p.PropertyType == type && !p.GetIndexParameters().Any() && !p.GetCustomAttributes(typeof(ReflectionIgnoreAttribute), false).Any())
27+
.Select(p => new KeyValuePair<string, string>(p.Name, (string) p.GetValue(obj, null)));
28+
}
29+
}
30+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Text;
2+
using GitVersion.Extensions;
3+
4+
namespace GitVersion.Helpers
5+
{
6+
public static class JsonSerializer
7+
{
8+
public static string Serialize(object obj)
9+
{
10+
var builder = new StringBuilder();
11+
builder.AppendLine("{");
12+
var first = true;
13+
foreach (var (key, value) in obj.GetProperties())
14+
{
15+
if (!first) builder.AppendLine(",");
16+
else first = false;
17+
18+
builder.Append($" \"{key}\":");
19+
20+
// preserve leading zeros for padding
21+
if (NotAPaddedNumber(value) && int.TryParse(value, out var number))
22+
builder.Append(number);
23+
else
24+
builder.Append($"\"{value}\"");
25+
}
26+
27+
builder.AppendLine().Append("}");
28+
return builder.ToString();
29+
}
30+
31+
private static bool NotAPaddedNumber(string value) => value != null && (value == "0" || !value.StartsWith("0"));
32+
}
33+
}

src/GitVersionCore/OutputFormatters/JsonOutputFormatter.cs

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/GitVersionCore/OutputVariables/VersionVariables.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
using System.Collections.Generic;
44
using System.IO;
55
using System.Linq;
6+
using GitVersion.Helpers;
67
using YamlDotNet.Serialization;
8+
using static GitVersion.Extensions.ObjectExtensions;
79

810
namespace GitVersion.OutputVariables
911
{
@@ -125,12 +127,7 @@ public static IEnumerable<string> AvailableVariables
125127

126128
public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
127129
{
128-
var type = typeof(string);
129-
return typeof(VersionVariables)
130-
.GetProperties()
131-
.Where(p => p.PropertyType == type && !p.GetIndexParameters().Any() && !p.GetCustomAttributes(typeof(ReflectionIgnoreAttribute), false).Any())
132-
.Select(p => new KeyValuePair<string, string>(p.Name, (string)p.GetValue(this, null)))
133-
.GetEnumerator();
130+
return this.GetProperties().GetEnumerator();
134131
}
135132

136133
IEnumerator IEnumerable.GetEnumerator()
@@ -178,8 +175,9 @@ public bool ContainsKey(string variable)
178175
return typeof(VersionVariables).GetProperty(variable) != null;
179176
}
180177

181-
private sealed class ReflectionIgnoreAttribute : Attribute
178+
public override string ToString()
182179
{
180+
return JsonSerializer.Serialize(this);
183181
}
184182
}
185183
}

src/GitVersionExe/ExecCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void Execute()
5454
switch (arguments.ShowVariable)
5555
{
5656
case null:
57-
Console.WriteLine(JsonOutputFormatter.ToJson(variables));
57+
Console.WriteLine(variables.ToString());
5858
break;
5959

6060
default:

0 commit comments

Comments
 (0)