예제 #1
0
        public string SerializeToString(IRequest req, object response)
        {
            var contentType = ContentFormat.NormalizeContentType(req.ResponseContentType);

            if (ContentTypeStringSerializers.TryGetValue(contentType, out var stringSerializer))
            {
                return(stringSerializer(req, response));
            }

            var responseStreamWriter = GetStreamSerializer(contentType);

            if (responseStreamWriter != null)
            {
                using (var ms = MemoryStreamFactory.GetStream())
                {
                    responseStreamWriter(req, response, ms);
                    return(ms.ReadToEnd());
                }
            }

            var responseWriterAsync = GetStreamSerializerAsync(contentType);

            if (responseWriterAsync != null)
            {
                using (var ms = MemoryStreamFactory.GetStream())
                {
                    responseWriterAsync(req, response, ms).Wait();
                    return(ms.ReadToEnd());
                }
            }

            throw new NotSupportedException(ErrorMessages.ContentTypeNotSupported.Fmt(contentType));
        }
예제 #2
0
        public byte[] SerializeToBytes(IRequest req, object response)
        {
            var contentType = ContentFormat.NormalizeContentType(req.ResponseContentType);

            if (ContentTypeSerializers.TryGetValue(contentType, out var responseStreamWriter))
            {
                using (var ms = MemoryStreamFactory.GetStream())
                {
                    responseStreamWriter(req, response, ms);
                    ms.Position = 0;
                    return(ms.ToArray());
                }
            }

            if (ContentTypeSerializersAsync.TryGetValue(contentType, out var responseWriterAsync))
            {
                using (var ms = MemoryStreamFactory.GetStream())
                {
                    responseWriterAsync(req, response, ms).Wait();
                    ms.Position = 0;
                    return(ms.ToArray());
                }
            }

            throw new NotSupportedException(ErrorMessages.ContentTypeNotSupported.Fmt(contentType));
        }
예제 #3
0
        public StreamSerializerDelegateAsync GetStreamSerializerAsync(string contentType)
        {
            contentType = ContentFormat.NormalizeContentType(contentType);

            if (ContentTypeSerializersAsync.TryGetValue(contentType, out var serializerAsync))
            {
                return(serializerAsync);
            }

            var serializer = GetStreamSerializer(contentType);

            if (serializer == null)
            {
                return(UnknownContentTypeSerializer);
            }

            return((httpReq, dto, stream) =>
            {
                if (HostContext.Config.BufferSyncSerializers)
                {
                    using (var ms = MemoryStreamFactory.GetStream())
                    {
                        serializer(httpReq, dto, ms);
                        ms.Position = 0;
                        return ms.CopyToAsync(stream);
                    }
                }

                httpReq.Response.AllowSyncIO();
                serializer(httpReq, dto, stream);
                return TypeConstants.EmptyTask;
            });
        }
예제 #4
0
        public void Register(string contentType, StreamSerializerDelegate streamSerializer, StreamDeserializerDelegate streamDeserializer)
        {
            if (contentType.IsNullOrEmpty())
            {
                throw new ArgumentNullException(nameof(contentType));
            }

            var format = contentType.LastRightPart('/');

            var normalizedContentType = ContentFormat.NormalizeContentType(contentType);

            ContentTypeFormats[format] = normalizedContentType;

            SetContentTypeSerializer(normalizedContentType, streamSerializer);
            SetContentTypeDeserializer(normalizedContentType, streamDeserializer);
        }
예제 #5
0
        public void RegisterAsync(string contentType, StreamSerializerDelegateAsync streamSerializer, StreamDeserializerDelegateAsync streamDeserializer)
        {
            if (contentType.IsNullOrEmpty())
            {
                throw new ArgumentNullException(nameof(contentType));
            }

            var format = ContentFormat.GetContentFormat(contentType);

            var normalizedContentType = ContentFormat.NormalizeContentType(contentType);

            ContentTypeFormats[format] = normalizedContentType;

            ContentTypeSerializersAsync[normalizedContentType]   = streamSerializer;
            ContentTypeDeserializersAsync[normalizedContentType] = streamDeserializer;
        }
예제 #6
0
        public StreamDeserializerDelegateAsync GetStreamDeserializerAsync(string contentType)
        {
            contentType = ContentFormat.NormalizeContentType(contentType);

            if (ContentTypeDeserializersAsync.TryGetValue(contentType, out var deserializerAsync))
            {
                return(deserializerAsync);
            }

            var deserializer = GetStreamDeserializer(contentType);

            if (deserializer == null)
            {
                return(null);
            }

            return((type, stream) => Task.FromResult(deserializer(type, stream)));
        }
예제 #7
0
        public StreamSerializerDelegateAsync GetStreamSerializerAsync(string contentType)
        {
            contentType = ContentFormat.NormalizeContentType(contentType);

            if (ContentTypeSerializersAsync.TryGetValue(contentType, out var serializerAsync))
            {
                return(serializerAsync);
            }

            var serializer = GetStreamSerializer(contentType);

            if (serializer == null)
            {
                return(UnknownContentTypeSerializer);
            }

            return((httpReq, dto, stream) =>
                   serializeSync(serializer, httpReq, dto, stream));
        }
예제 #8
0
        public Task SerializeToStreamAsync(IRequest req, object response, Stream responseStream)
        {
            var contentType = ContentFormat.NormalizeContentType(req.ResponseContentType);

            var serializer = GetStreamSerializer(contentType);

            if (serializer != null)
            {
                return(serializeSync(serializer, req, response, responseStream));
            }

            var serializerAsync = GetStreamSerializerAsync(contentType);

            if (serializerAsync != null)
            {
                return(serializerAsync(req, response, responseStream));
            }

            throw new NotSupportedException(ErrorMessages.ContentTypeNotSupported.Fmt(contentType));
        }
예제 #9
0
        public object DeserializeFromStream(string contentType, Type type, Stream fromStream)
        {
            contentType = ContentFormat.NormalizeContentType(contentType);

            var deserializer = GetStreamDeserializer(contentType);

            if (deserializer != null)
            {
                return(deserializer(type, fromStream));
            }

            var deserializerAsync = GetStreamDeserializerAsync(contentType);

            if (deserializerAsync != null)
            {
                var task = deserializerAsync(type, fromStream);
                return(task.Result);
            }

            throw new NotSupportedException(ErrorMessages.ContentTypeNotSupported.Fmt(contentType));
        }
예제 #10
0
        public void Remove(string contentType)
        {
            contentType = ContentFormat.NormalizeContentType(contentType);

            ContentTypeFormats.Remove(contentType);

            ContentTypeSerializers.Remove(contentType);
            ContentTypeSerializersAsync.Remove(contentType);
            ContentTypeStringSerializers.Remove(contentType);

            ContentTypeDeserializers.Remove(contentType);
            ContentTypeDeserializersAsync.Remove(contentType);
            ContentTypeStringDeserializers.Remove(contentType);

            if (contentType == MimeTypes.Xml)
            {
                //"text/xml; charset=utf-8" also matches xml
                ContentTypeSerializers.Remove(MimeTypes.Soap11);
                ContentTypeDeserializers.Remove(MimeTypes.Soap11);
            }
        }
예제 #11
0
        public string SerializeToString(IRequest req, object response)
        {
            var contentType = ContentFormat.NormalizeContentType(req.ResponseContentType);

            if (ContentTypeStringSerializers.TryGetValue(contentType, out var stringSerializer))
            {
                return(stringSerializer(req, response));
            }

            var responseStreamWriter = GetStreamSerializer(contentType);

            if (responseStreamWriter != null)
            {
                using (var ms = MemoryStreamFactory.GetStream())
                {
                    responseStreamWriter(req, response, ms);

                    ms.Position = 0;
                    var result = new StreamReader(ms, UTF8EncodingWithoutBom).ReadToEnd();
                    return(result);
                }
            }

            var responseWriterAsync = GetStreamSerializerAsync(contentType);

            if (responseWriterAsync != null)
            {
                using (var ms = MemoryStreamFactory.GetStream())
                {
                    responseWriterAsync(req, response, ms).Wait();

                    var bytes  = ms.ToArray();
                    var result = bytes.FromUtf8Bytes();

                    return(result);
                }
            }

            throw new NotSupportedException(ErrorMessages.ContentTypeNotSupported.Fmt(contentType));
        }
예제 #12
0
        public object DeserializeFromString(string contentType, Type type, string request)
        {
            contentType = ContentFormat.NormalizeContentType(contentType);

            if (ContentTypeStringDeserializers.TryGetValue(contentType, out var stringDeserializer))
            {
                return(stringDeserializer(request, type));
            }

            var deserializerAsync = GetStreamDeserializerAsync(contentType);

            if (deserializerAsync != null)
            {
                using (var ms = MemoryStreamFactory.GetStream(request.ToUtf8Bytes()))
                {
                    var task = deserializerAsync(type, ms);
                    return(task.Result);
                }
            }

            throw new NotSupportedException(ErrorMessages.ContentTypeNotSupported.Fmt(contentType));
        }
예제 #13
0
        public StreamSerializerDelegateAsync GetStreamSerializerAsync(string contentType)
        {
            contentType = ContentFormat.NormalizeContentType(contentType);

            if (ContentTypeSerializersAsync.TryGetValue(contentType, out var serializerAsync))
            {
                return(serializerAsync);
            }

            var serializer = GetStreamSerializer(contentType);

            if (serializer == null)
            {
                return(null);
            }

            return((httpReq, dto, stream) =>
            {
                serializer(httpReq, dto, stream);
                return TypeConstants.EmptyTask;
            });
        }
예제 #14
0
        public void SerializeToStream(IRequest req, object response, Stream responseStream)
        {
            var contentType = ContentFormat.NormalizeContentType(req.ResponseContentType);

            var serializer = GetStreamSerializer(contentType);

            if (serializer != null)
            {
                serializer(req, response, responseStream);
                return;
            }

            var serializerAsync = GetStreamSerializerAsync(contentType);

            if (serializerAsync != null)
            {
                var task = serializerAsync(req, response, responseStream);
                task.Wait();
                return;
            }

            throw new NotSupportedException(ErrorMessages.ContentTypeNotSupported.Fmt(contentType));
        }
예제 #15
0
 public StreamDeserializerDelegate GetStreamDeserializer(string contentType)
 {
     return(ContentTypeDeserializers.TryGetValue(ContentFormat.NormalizeContentType(contentType), out var deserializer)
         ? deserializer
         : null);
 }
예제 #16
0
 public void SetContentTypeDeserializer(string contentType, StreamDeserializerDelegate streamDeserializer)
 {
     ContentTypeDeserializers[ContentFormat.NormalizeContentType(contentType)] = streamDeserializer;
 }