示例#1
0
        static TypeNameHandling GetTypeNameHandling(JsonInteroperabilityMode mode)
        {
            switch (mode)
            {
            case JsonInteroperabilityMode.FullTypeInformation:
                return(TypeNameHandling.All);

            case JsonInteroperabilityMode.PureJson:
                return(TypeNameHandling.None);

            default:
                throw new ArgumentOutOfRangeException(nameof(mode), mode, $"Unknown {typeof(JsonInteroperabilityMode).Name} value");
            }
        }
        ISerializer SnatchTheSerializer(JsonInteroperabilityMode mode)
        {
            ISerializer serializerToReturn = null;

            Configure.With(Using(new BuiltinHandlerActivator()))
            .Transport(t => t.UseInMemoryTransport(new InMemNetwork(), "who cares"))
            .Serialization(s => s.UseNewtonsoftJson(mode))
            .Options(o =>
            {
                o.Decorate(c =>
                {
                    var serializer     = c.Get <ISerializer>();
                    serializerToReturn = serializer;
                    return(serializer);
                });
            })
            .Start();

            return(serializerToReturn);
        }
        /// <summary>
        /// Configures Rebus to use Newtonsoft JSON.NET to serialize messages, using appropriate <see cref="JsonSerializerSettings"/>
        /// depending on the given <paramref name="mode"/>. Message bodies are UTF8-encoded.
        /// Passing <see cref="JsonInteroperabilityMode.FullTypeInformation"/> as the value for <paramref name="mode"/> is equivalent
        /// to calling <see cref="UseNewtonsoftJson(StandardConfigurer{ISerializer})"/> which in turn
        /// is equivalent to not explicitly configuring the serializer because it's the default serialization :)
        /// </summary>
        public static void UseNewtonsoftJson(this StandardConfigurer <ISerializer> configurer, JsonInteroperabilityMode mode)
        {
            if (configurer == null)
            {
                throw new ArgumentNullException(nameof(configurer));
            }

            TypeNameHandling GetTypeNameHandling()
            {
                switch (mode)
                {
                case JsonInteroperabilityMode.FullTypeInformation:
                    return(TypeNameHandling.All);

                case JsonInteroperabilityMode.PureJson:
                    return(TypeNameHandling.None);

                default:
                    throw new ArgumentOutOfRangeException(nameof(mode), mode, $"Unknown {typeof(JsonInteroperabilityMode).Name} value");
                }
            }

            var settings = new JsonSerializerSettings {
                TypeNameHandling = GetTypeNameHandling()
            };

            RegisterSerializer(configurer, settings, Encoding.UTF8);
        }
示例#4
0
        /// <summary>
        /// Configures Rebus to use Newtonsoft JSON.NET to serialize messages, using appropriate <see cref="JsonSerializerSettings"/>
        /// depending on the given <paramref name="mode"/>. Message bodies are UTF8-encoded.
        /// Passing <see cref="JsonInteroperabilityMode.FullTypeInformation"/> as the value for <paramref name="mode"/> is equivalent
        /// to calling <see cref="UseNewtonsoftJson(StandardConfigurer{ISerializer})"/> which in turn
        /// is equivalent to not explicitly configuring the serializer because it's the default serialization :)
        /// </summary>
        public static void UseNewtonsoftJson(this StandardConfigurer <ISerializer> configurer, JsonInteroperabilityMode mode)
        {
            if (configurer == null)
            {
                throw new ArgumentNullException(nameof(configurer));
            }

            var settings = new JsonSerializerSettings {
                TypeNameHandling = GetTypeNameHandling(mode)
            };

            RegisterSerializer(configurer, settings, Encoding.UTF8);
        }
 static TypeNameHandling GetTypeNameHandling(JsonInteroperabilityMode mode) => mode switch
 {