/// <summary>
        /// Initializes a new instance of the <see cref="SerializationDescriptionToSerializerFactory"/> class.
        /// </summary>
        /// <param name="supportedSerializationDescription"><see cref="SerializationDescription" /> that is supported by the serializer.</param>
        /// <param name="serializer"><see cref="ISerializeAndDeserialize" /> implementation to use.</param>
        public SerializationDescriptionToSerializerFactory(SerializationDescription supportedSerializationDescription, ISerializeAndDeserialize serializer)
        {
            new { supportedSerializationDescription }.Must().NotBeNull();
            new { serializer }.Must().NotBeNull();

            this.supportedSerializationDescription = supportedSerializationDescription;
            this.serializer = serializer;
        }
        /// <inheritdoc />
        public ISerializeAndDeserialize BuildSerializer(SerializationDescription serializationDescription, TypeMatchStrategy typeMatchStrategy = TypeMatchStrategy.NamespaceAndName, MultipleMatchStrategy multipleMatchStrategy = MultipleMatchStrategy.ThrowOnMultiple, UnregisteredTypeEncounteredStrategy unregisteredTypeEncounteredStrategy = UnregisteredTypeEncounteredStrategy.Default)
        {
            new { serializationDescription }.Must().NotBeNull();

            if (serializationDescription != this.supportedSerializationDescription)
            {
                throw new NotSupportedException(Invariant($"Supplied '{nameof(serializationDescription)}' ({serializationDescription}) does not match '{nameof(this.supportedSerializationDescription)}' ({this.supportedSerializationDescription})."));
            }

            return(this.serializer);
        }
        public static DescribedSerialization ToDescribedSerializationUsingSpecificSerializer <T>(
            this T objectToPackageIntoDescribedSerialization,
            SerializationDescription serializationDescription,
            ISerialize serializer,
            ICompress compressor = null)
        {
            new { serializationDescription }.Must().NotBeNull();
            new { serializer }.Must().NotBeNull();

            var localCompressor = compressor ?? new NullCompressor();

            localCompressor.CompressionKind.Named(Invariant($"{nameof(serializationDescription)}.{nameof(serializationDescription.CompressionKind)}-Must-match-{nameof(compressor)}.{nameof(compressor.CompressionKind)}")).Must().BeEqualTo(serializationDescription.CompressionKind);

            string payload;

            switch (serializationDescription.SerializationFormat)
            {
            case SerializationFormat.Binary:
                var rawBytes        = serializer.SerializeToBytes(objectToPackageIntoDescribedSerialization);
                var compressedBytes = localCompressor.CompressBytes(rawBytes);
                payload = Convert.ToBase64String(compressedBytes);
                break;

            case SerializationFormat.String:
                payload = serializer.SerializeToString(objectToPackageIntoDescribedSerialization);
                break;

            default: throw new NotSupportedException(Invariant($"{nameof(SerializationFormat)} - {serializationDescription.SerializationFormat} is not supported."));
            }

            var payloadType = objectToPackageIntoDescribedSerialization?.GetType() ?? typeof(T);

            if (payloadType.IsAnonymous())
            {
                payloadType = typeof(DynamicTypePlaceholder);
            }

            var ret = new DescribedSerialization(
                payloadType.ToRepresentation(),
                payload,
                serializationDescription);

            return(ret);
        }
        public static DescribedSerialization ToDescribedSerializationUsingSpecificFactory <T>(
            this T objectToPackageIntoDescribedSerialization,
            SerializationDescription serializationDescription,
            ISerializerFactory serializerFactory,
            ICompressorFactory compressorFactory,
            TypeMatchStrategy typeMatchStrategy         = TypeMatchStrategy.NamespaceAndName,
            MultipleMatchStrategy multipleMatchStrategy = MultipleMatchStrategy.ThrowOnMultiple,
            UnregisteredTypeEncounteredStrategy unregisteredTypeEncounteredStrategy = UnregisteredTypeEncounteredStrategy.Default)
        {
            new { serializationDescription }.Must().NotBeNull();
            new { serializerFactory }.Must().NotBeNull();
            new { compressorFactory }.Must().NotBeNull();

            var serializer = serializerFactory.BuildSerializer(serializationDescription, typeMatchStrategy, multipleMatchStrategy, unregisteredTypeEncounteredStrategy);
            var compressor = compressorFactory.BuildCompressor(serializationDescription.CompressionKind);

            var ret = objectToPackageIntoDescribedSerialization.ToDescribedSerializationUsingSpecificSerializer(
                serializationDescription,
                serializer,
                compressor);

            return(ret);
        }
示例#5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DescribedSerialization"/> class.
        /// </summary>
        /// <param name="payloadTypeRepresentation">A description of the type of object serialized.</param>
        /// <param name="serializedPayload">The object serialized to a string.</param>
        /// <param name="serializationDescription">The serializer used to generate the payload.</param>
        /// <exception cref="ArgumentNullException"><paramref name="payloadTypeRepresentation"/> is null.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="serializedPayload"/> is null.</exception>
        /// <exception cref="ArgumentException"><paramref name="serializedPayload"/> is whitespace.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="serializationDescription"/> is null.</exception>
        /// <exception cref="ArgumentException"><paramref name="serializationDescription"/> is whitespace.</exception>
        public DescribedSerialization(TypeRepresentation payloadTypeRepresentation, string serializedPayload, SerializationDescription serializationDescription)
        {
            new { payloadTypeRepresentation }.Must().NotBeNull();
            new { serializationDescription }.Must().NotBeNull();

            this.PayloadTypeRepresentation = payloadTypeRepresentation;
            this.SerializedPayload         = serializedPayload;
            this.SerializationDescription  = serializationDescription;
        }