Created
February 25, 2013 14:25
-
-
Save hvitorino/5030118 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.IO; | |
| using Newtonsoft.Json; | |
| using Newtonsoft.Json.Converters; | |
| using Restfulie.Server.Marshalling.Serializers; | |
| namespace NLib.Web.Configuracao.Restfulie | |
| { | |
| public abstract class NLSerializer : IResourceSerializer | |
| { | |
| private readonly JsonSerializer jsonSerializer; | |
| protected NLSerializer(JsonConverter dateTimeConverter) | |
| { | |
| jsonSerializer = new JsonSerializer(); | |
| jsonSerializer.Converters.Add(new NHibernateProxyJsonConverter()); | |
| jsonSerializer.Converters.Add(new StringEnumConverter()); | |
| jsonSerializer.Converters.Add(dateTimeConverter); | |
| jsonSerializer.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; | |
| jsonSerializer.NullValueHandling = NullValueHandling.Ignore; | |
| } | |
| public string Serialize(object model) | |
| { | |
| var result = "{}"; | |
| if (model != null) | |
| { | |
| var stringWriter = new StringWriter(); | |
| jsonSerializer.Serialize(stringWriter, model); | |
| result = stringWriter.GetStringBuilder().ToString(); | |
| } | |
| return result; | |
| } | |
| } | |
| public class NLJsonSerializer : NLSerializer | |
| { | |
| public NLJsonSerializer() | |
| : base(new RFCDateTimeConverter()) | |
| { | |
| } | |
| } | |
| public class NLJavascriptSerializer : NLSerializer | |
| { | |
| public NLJavascriptSerializer() | |
| : base(new JavaScriptDateTimeConverter()) | |
| { | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment