Created
October 25, 2010 12:57
-
-
Save sagacity/644901 to your computer and use it in GitHub Desktop.
JSON.NET Message Serializer for aspComet
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
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