public static object DeserializePayloadUsingSpecificSerializer(
            this DescribedSerialization describedSerialization,
            IDeserialize deserializer,
            IDecompress decompressor                    = null,
            TypeMatchStrategy typeMatchStrategy         = TypeMatchStrategy.NamespaceAndName,
            MultipleMatchStrategy multipleMatchStrategy = MultipleMatchStrategy.ThrowOnMultiple)
        {
            new { describedSerialization }.Must().NotBeNull();
            new { deserializer }.Must().NotBeNull();

            var localDecompressor = decompressor ?? new NullCompressor();
            var targetType        = describedSerialization.PayloadTypeRepresentation.ResolveFromLoadedTypes(typeMatchStrategy, multipleMatchStrategy);

            object ret;

            switch (describedSerialization.SerializationDescription.SerializationFormat)
            {
            case SerializationFormat.Binary:
                var rawBytes          = Convert.FromBase64String(describedSerialization.SerializedPayload);
                var decompressedBytes = localDecompressor.DecompressBytes(rawBytes);
                ret = deserializer.Deserialize(decompressedBytes, targetType);
                break;

            case SerializationFormat.String:
                ret = deserializer.Deserialize(describedSerialization.SerializedPayload, targetType);
                break;

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

            return(ret);
        }
 public static T DeserializePayloadUsingSpecificFactory <T>(
     this DescribedSerialization describedSerialization,
     ISerializerFactory serializerFactory,
     ICompressorFactory compressorFactory,
     TypeMatchStrategy typeMatchStrategy         = TypeMatchStrategy.NamespaceAndName,
     MultipleMatchStrategy multipleMatchStrategy = MultipleMatchStrategy.ThrowOnMultiple,
     UnregisteredTypeEncounteredStrategy unregisteredTypeEncounteredStrategy = UnregisteredTypeEncounteredStrategy.Default)
 {
     return((T)DeserializePayloadUsingSpecificFactory(describedSerialization, serializerFactory, compressorFactory, typeMatchStrategy, multipleMatchStrategy, unregisteredTypeEncounteredStrategy));
 }
        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 object DeserializePayloadUsingSpecificFactory(
            this DescribedSerialization describedSerialization,
            ISerializerFactory serializerFactory,
            ICompressorFactory compressorFactory,
            TypeMatchStrategy typeMatchStrategy         = TypeMatchStrategy.NamespaceAndName,
            MultipleMatchStrategy multipleMatchStrategy = MultipleMatchStrategy.ThrowOnMultiple,
            UnregisteredTypeEncounteredStrategy unregisteredTypeEncounteredStrategy = UnregisteredTypeEncounteredStrategy.Default)
        {
            new { describedSerialization }.Must().NotBeNull();
            new { serializerFactory }.Must().NotBeNull();
            new { compressorFactory }.Must().NotBeNull();

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

            var ret = describedSerialization.DeserializePayloadUsingSpecificSerializer(serializer, compressor, typeMatchStrategy, multipleMatchStrategy);

            return(ret);
        }