Skip to content

Instantly share code, notes, and snippets.

@sagacity
Created October 25, 2010 12:57
Show Gist options
  • Save sagacity/644901 to your computer and use it in GitHub Desktop.
Save sagacity/644901 to your computer and use it in GitHub Desktop.
JSON.NET Message Serializer for aspComet
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AspComet;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace Teletrax.Common.Web.Comet
{
public class JsonMessageSerializer: IMessageSerializer
{
public T Deserialize<T>(string input)
{
return JsonConvert.DeserializeObject<T>(input);
}
public string Serialize(object model)
{
return JsonConvert.SerializeObject(model, Formatting.None, new JsonSerializerSettings { ContractResolver = new AspCometMessageResolver() });
}
}
public class AspCometMessageResolver : DefaultContractResolver
{
protected override IList<JsonProperty> CreateProperties(JsonObjectContract contract)
{
var properties = base.CreateProperties(contract);
// Ignore null properties that are in the root of the message object
if (contract.UnderlyingType == typeof(AspComet.Message))
{
foreach (var property in properties)
{
property.NullValueHandling = NullValueHandling.Ignore;
}
}
return properties;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment