Skip to content

add JSON Serializer #2060

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 13 commits into from
Jan 24, 2020
Merged

add JSON Serializer #2060

merged 13 commits into from
Jan 24, 2020

Conversation

jetersen
Copy link
Contributor

@jetersen jetersen commented Jan 21, 2020

The problem with adding a JSON Serializer dependency is that you may have dependency conflicts with those using the same JSON serializer dependency.

Reason for #2017 😉 When using AppVeyor, that needed newtonsoft.json dep and the project uses newtonsoft.json in a different version will cause such a conflict.

@jetersen
Copy link
Contributor Author

jetersen commented Jan 21, 2020

@gep13 new build failure 🤔

Time Elapsed 00:00:12.72
	TargetSite:	BuildVersion Calculate(Cake.Core.ICakeContext, BuildParameters, Cake.Common.Tools.GitVersion.GitVersion)
	StackTrace:	   at Submission#0.BuildVersion.Calculate(ICakeContext context, BuildParameters parameters, GitVersion gitVersion)
   at Submission#0.BuildParameters.Initialize(ICakeContext context, GitVersion gitVersion)
   at Submission#0.<<Initialize>>b__0_83(ISetupContext context)
	Message:	Object reference not set to an instance of an object.
	Data:	[  ]
	InnerException:	
	HelpLink:	
	Source:	ℛ*c0849450-dbab-4ef2-9cc0-640e37e6fbdb#1-0
	HResult:	-2147467261

However I am not seeing that locally

@jetersen
Copy link
Contributor Author

jetersen commented Jan 21, 2020

Okay now I am just generating unexpected data:

[
  {
    "Key":"Major",
    "Value":5
  },
  {
    "Key":"Minor",
    "Value":1
  },
  {
    "Key":"Patch",
    "Value":4
  },
  {
    "Key":"PreReleaseTag",
    "Value":"json.1"
  },
  {
    "Key":"PreReleaseTagWithDash",
    "Value":"-json.1"
  },
  {
    "Key":"PreReleaseLabel",
    "Value":"json"
  },
  {
    "Key":"PreReleaseNumber",
    "Value":1
  },
  {
    "Key":"WeightedPreReleaseNumber",
    "Value":1
  },
  {
    "Key":"BuildMetaData",
    "Value":76
  },
  {
    "Key":"BuildMetaDataPadded",
    "Value":"0076"
  },
  {
    "Key":"FullBuildMetaData",
    "Value":"76.Branch.json.Sha.26623e2abbc134b7217d567d331b102b5793a1ed"
  },
  {
    "Key":"MajorMinorPatch",
    "Value":"5.1.4"
  },
  {
    "Key":"SemVer",
    "Value":"5.1.4-json.1"
  },
  {
    "Key":"LegacySemVer",
    "Value":"5.1.4-json1"
  },
  {
    "Key":"LegacySemVerPadded",
    "Value":"5.1.4-json0001"
  },
  {
    "Key":"AssemblySemVer",
    "Value":"5.1.4.0"
  },
  {
    "Key":"AssemblySemFileVer",
    "Value":"5.1.4.0"
  },
  {
    "Key":"FullSemVer",
    "Value":"5.1.4-json.1+76"
  },
  {
    "Key":"InformationalVersion",
    "Value":"5.1.4-json.1+76.Branch.json.Sha.26623e2abbc134b7217d567d331b102b5793a1ed"
  },
  {
    "Key":"BranchName",
    "Value":"json"
  },
  {
    "Key":"Sha",
    "Value":"26623e2abbc134b7217d567d331b102b5793a1ed"
  },
  {
    "Key":"ShortSha",
    "Value":"26623e2"
  },
  {
    "Key":"NuGetVersionV2",
    "Value":"5.1.4-json0001"
  },
  {
    "Key":"NuGetVersion",
    "Value":"5.1.4-json0001"
  },
  {
    "Key":"NuGetPreReleaseTagV2",
    "Value":"json0001"
  },
  {
    "Key":"NuGetPreReleaseTag",
    "Value":"json0001"
  },
  {
    "Key":"VersionSourceSha",
    "Value":"bef8ebc0b62b3ddd0cdafe09b66d68bbfcaf90d5"
  },
  {
    "Key":"CommitsSinceVersionSource",
    "Value":76
  },
  {
    "Key":"CommitsSinceVersionSourcePadded",
    "Value":"0076"
  },
  {
    "Key":"CommitDate",
    "Value":"2020-01-21"
  }
]

@jetersen
Copy link
Contributor Author

I get it. This happens because we extends IEnumerable

@jetersen
Copy link
Contributor Author

@asbjornu I am somewhat concerned about returning null's as it might change the behavior on too many places, it seems like a breaking change.

Copy link
Contributor Author

@jetersen jetersen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we return null or keep empty strings "" in JSON?
Need to update tests either way.

{
// Credit to https://github.com/neuecc
// Inspired by https://gist.github.com/neuecc/7d728cd99d2a1e613362
public static class JsonSerializer
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this overkill?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. This turned out quite a bit more complicated than I had originally visioned, seeing how naïve the existing custom serialization code is. Is all of this necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can write a simpler one that only supports dictionary in one depth?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@asbjornu Please take another look, I chose a more simplified version that can only do objects.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much simpler and sufficient, I think. If we need the full power of a full-fledged JSON serializer, we'll add a dependency to one, but I don't think that need is here at the moment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with adding such dependency is that you may have dependency conflicts with those using the same JSON serializer dependency.
Reason for #2017 😉 When using AppVeyor, that needed ewtonsoft.json dep and the project uses newtonsoft.json in a different version.


var stringContent = new StringContent(body, Encoding.UTF8, "application/json");
var stringContent = new StringContent(JsonSerializer.Serialize(body), Encoding.UTF8, "application/json");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, it's back using our own Serializer 🙇

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😄

@jetersen jetersen requested a review from asbjornu January 22, 2020 07:10
value = kvp.Value;
}

public static IEnumerable<KeyValuePair<string, string>> GetProperties(this object obj)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could properly use a better name 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think GetProperties is fine?

Copy link
Member

@asbjornu asbjornu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this looks good! Tests are green and all is well. 😃

value = kvp.Value;
}

public static IEnumerable<KeyValuePair<string, string>> GetProperties(this object obj)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think GetProperties is fine?

{
// Credit to https://github.com/neuecc
// Inspired by https://gist.github.com/neuecc/7d728cd99d2a1e613362
public static class JsonSerializer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much simpler and sufficient, I think. If we need the full power of a full-fledged JSON serializer, we'll add a dependency to one, but I don't think that need is here at the moment.

@jetersen
Copy link
Contributor Author

all green @arturcic

@arturcic
Copy link
Member

it's up to @asbjornu to merge, as he did the review

@asbjornu asbjornu merged commit f0e5275 into GitTools:master Jan 24, 2020
@asbjornu
Copy link
Member

Thank you so much for your contributions, @Casz! 🙏

@arturcic arturcic added this to the 5.1.4 milestone Jan 24, 2020
@jetersen
Copy link
Contributor Author

Thank you for accepting my contributions @asbjornu 🙇

Just want to make it more clear why this was added, see the updated description.

@jetersen jetersen deleted the fix/remove-json-output-formatter branch January 24, 2020 14:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants