Skip to content

Instantly share code, notes, and snippets.

@jchadwick
Created April 20, 2012 18:47
Show Gist options
  • Save jchadwick/2430984 to your computer and use it in GitHub Desktop.
Save jchadwick/2430984 to your computer and use it in GitHub Desktop.

Revisions

  1. jchadwick revised this gist Apr 20, 2012. 1 changed file with 21 additions and 5 deletions.
    26 changes: 21 additions & 5 deletions JsonMessageFormatter.cs
    Original 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()

    public JsonMessageFormatter(Encoding encoding = null)
    : this(encoding, null)
    {
    Encoding = Encoding.UTF8;
    }

    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();
    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);
    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);
    string json = JsonConvert.SerializeObject(obj, Formatting.None, _serializerSettings);

    message.BodyStream = new MemoryStream(Encoding.GetBytes(json));

  2. jchadwick revised this gist Apr 20, 2012. 1 changed file with 8 additions and 2 deletions.
    10 changes: 8 additions & 2 deletions JsonMessageFormatter.cs
    Original 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");

    return message.BodyStream != null
    && message.BodyStream.Length > 0;
    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();
  3. jchadwick created this gist Apr 20, 2012.
    58 changes: 58 additions & 0 deletions JsonMessageFormatter.cs
    Original 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;
    }
    }