0

I am trying to dequeue messages off of an audit queue that have been put there by the NServiceBus framework. The messages are Json Serialized and I am using RabbitMQ as a transport layer. As per the tutorials on the RabbitMQ site, I am doing the following:

var factory = new ConnectionFactory { HostName = "localhost" };
using (var connection = factory.CreateConnection())
{
    using (var channel = connection.CreateModel())
    {
        channel.QueueDeclare("audit", true, false, false, null);

        if (myType.IsNormalized()) { }

        var consumer = new QueueingBasicConsumer(channel);
        channel.BasicConsume("audit", true, consumer);

        while (true)
        {
            var eventArgs = (BasicDeliverEventArgs) consumer.Queue.Dequeue();
            var body = eventArgs.Body;

            var message = Encoding.UTF8.GetString(body);

            var assemblyQualifiedName = String.Concat(eventArgs.BasicProperties.Type,
                        ", Messages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
            var objectType = Type.GetType(assemblyQualifiedName);

            var deserializedMessage = JsonConvert.DeserializeObject(message, objectType);
        }
    }
}

This runs fine up until the last line, where it promptly breaks and tells me the JSON is not valid. When I debug and check the content of message, the JSON looks fine to me, however the Visual Studio JSON visualizer claims it is not. What is more bizarre is if I copy the exact content from the debug session and hard code it in, then try to deserialize it, it works fine. If I copy the content into an online JSON validator, it confirms the JSON is valid.

So my question is whether Encoding.UTF8.GetString() is doing something that can compromise my message? Is it introducing characters that might not be JSON friendly but could somehow be fixed via copy / paste.

The actual error thrown is: {"Unexpected character encountered while parsing value: . Path '', line 0, position 0."}

EDIT: Below is a sample of the JSON returned by Encoding.UTF8.GetString()

"{\"ProposalKey\":\"123-5\"}"

Corresponding to:

239 187 191 123 34 80 114 111 112 111 115 97 108 75 101 121 34 58 34 49 50 51 45 53 34 125 
mike
  • 1,318
  • 3
  • 21
  • 41
  • Can you post an example of the `JSON` before and after the `Encoding.UTF8.GetString()`? – Der Kommissar Jun 02 '15 at 20:39
  • Posted a sample byte array / json combo – mike Jun 02 '15 at 20:44
  • 1
    Are those `quote-escapes (\")` actually there, or are they quotes (`"`)? I recommend sending it to a `Console.WriteLine` or `Debug.Log` command to get the absolute string. – Der Kommissar Jun 02 '15 at 20:45
  • with what encoding eventArgs.Body was encoded, for a try you can give a try with utf-16 or unicode. – Dreamweaver Jun 02 '15 at 20:45
  • @EBrown the console prints the following: `?{"ProposalKey":"123-5"}` which certainly explains what's wrong. . .but I am not sure what this means, since I cannot see that `?` in the debugger – mike Jun 02 '15 at 20:50
  • 1
    Sounds like a [byte order mark](http://en.wikipedia.org/wiki/Byte_order_mark). – dbc Jun 02 '15 at 20:50
  • 1
    See here for advice about stripping the BOM, if that's what it is: http://stackoverflow.com/questions/2915182/how-do-i-ignore-the-utf-8-byte-order-marker-in-string-comparisons – dbc Jun 02 '15 at 20:52
  • 2
    Those first three bytes in your output are **definitely** a UTF-8 Byte-Order Mark. (They correspond to HEX: `EF BB BF` which is the UTF-8 accepted BOM.) That's likely your issue. (I thought they looked familiar, did not recognize them in decimal form.) Of course, UTF-8, by definition, does not need a BOM. But that's not the point. – Der Kommissar Jun 02 '15 at 20:52
  • And also here: http://stackoverflow.com/questions/1317700/strip-byte-order-mark-from-string-in-c-sharp – dbc Jun 02 '15 at 20:53
  • 1
    Awesome, thanks guys, if someone wants to post an answer I will accept it – mike Jun 02 '15 at 20:54

0 Answers0