-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Serialization fixes and small refactorings
- Loading branch information
1 parent
2a9d824
commit ecd8a7a
Showing
12 changed files
with
155 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using Newtonsoft.Json.Serialization; | ||
|
||
namespace metrics.Serialization | ||
{ | ||
/// <summary> | ||
/// Serializes JSON like the rest of the universe | ||
/// </summary> | ||
internal class JsonConventionResolver : DefaultContractResolver | ||
{ | ||
public class ToStringComparer : IComparer | ||
{ | ||
public int Compare(object x, object y) | ||
{ | ||
return x.ToString().CompareTo(y.ToString()); | ||
} | ||
} | ||
|
||
protected override IList<JsonProperty> CreateProperties(Type type, Newtonsoft.Json.MemberSerialization memberSerialization) | ||
{ | ||
var properties = base.CreateProperties(type, memberSerialization); | ||
|
||
return CreatePropertiesImpl(properties); | ||
} | ||
|
||
private static IList<JsonProperty> CreatePropertiesImpl(IList<JsonProperty> properties) | ||
{ | ||
foreach (var property in properties) | ||
{ | ||
property.PropertyName = PascalCaseToElement(property.PropertyName); | ||
} | ||
|
||
return properties; | ||
} | ||
|
||
private static string PascalCaseToElement(string input) | ||
{ | ||
if (string.IsNullOrEmpty(input)) | ||
{ | ||
return null; | ||
} | ||
|
||
var result = new StringBuilder(); | ||
result.Append(char.ToLowerInvariant(input[0])); | ||
|
||
for (var i = 1; i < input.Length; i++) | ||
{ | ||
if (char.IsLower(input[i])) | ||
{ | ||
result.Append(input[i]); | ||
} | ||
else | ||
{ | ||
result.Append("_"); | ||
result.Append(char.ToLowerInvariant(input[i])); | ||
} | ||
} | ||
|
||
return result.ToString(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using metrics.Core; | ||
using Newtonsoft.Json; | ||
|
||
namespace metrics.Serialization | ||
{ | ||
internal class MetricItem | ||
{ | ||
public string Name { get; set; } | ||
public IMetric Metric { get; set; } | ||
} | ||
|
||
/// <summary> | ||
/// Properly serializes a metrics hash | ||
/// </summary> | ||
internal class MetricsConverter : JsonConverter | ||
{ | ||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | ||
{ | ||
if (!(value is IDictionary<MetricName, IMetric>)) | ||
{ | ||
return; | ||
} | ||
|
||
var collection = (IDictionary<MetricName, IMetric>)value; | ||
var container = new List<MetricItem>(collection.Count); | ||
container.AddRange(collection.Select(item => new MetricItem {Name = item.Key.Name, Metric = item.Value})); | ||
var serialized = Serializer.Serialize(container); | ||
|
||
writer.WriteRawValue(serialized); | ||
} | ||
|
||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public override bool CanConvert(Type objectType) | ||
{ | ||
return typeof (IDictionary<MetricName, IMetric>).IsAssignableFrom(objectType); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using Newtonsoft.Json; | ||
|
||
namespace metrics.Serialization | ||
{ | ||
internal class Serializer | ||
{ | ||
private static readonly JsonSerializerSettings _settings; | ||
|
||
static Serializer() | ||
{ | ||
_settings = new JsonSerializerSettings | ||
{ | ||
DefaultValueHandling = DefaultValueHandling.Ignore, | ||
NullValueHandling = NullValueHandling.Ignore, | ||
ContractResolver = new JsonConventionResolver(), | ||
}; | ||
_settings.Converters.Add(new MetricsConverter()); | ||
} | ||
|
||
public static string Serialize<T>(T entity) | ||
{ | ||
return JsonConvert.SerializeObject(entity, Formatting.Indented, _settings); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters