Skip to content

Fixed json serialisation of 0's #767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/GitVersionCore.Tests/JsonVersionBuilderTests.Json.approved.txt
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
{
"Major":1,
"Minor":2,
"Patch":3,
"Patch":0,
"PreReleaseTag":"unstable.4",
"PreReleaseTagWithDash":"-unstable.4",
"PreReleaseLabel":"unstable",
"PreReleaseNumber":4,
"BuildMetaData":5,
"BuildMetaDataPadded":"0005",
"FullBuildMetaData":"5.Branch.feature1.Sha.commitSha",
"MajorMinorPatch":"1.2.3",
"SemVer":"1.2.3-unstable.4",
"LegacySemVer":"1.2.3-unstable4",
"LegacySemVerPadded":"1.2.3-unstable0004",
"AssemblySemVer":"1.2.3.0",
"FullSemVer":"1.2.3-unstable.4+5",
"InformationalVersion":"1.2.3-unstable.4+5.Branch.feature1.Sha.commitSha",
"MajorMinorPatch":"1.2.0",
"SemVer":"1.2.0-unstable.4",
"LegacySemVer":"1.2.0-unstable4",
"LegacySemVerPadded":"1.2.0-unstable0004",
"AssemblySemVer":"1.2.0.0",
"FullSemVer":"1.2.0-unstable.4+5",
"InformationalVersion":"1.2.0-unstable.4+5.Branch.feature1.Sha.commitSha",
"BranchName":"feature1",
"Sha":"commitSha",
"NuGetVersionV2":"1.2.3-unstable0004",
"NuGetVersion":"1.2.3-unstable0004",
"NuGetVersionV2":"1.2.0-unstable0004",
"NuGetVersion":"1.2.0-unstable0004",
"CommitsSinceVersionSource":5,
"CommitsSinceVersionSourcePadded":"0005",
"CommitDate":"2014-03-06"
Expand Down
2 changes: 1 addition & 1 deletion src/GitVersionCore.Tests/JsonVersionBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void Json()
{
Major = 1,
Minor = 2,
Patch = 3,
Patch = 0,
PreReleaseTag = "unstable4",
BuildMetaData = new SemanticVersionBuildMetaData(5, "feature1", "commitSha",DateTimeOffset.Parse("2014-03-06 23:59:59Z"))
};
Expand Down
12 changes: 11 additions & 1 deletion src/GitVersionCore/OutputFormatters/JsonOutputFormatter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace GitVersion
{
using System.Collections.Generic;
using System.Linq;
using System.Text;

Expand All @@ -14,7 +15,8 @@ public static string ToJson(VersionVariables variables)
{
var isLast = (variable.Key == last);
int value;
if (int.TryParse(variable.Value, out value) && variable.Value[0] != '0') // preserve leading zeros for padding
// preserve leading zeros for padding
if (int.TryParse(variable.Value, out value) && NotAPaddedNumber(variable))
builder.AppendLineFormat(" \"{0}\":{1}{2}", variable.Key, value, isLast ? string.Empty : ",");
else
builder.AppendLineFormat(" \"{0}\":\"{1}\"{2}", variable.Key, variable.Value, isLast ? string.Empty : ",");
Expand All @@ -23,5 +25,13 @@ public static string ToJson(VersionVariables variables)
builder.Append("}");
return builder.ToString();
}

static bool NotAPaddedNumber(KeyValuePair<string, string> variable)
{
if (variable.Value == "0")
return true;

return !variable.Value.StartsWith("0");
}
}
}