Skip to content

Commit

Permalink
Serialization fixes and small refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
danielcrenna committed Apr 13, 2011
1 parent 2a9d824 commit ecd8a7a
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 14 deletions.
6 changes: 3 additions & 3 deletions metrics.AspNetMvc/Controllers/MetricsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Web.Mvc;
using metrics.AspNetMvc.Extensions;
using metrics.Core;
using Newtonsoft.Json;
using metrics.Serialization;

namespace metrics.AspNetMvc.Controllers
{
Expand All @@ -25,7 +25,7 @@ public ActionResult Metrics(string username, string password)

var result = new ContentResult
{
Content = JsonConvert.SerializeObject(metrics.Metrics.All),
Content = Serializer.Serialize(metrics.Metrics.All),
ContentType = "application/json",
ContentEncoding = Encoding.UTF8
};
Expand Down Expand Up @@ -69,7 +69,7 @@ public ActionResult HealthCheck(string username, string password)

var result = new ContentResult
{
Content = JsonConvert.SerializeObject(health),
Content = Serializer.Serialize(health),
ContentType = "application/json",
ContentEncoding = Encoding.UTF8
};
Expand Down
2 changes: 1 addition & 1 deletion metrics.AspNetMvc/metrics.AspNetMvc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
<WebProjectProperties>
<UseIIS>False</UseIIS>
<AutoAssignPort>True</AutoAssignPort>
<DevelopmentServerPort>52150</DevelopmentServerPort>
<DevelopmentServerPort>19105</DevelopmentServerPort>
<DevelopmentServerVPath>/</DevelopmentServerVPath>
<IISUrl>
</IISUrl>
Expand Down
1 change: 0 additions & 1 deletion metrics.Example/Global.asax.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using metrics.AspNetMvc;

namespace metrics.Example
{
Expand Down
8 changes: 3 additions & 5 deletions metrics.Example/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,22 @@
<title>@ViewBag.Title</title>
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
</head>

<body>
<div class="page">

<div id="header">
<div id="title">
<h1>My MVC Application</h1>
</div>


<div id="logindisplay">&nbsp;</div>

<div id="menucontainer">
<ul id="menu">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
</ul>

</div>
</div>

<div id="main">
@RenderBody()
<div id="footer">
Expand Down
4 changes: 3 additions & 1 deletion metrics/Core/CounterMetric.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System.Threading;
using Newtonsoft.Json;

namespace metrics.Core
{
/// <summary>
/// An atomic counter metric
/// </summary>
public class CounterMetric : IMetric
public sealed class CounterMetric : IMetric
{
private /* atomic */ long _count;

Expand Down Expand Up @@ -49,6 +50,7 @@ public long Count
get { return Interlocked.Read(ref _count); }
}

[JsonIgnore]
public IMetric Copy
{
get { return new CounterMetric(_count); }
Expand Down
8 changes: 5 additions & 3 deletions metrics/Core/GaugeMetric.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Newtonsoft.Json;

namespace metrics.Core
{
Expand All @@ -12,11 +13,12 @@ namespace metrics.Core
/// </code>
/// </example>
/// </summary>
public class GaugeMetric<T> : IMetric
public sealed class GaugeMetric<T> : IMetric
{
private readonly Func<T> _evaluator;

public virtual IMetric Copy
[JsonIgnore]
public IMetric Copy
{
get { return new GaugeMetric<T>(_evaluator); }
}
Expand All @@ -26,7 +28,7 @@ public GaugeMetric(Func<T> evaluator)
_evaluator = evaluator;
}

public virtual T Value
public T Value
{
get { return _evaluator.Invoke(); }
}
Expand Down
1 change: 1 addition & 0 deletions metrics/Core/MetricName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace metrics.Core
public struct MetricName
{
public Type Class { get; private set; }

public string Name { get; private set; }

public MetricName(Type @class, string name) : this()
Expand Down
1 change: 1 addition & 0 deletions metrics/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: InternalsVisibleTo("metrics.Tests")]
[assembly: InternalsVisibleTo("metrics.AspNetMvc")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
Expand Down
65 changes: 65 additions & 0 deletions metrics/Serialization/JsonConventionResolver.cs
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();
}
}
}
45 changes: 45 additions & 0 deletions metrics/Serialization/MetricsConverter.cs
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);
}
}
}
25 changes: 25 additions & 0 deletions metrics/Serialization/Serializer.cs
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);
}
}
}
3 changes: 3 additions & 0 deletions metrics/metrics.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
<Compile Include="Core\MetricName.cs" />
<Compile Include="Metrics.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Serialization\JsonConventionResolver.cs" />
<Compile Include="Serialization\MetricsConverter.cs" />
<Compile Include="Serialization\Serializer.cs" />
<Compile Include="Stats\EWMA.cs" />
<Compile Include="Stats\ExponentiallyDecayingSample.cs" />
<Compile Include="Stats\ISample.cs" />
Expand Down

0 comments on commit ecd8a7a

Please sign in to comment.