Created
April 20, 2012 18:47
-
-
Save jchadwick/2430984 to your computer and use it in GitHub Desktop.
Revisions
-
jchadwick revised this gist
Apr 20, 2012 . 1 changed file with 21 additions and 5 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -6,13 +6,29 @@ public class JsonMessageFormatter : IMessageFormatter { private static readonly JsonSerializerSettings DefaultSerializerSettings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Objects }; private readonly JsonSerializerSettings _serializerSettings; public Encoding Encoding { get; set; } public JsonMessageFormatter(Encoding encoding = null) : this(encoding, null) { } internal JsonMessageFormatter(Encoding encoding, JsonSerializerSettings serializerSettings = null) { Encoding = encoding ?? Encoding.UTF8; _serializerSettings = serializerSettings ?? DefaultSerializerSettings; } public bool CanRead(Message message) { if (message == null) @@ -27,7 +43,7 @@ public bool CanRead(Message message) public object Clone() { return new JsonMessageFormatter(Encoding, _serializerSettings); } public object Read(Message message) @@ -41,7 +57,7 @@ public object Read(Message message) using (var reader = new StreamReader(message.BodyStream, Encoding)) { var json = reader.ReadToEnd(); return JsonConvert.DeserializeObject(json, _serializerSettings); } } @@ -53,7 +69,7 @@ public void Write(Message message, object obj) if (obj == null) throw new ArgumentNullException("obj"); string json = JsonConvert.SerializeObject(obj, Formatting.None, _serializerSettings); message.BodyStream = new MemoryStream(Encoding.GetBytes(json)); -
jchadwick revised this gist
Apr 20, 2012 . 1 changed file with 8 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -18,8 +18,11 @@ public bool CanRead(Message message) if (message == null) throw new ArgumentNullException("message"); var stream = message.BodyStream; return stream != null && stream.CanRead && stream.Length > 0; } public object Clone() @@ -32,6 +35,9 @@ public object Read(Message message) if (message == null) throw new ArgumentNullException("message"); if(CanRead(message) == false) return null; using (var reader = new StreamReader(message.BodyStream, Encoding)) { var json = reader.ReadToEnd(); -
jchadwick created this gist
Apr 20, 2012 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,58 @@ using System; using System.IO; using System.Messaging; using System.Text; using Newtonsoft.Json; public class JsonMessageFormatter : IMessageFormatter { public Encoding Encoding { get; set; } public JsonMessageFormatter() { Encoding = Encoding.UTF8; } public bool CanRead(Message message) { if (message == null) throw new ArgumentNullException("message"); return message.BodyStream != null && message.BodyStream.Length > 0; } public object Clone() { return new JsonMessageFormatter(); } public object Read(Message message) { if (message == null) throw new ArgumentNullException("message"); using (var reader = new StreamReader(message.BodyStream, Encoding)) { var json = reader.ReadToEnd(); return JsonConvert.DeserializeObject(json); } } public void Write(Message message, object obj) { if (message == null) throw new ArgumentNullException("message"); if (obj == null) throw new ArgumentNullException("obj"); string json = JsonConvert.SerializeObject(obj, Formatting.None); message.BodyStream = new MemoryStream(Encoding.GetBytes(json)); //Need to reset the body type, in case the same message //is reused by some other formatter. message.BodyType = 0; } }