-
Notifications
You must be signed in to change notification settings - Fork 654
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
add JSON Serializer #2060
Changes from 8 commits
eb5145a
508f405
17bfb16
8dacf4d
d42fe78
7de39cd
348b1e5
8d64bfa
914f358
22c2206
ed8a651
6304d7b
aad3ee8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace GitVersion.Extensions | ||
{ | ||
public static class ObjectExtensions | ||
{ | ||
public sealed class ReflectionIgnoreAttribute : Attribute | ||
jetersen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
} | ||
|
||
public static void Deconstruct<TKey, TValue>( | ||
this KeyValuePair<TKey, TValue> kvp, | ||
out TKey key, | ||
out TValue value) | ||
{ | ||
key = kvp.Key; | ||
value = kvp.Value; | ||
} | ||
|
||
public static IEnumerable<KeyValuePair<string, string>> GetProperties(this object obj) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could properly use a better name 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
{ | ||
var type = typeof(string); | ||
return obj.GetType().GetProperties() | ||
.Where(p => p.PropertyType == type && !p.GetIndexParameters().Any() && !p.GetCustomAttributes(typeof(ReflectionIgnoreAttribute), false).Any()) | ||
.Select(p => new KeyValuePair<string, string>(p.Name, (string) p.GetValue(obj, null))); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System.Text; | ||
using GitVersion.Extensions; | ||
|
||
namespace GitVersion.Helpers | ||
{ | ||
public static class JsonSerializer | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this overkill? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
{ | ||
public static string Serialize(object obj) | ||
{ | ||
var builder = new StringBuilder(); | ||
builder.AppendLine("{"); | ||
var first = true; | ||
foreach (var (key, value) in obj.GetProperties()) | ||
{ | ||
if (!first) builder.AppendLine(","); | ||
else first = false; | ||
|
||
builder.Append($" \"{key}\":"); | ||
|
||
// preserve leading zeros for padding | ||
if (NotAPaddedNumber(value) && int.TryParse(value, out var number)) | ||
builder.Append(number); | ||
else | ||
builder.Append($"\"{value}\""); | ||
} | ||
|
||
builder.AppendLine().Append("}"); | ||
return builder.ToString(); | ||
} | ||
|
||
private static bool NotAPaddedNumber(string value) => value != null && (value == "0" || !value.StartsWith("0")); | ||
} | ||
} |
This file was deleted.
There was a problem hiding this comment.
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 🙇
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😄