Пример #1
0
        /// <summary>Serialize to string.</summary>
        ///
        /// <exception cref="NotSupportedException">Thrown when the requested operation is not supported.</exception>
        ///
        /// <param name="requestContext">Context for the request.</param>
        /// <param name="response">      The response.</param>
        ///
        /// <returns>A string.</returns>
        public string SerializeToString(IRequestContext requestContext, object response)
        {
            var contentType = requestContext.ResponseContentType;

            StreamSerializerDelegate responseStreamWriter;

            if (this.ContentTypeSerializers.TryGetValue(contentType, out responseStreamWriter) ||
                this.ContentTypeSerializers.TryGetValue(ContentType.GetRealContentType(contentType), out responseStreamWriter))
            {
                using (var ms = new MemoryStream())
                {
                    responseStreamWriter(requestContext, response, ms);

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

            ResponseSerializerDelegate responseWriter;

            if (this.ContentTypeResponseSerializers.TryGetValue(contentType, out responseWriter) ||
                this.ContentTypeResponseSerializers.TryGetValue(ContentType.GetRealContentType(contentType), out responseWriter))
            {
                using (var ms = new MemoryStream())
                {
                    var httpRes = new HttpResponseStreamWrapper(ms)
                    {
                        KeepOpen = true, //Don't let view engines close the OutputStream
                    };
                    responseWriter(requestContext, response, httpRes);

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

                    httpRes.ForceClose(); //Manually close the OutputStream

                    return(result);
                }
            }


            var contentTypeAttr = ContentType.GetEndpointAttributes(contentType);

            switch (contentTypeAttr)
            {
            case EndpointAttributes.Xml:
                return(XmlSerializer.SerializeToString(response));

            case EndpointAttributes.Json:
                return(JsonDataContractSerializer.Instance.SerializeToString(response));

            case EndpointAttributes.Jsv:
                return(TypeSerializer.SerializeToString(response));
            }

            throw new NotSupportedException("ContentType not supported: " + contentType);
        }
Пример #2
0
        /// <summary>Serialize to stream.</summary>
        ///
        /// <exception cref="NotSupportedException">Thrown when the requested operation is not supported.</exception>
        ///
        /// <param name="requestContext">Context for the request.</param>
        /// <param name="response">      The response.</param>
        /// <param name="responseStream">The response stream.</param>
        public void SerializeToStream(IRequestContext requestContext, object response, Stream responseStream)
        {
            var contentType = requestContext.ResponseContentType;
            var serializer  = GetResponseSerializer(contentType);

            if (serializer == null)
            {
                throw new NotSupportedException("ContentType not supported: " + contentType);
            }

            var httpRes = new HttpResponseStreamWrapper(responseStream);

            serializer(requestContext, response, httpRes);
        }
Пример #3
0
        /// <summary>Serialize to bytes.</summary>
        ///
        /// <exception cref="NotSupportedException">Thrown when the requested operation is not supported.</exception>
        ///
        /// <param name="requestContext">Context for the request.</param>
        /// <param name="response">      The response.</param>
        ///
        /// <returns>A byte[].</returns>
        public byte[] SerializeToBytes(IRequestContext requestContext, object response)
        {
            var contentType = requestContext.ResponseContentType;

            StreamSerializerDelegate responseStreamWriter;

            if (this.ContentTypeSerializers.TryGetValue(contentType, out responseStreamWriter) ||
                this.ContentTypeSerializers.TryGetValue(ContentType.GetRealContentType(contentType), out responseStreamWriter))
            {
                using (var ms = new MemoryStream())
                {
                    responseStreamWriter(requestContext, response, ms);
                    ms.Position = 0;
                    return(ms.ToArray());
                }
            }

            ResponseSerializerDelegate responseWriter;

            if (this.ContentTypeResponseSerializers.TryGetValue(contentType, out responseWriter) ||
                this.ContentTypeResponseSerializers.TryGetValue(ContentType.GetRealContentType(contentType), out responseWriter))
            {
                using (var ms = new MemoryStream())
                {
                    var httpRes = new HttpResponseStreamWrapper(ms);
                    responseWriter(requestContext, response, httpRes);
                    ms.Position = 0;
                    return(ms.ToArray());
                }
            }

            var contentTypeAttr = ContentType.GetEndpointAttributes(contentType);

            switch (contentTypeAttr)
            {
            case EndpointAttributes.Xml:
                return(XmlSerializer.SerializeToString(response).ToUtf8Bytes());

            case EndpointAttributes.Json:
                return(JsonDataContractSerializer.Instance.SerializeToString(response).ToUtf8Bytes());

            case EndpointAttributes.Jsv:
                return(TypeSerializer.SerializeToString(response).ToUtf8Bytes());
            }

            throw new NotSupportedException("ContentType not supported: " + contentType);
        }
Пример #4
0
        /// <summary>Serialize to stream.</summary>
        ///
        /// <exception cref="NotSupportedException">Thrown when the requested operation is not supported.</exception>
        ///
        /// <param name="requestContext">Context for the request.</param>
        /// <param name="response">      The response.</param>
        /// <param name="responseStream">The response stream.</param>
        public void SerializeToStream(IRequestContext requestContext, object response, Stream responseStream)
        {
            var contentType = requestContext.ResponseContentType;
            var serializer = GetResponseSerializer(contentType);
            if (serializer == null)
                throw new NotSupportedException("ContentType not supported: " + contentType);

            var httpRes = new HttpResponseStreamWrapper(responseStream);
            serializer(requestContext, response, httpRes);
        }
Пример #5
0
        /// <summary>Serialize to string.</summary>
        ///
        /// <exception cref="NotSupportedException">Thrown when the requested operation is not supported.</exception>
        ///
        /// <param name="requestContext">Context for the request.</param>
        /// <param name="response">      The response.</param>
        ///
        /// <returns>A string.</returns>
        public string SerializeToString(IRequestContext requestContext, object response)
        {
            var contentType = requestContext.ResponseContentType;

            StreamSerializerDelegate responseStreamWriter;
            if (this.ContentTypeSerializers.TryGetValue(contentType, out responseStreamWriter) ||
                this.ContentTypeSerializers.TryGetValue(ContentType.GetRealContentType(contentType), out responseStreamWriter))
            {
                using (var ms = new MemoryStream())
                {
                    responseStreamWriter(requestContext, response, ms);

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

            ResponseSerializerDelegate responseWriter;
            if (this.ContentTypeResponseSerializers.TryGetValue(contentType, out responseWriter) ||
                this.ContentTypeResponseSerializers.TryGetValue(ContentType.GetRealContentType(contentType), out responseWriter))
            {
                using (var ms = new MemoryStream())
                {

                    var httpRes = new HttpResponseStreamWrapper(ms) {
                        KeepOpen = true, //Don't let view engines close the OutputStream
                    };
                    responseWriter(requestContext, response, httpRes);

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

                    httpRes.ForceClose(); //Manually close the OutputStream

                    return result;
                }
            }


            var contentTypeAttr = ContentType.GetEndpointAttributes(contentType);
            switch (contentTypeAttr)
            {
                case EndpointAttributes.Xml:
                    return XmlSerializer.SerializeToString(response);

                case EndpointAttributes.Json:
                    return JsonDataContractSerializer.Instance.SerializeToString(response);

                case EndpointAttributes.Jsv:
                    return TypeSerializer.SerializeToString(response);
            }

            throw new NotSupportedException("ContentType not supported: " + contentType);
        }
Пример #6
0
        /// <summary>Serialize to bytes.</summary>
        ///
        /// <exception cref="NotSupportedException">Thrown when the requested operation is not supported.</exception>
        ///
        /// <param name="requestContext">Context for the request.</param>
        /// <param name="response">      The response.</param>
        ///
        /// <returns>A byte[].</returns>
        public byte[] SerializeToBytes(IRequestContext requestContext, object response)
        {
            var contentType = requestContext.ResponseContentType;

            StreamSerializerDelegate responseStreamWriter;
            if (this.ContentTypeSerializers.TryGetValue(contentType, out responseStreamWriter) ||
                this.ContentTypeSerializers.TryGetValue(ContentType.GetRealContentType(contentType), out responseStreamWriter))
            {
                using (var ms = new MemoryStream())
                {
                    responseStreamWriter(requestContext, response, ms);
                    ms.Position = 0;
                    return ms.ToArray();
                }
            }

            ResponseSerializerDelegate responseWriter;
            if (this.ContentTypeResponseSerializers.TryGetValue(contentType, out responseWriter) ||
                this.ContentTypeResponseSerializers.TryGetValue(ContentType.GetRealContentType(contentType), out responseWriter))
            {
                using (var ms = new MemoryStream())
                {
                    var httpRes = new HttpResponseStreamWrapper(ms);
                    responseWriter(requestContext, response, httpRes);
                    ms.Position = 0;
                    return ms.ToArray();
                }
            }

            var contentTypeAttr = ContentType.GetEndpointAttributes(contentType);
            switch (contentTypeAttr)
            {
                case EndpointAttributes.Xml:
                    return XmlSerializer.SerializeToString(response).ToUtf8Bytes();

                case EndpointAttributes.Json:
                    return JsonDataContractSerializer.Instance.SerializeToString(response).ToUtf8Bytes();

                case EndpointAttributes.Jsv:
                    return TypeSerializer.SerializeToString(response).ToUtf8Bytes();
            }

            throw new NotSupportedException("ContentType not supported: " + contentType);
        }