Skip to content

Commit 9652d3f

Browse files
committed
Merge pull request #767 from JakeGinnivan/FixJsonFormatting
Fixed json serialisation of 0's
2 parents 7e43610 + 033600a commit 9652d3f

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

src/GitVersionCore.Tests/JsonVersionBuilderTests.Json.approved.txt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
{
22
"Major":1,
33
"Minor":2,
4-
"Patch":3,
4+
"Patch":0,
55
"PreReleaseTag":"unstable.4",
66
"PreReleaseTagWithDash":"-unstable.4",
77
"PreReleaseLabel":"unstable",
88
"PreReleaseNumber":4,
99
"BuildMetaData":5,
1010
"BuildMetaDataPadded":"0005",
1111
"FullBuildMetaData":"5.Branch.feature1.Sha.commitSha",
12-
"MajorMinorPatch":"1.2.3",
13-
"SemVer":"1.2.3-unstable.4",
14-
"LegacySemVer":"1.2.3-unstable4",
15-
"LegacySemVerPadded":"1.2.3-unstable0004",
16-
"AssemblySemVer":"1.2.3.0",
17-
"FullSemVer":"1.2.3-unstable.4+5",
18-
"InformationalVersion":"1.2.3-unstable.4+5.Branch.feature1.Sha.commitSha",
12+
"MajorMinorPatch":"1.2.0",
13+
"SemVer":"1.2.0-unstable.4",
14+
"LegacySemVer":"1.2.0-unstable4",
15+
"LegacySemVerPadded":"1.2.0-unstable0004",
16+
"AssemblySemVer":"1.2.0.0",
17+
"FullSemVer":"1.2.0-unstable.4+5",
18+
"InformationalVersion":"1.2.0-unstable.4+5.Branch.feature1.Sha.commitSha",
1919
"BranchName":"feature1",
2020
"Sha":"commitSha",
21-
"NuGetVersionV2":"1.2.3-unstable0004",
22-
"NuGetVersion":"1.2.3-unstable0004",
21+
"NuGetVersionV2":"1.2.0-unstable0004",
22+
"NuGetVersion":"1.2.0-unstable0004",
2323
"CommitsSinceVersionSource":5,
2424
"CommitsSinceVersionSourcePadded":"0005",
2525
"CommitDate":"2014-03-06"

src/GitVersionCore.Tests/JsonVersionBuilderTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public void Json()
1414
{
1515
Major = 1,
1616
Minor = 2,
17-
Patch = 3,
17+
Patch = 0,
1818
PreReleaseTag = "unstable4",
1919
BuildMetaData = new SemanticVersionBuildMetaData(5, "feature1", "commitSha",DateTimeOffset.Parse("2014-03-06 23:59:59Z"))
2020
};

src/GitVersionCore/OutputFormatters/JsonOutputFormatter.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace GitVersion
22
{
3+
using System.Collections.Generic;
34
using System.Linq;
45
using System.Text;
56

@@ -14,7 +15,8 @@ public static string ToJson(VersionVariables variables)
1415
{
1516
var isLast = (variable.Key == last);
1617
int value;
17-
if (int.TryParse(variable.Value, out value) && variable.Value[0] != '0') // preserve leading zeros for padding
18+
// preserve leading zeros for padding
19+
if (int.TryParse(variable.Value, out value) && NotAPaddedNumber(variable))
1820
builder.AppendLineFormat(" \"{0}\":{1}{2}", variable.Key, value, isLast ? string.Empty : ",");
1921
else
2022
builder.AppendLineFormat(" \"{0}\":\"{1}\"{2}", variable.Key, variable.Value, isLast ? string.Empty : ",");
@@ -23,5 +25,13 @@ public static string ToJson(VersionVariables variables)
2325
builder.Append("}");
2426
return builder.ToString();
2527
}
28+
29+
static bool NotAPaddedNumber(KeyValuePair<string, string> variable)
30+
{
31+
if (variable.Value == "0")
32+
return true;
33+
34+
return !variable.Value.StartsWith("0");
35+
}
2636
}
2737
}

0 commit comments

Comments
 (0)