Skip to content

Commit

Permalink
Patching v3 to support v4+ unwrapped messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
distantcam committed Jun 19, 2014
1 parent f606444 commit 0ea5c06
Showing 1 changed file with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
using System.Globalization;
using System;
using System.Globalization;
using System.IO;
using System.Runtime.Serialization.Formatters;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using NServiceBus.MessageInterfaces;
using NServiceBus.Serialization;
using NServiceBus.Serializers.Json.Internal;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace NServiceBus.Serializers.Json
{
public abstract class JsonMessageSerializerBase : IMessageSerializer
{
// From v4+ we just look for it, but don't expect it
const string EnclosedMessageTypes = "NServiceBus.EnclosedMessageTypes";

private readonly IMessageMapper _messageMapper;

protected JsonMessageSerializerBase(IMessageMapper messageMapper)
Expand All @@ -26,7 +30,7 @@ public JsonSerializerSettings JsonSerializerSettings
{
TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple,
TypeNameHandling = TypeNameHandling.Auto,
Converters = { new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.RoundtripKind} }
Converters = { new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.RoundtripKind } }
};
return serializerSettings;
}
Expand All @@ -49,6 +53,23 @@ public object[] Deserialize(Stream stream)
jsonSerializer.ContractResolver = new MessageContractResolver(_messageMapper);

JsonReader reader = CreateJsonReader(stream);
reader.Read();

JsonToken firstTokenType = reader.TokenType;

if (firstTokenType != JsonToken.StartArray)
{
string messageTypes;

var bus = Configure.Instance.Builder.Build<IBus>();
if (bus != null &&
bus.CurrentMessageContext != null &&
bus.CurrentMessageContext.Headers != null &&
bus.CurrentMessageContext.Headers.TryGetValue(EnclosedMessageTypes, out messageTypes))
{
return new[] { jsonSerializer.Deserialize(reader, Type.GetType(messageTypes.Split(';')[0])) };
}
}

return jsonSerializer.Deserialize<object[]>(reader);
}
Expand Down

0 comments on commit 0ea5c06

Please sign in to comment.