<commit_before>using System;
using System.Collections.Generic;
using System.IO;

using Newtonsoft.Json;

namespace Machine.MassTransitExtensions.InterfacesAsMessages
{
  public class MessageInterfaceTransportFormatter : ITransportMessageBodyFormatter
  {
    private readonly MessageInterfaceImplementations _messageInterfaceImplementor;

    public MessageInterfaceTransportFormatter(MessageInterfaceImplementations messageInterfaceImplementor)
    {
      _messageInterfaceImplementor = messageInterfaceImplementor;
    }

    #region ITransportMessageBodyFormatter Members
    public void Serialize(IMessage[] messages, Stream stream)
    {
      using (StreamWriter writer = new StreamWriter(stream))
      {
        InterfaceMessageJsonConverter converter = new InterfaceMessageJsonConverter(_messageInterfaceImplementor, false);
        JsonSerializer serializer = new JsonSerializer();
        serializer.Converters.Add(converter);
        serializer.Serialize(writer, messages);
      }
    }

    public IMessage[] Deserialize(Stream stream)
    {
      using (StreamReader reader = new StreamReader(stream))
      {
        InterfaceMessageJsonConverter converter = new InterfaceMessageJsonConverter(_messageInterfaceImplementor, true);
        JsonSerializer serializer = new JsonSerializer();
        serializer.Converters.Add(converter);
        return (IMessage[])serializer.Deserialize(new JsonReader(reader), typeof(IMessage[]));
      }
    }
    #endregion
  }

  public class InterfaceMessageJsonConverter : JsonConverter
  {
    private static readonly string MessageTypePropertyName = "MessageType";
    private static readonly string MessageBodyPropertyName = "MessageBody";
    private readonly MessageInterfaceImplementations _messageInterfaceImplementor;
    private readonly bool _ignoreClassesBecauseWeAreReading;
    private bool _skipNext;

    public InterfaceMessageJsonConverter(MessageInterfaceImplementations messageInterfaceImplementor, bool reading)
    {
      _messageInterfaceImplementor = messageInterfaceImplementor;
      _ignoreClassesBecauseWeAreReading = reading;
    }

    public override bool CanConvert(Type objectType)
    {
      if (_skipNext)
      {
        return _skipNext = false;
      }
      if (typeof(IMessage).IsAssignableFrom(objectType))
      {
        return !_ignoreClassesBecauseWeAreReading || objectType.IsInterface;
      }
      return false;
    }

    public override object ReadJson(JsonReader reader, Type objectType)
    {
      if (reader.TokenType == JsonToken.Null)
      {
        return null;
      }
      reader.Read();
      System.Diagnostics.Debug.Assert(reader.Value.Equals(MessageTypePropertyName));
      
      reader.Read();
      string messageTypeName = reader.Value.ToString();
      
      reader.Read();
      System.Diagnostics.Debug.Assert(reader.Value.Equals(MessageBodyPropertyName));
     
      // Allow interfaces and non-interfaces...
      Type deserializeAs = Type.GetType(messageTypeName);
      if (deserializeAs.IsInterface)
      {
        deserializeAs = _messageInterfaceImplementor.GetClassFor(deserializeAs);
      }
      JsonSerializer serializer = new JsonSerializer();
      serializer.Converters.Add(this);
      object value = serializer.Deserialize(reader, deserializeAs);
      reader.Read();
      return value;
    }

    public override void WriteJson(JsonWriter writer, object value)
    {
      Type objectType = value.GetType();
      Type messageType = objectType;
      // Allow interfaces and non-interfaces...
      if (_messageInterfaceImplementor.IsClassOrInterface(objectType))
      {
        messageType = _messageInterfaceImplementor.GetInterfaceFor(value.GetType());
      }
      writer.WriteStartObject();
      writer.WritePropertyName(MessageTypePropertyName);
      writer.WriteValue(messageType.FullName);<commit_msg>Fixing a bug in how message type names are resolved.<commit_after>using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Newtonsoft.Json;

namespace Machine.MassTransitExtensions.InterfacesAsMessages
{
  public class MessageInterfaceTransportFormatter : ITransportMessageBodyFormatter
  {
    private readonly MessageInterfaceImplementations _messageInterfaceImplementor;

    public MessageInterfaceTransportFormatter(MessageInterfaceImplementations messageInterfaceImplementor)
    {
      _messageInterfaceImplementor = messageInterfaceImplementor;
    }

    #region ITransportMessageBodyFormatter Members
    public void Serialize(IMessage[] messages, Stream stream)
    {
      using (StreamWriter writer = new StreamWriter(stream))
      {
        InterfaceMessageJsonConverter converter = new InterfaceMessageJsonConverter(_messageInterfaceImplementor, false);
        JsonSerializer serializer = new JsonSerializer();
        serializer.Converters.Add(converter);
        serializer.Serialize(writer, messages);
      }
    }

    public IMessage[] Deserialize(Stream stream)
    {
      using (StreamReader reader = new StreamReader(stream))
      {
        InterfaceMessageJsonConverter converter = new InterfaceMessageJsonConverter(_messageInterfaceImplementor, true);
        JsonSerializer serializer = new JsonSerializer();
        serializer.Converters.Add(converter);
        return (IMessage[])serializer.Deserialize(new JsonReader(reader), typeof(IMessage[]));
      }
    }
    #endregion
  }

  public class InterfaceMessageJsonConverter : JsonConverter
  {
    private static readonly string MessageTypePropertyName = "MessageType";
    private static readonly string MessageBodyPropertyName = "MessageBody";
    private readonly MessageInterfaceImplementations _messageInterfaceImplementor;
    private readonly bool _ignoreClassesBecauseWeAreReading;
    private bool _skipNext;

    public InterfaceMessageJsonConverter(MessageInterfaceImplementations messageInterfaceImplementor, bool reading)
    {
      _messageInterfaceImplementor = messageInterfaceImplementor;
      _ignoreClassesBecauseWeAreReading = reading;
    }

    public override bool CanConvert(Type objectType)
    {
      if (_skipNext)
      {
        return _skipNext = false;
      }
      if (typeof(IMessage).IsAssignableFrom(objectType))
      {
        return !_ignoreClassesBecauseWeAreReading || objectType.IsInterface;
      }
      return false;
    }

    public override object ReadJson(JsonReader reader, Type objectType)
    {
      if (reader.TokenType == JsonToken.Null)
      {
        return null;
      }
      reader.Read();
      System.Diagnostics.Debug.Assert(reader.Value.Equals(MessageTypePropertyName));
      
      reader.Read();
      string messageTypeName = reader.Value.ToString();
      
      reader.Read();
      System.Diagnostics.Debug.Assert(reader.Value.Equals(MessageBodyPropertyName));
     
      // Allow interfaces and non-interfaces...
      Type deserializeAs = FindTypeNamed(messageTypeName);
      if (deserializeAs.IsInterface)
      {
        deserializeAs = _messageInterfaceImplementor.GetClassFor(deserializeAs);
      }
      JsonSerializer serializer = new JsonSerializer();
      serializer.Converters.Add(this);
      object value = serializer.Deserialize(reader, deserializeAs);
      reader.Read();
      return value;
    }

    private static Type FindTypeNamed(string name)
    {
      Type deserializeAs = Type.GetType(name);
      if (deserializeAs != null)
      {
        return deserializeAs;
      }
      foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
      {
        deserializeAs = assembly.GetType(name);
        if (deserializeAs != null && typeof(IMessage).IsAssignableFrom(deserializeAs))
        {
          return deserializeAs;
        }
      }
      throw new InvalidOperationException("Unable to find " + name);
    }

    public override void WriteJson(JsonWriter writer, object value)
    {
      Type objectType = value.GetType();
      Type messageType = objectType;
      // Allow interfaces and non-interfaces...
      if (_messageInterfaceImplementor.IsClassOrInterface(objectType))
      {
        messageType = _messageInterfaceImplementor.GetInterfaceFor(value.GetType());
      }
      writer.WriteStartObject();
      writer.WritePropertyName(MessageTypePropertyName);
      writer.WriteValue(messageType.FullName);
{'fraction_non_alphanumeric': 0.07478093866282559, 'fraction_numerical': 0.0, 'mean_word_length': 3.555494805904866, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4745965', 'n_tokens_mistral': 2043, 'n_tokens_neox': 2048, 'n_words': 428}