Ejemplo n.º 1
0
        public static void Serialize(object data, Stream stream, DataFormat format)
        {
            switch (format)
            {
            case DataFormat.JSON:
                WrappedJsonSerializer.Instance.SerializeToStream(data, stream);
                break;

            case DataFormat.XML:
                WrappedXmlSerializer.SerializeToStream(data, stream);
                break;

            case DataFormat.ProtoBuf:
                Serializer.NonGeneric.Serialize(stream, data);
                break;

            case DataFormat.BJJSON:
                jsonSerializer.Serialize(data, stream);
                break;

            case DataFormat.BJBIN:
                binarySerializer.Serialize(data, stream);
                break;

            default:
                throw new NotSupportedException(format + " is not supported.");
            }
        }
Ejemplo n.º 2
0
        public static object Deserialize(Type type, Stream stream, DataFormat format)
        {
            object data = null;

            switch (format)
            {
            case DataFormat.JSON:
                data = WrappedJsonDeserializer.Instance.DeserializeFromStream(type, stream);
                break;

            case DataFormat.XML:
                data = WrappedXmlSerializer.DeserializeFromStream(type, stream);
                break;

            case DataFormat.ProtoBuf:
                data = Serializer.NonGeneric.Deserialize(type, stream);
                break;

            case DataFormat.BJJSON:
                data = jsonSerializer.Deserialize(type, stream);
                break;

            case DataFormat.BJBIN:
                data = binarySerializer.Deserialize(type, stream);
                break;

            default:
                throw new NotSupportedException(format + " is not supported.");
            }

            return(data);
        }
Ejemplo n.º 3
0
        public StreamSerializerDelegate GetStreamSerializer(string contentType)
        {
            StreamSerializerDelegate responseWriter;

            if (this.ContentTypeSerializers.TryGetValue(contentType, out responseWriter) ||
                this.ContentTypeSerializers.TryGetValue(ContentType.GetRealContentType(contentType), out responseWriter))
            {
                return(responseWriter);
            }

            var contentTypeAttr = ContentType.GetEndpointAttributes(contentType);

            switch (contentTypeAttr)
            {
            case EndpointAttributes.Xml:
                return((r, o, s) => WrappedXmlSerializer.SerializeToStream(o, s));

            case EndpointAttributes.Json:
                return((r, o, s) => WrappedJsonSerializer.Instance.SerializeToStream(o, s));

            case EndpointAttributes.Jsv:
                return((r, o, s) => TypeSerializer.SerializeToStream(o, s));
            }

            return(null);
        }
Ejemplo n.º 4
0
        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(WrappedXmlSerializer.SerializeToString(response, false));

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

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

            throw new NotSupportedException("ContentType not supported: " + contentType);
        }
Ejemplo n.º 5
0
 private string XmlSerializeToString(object obj)
 {
     try
     {
         return(WrappedXmlSerializer.SerializeToString(obj, true));
     }
     catch (Exception)
     {
         return("");
     }
 }
Ejemplo n.º 6
0
        public object ExecuteText(string operationName, string requestXml, Type requestType, IRequestContext requestContext)
        {
            var          request = WrappedXmlSerializer.DeserializeFromString(requestXml, requestType);
            IHttpRequest httpReq = requestContext.Get <IHttpRequest>();

            if (httpReq != null)
            {
                httpReq.RequestObject = request;
            }
            var response    = Execute(operationName, request, requestContext);
            var responseXml = WrappedXmlSerializer.SerializeToString(response, false);

            return(responseXml);
        }
Ejemplo n.º 7
0
        protected override string CreateMessage(Type dtoType)
        {
            var requestObj   = ReflectionUtils.PopulateObject(Activator.CreateInstance(dtoType));
            var xml          = WrappedXmlSerializer.SerializeToStringWithoutXmlDeclaration(requestObj, true);
            var soapEnvelope = string.Format(@"<?xml version=""1.0"" encoding=""utf-8""?>
<soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
    <soap:Body>

{0}

    </soap:Body>
</soap:Envelope>", xml);

            return(soapEnvelope);
        }
Ejemplo n.º 8
0
        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(WrappedXmlSerializer.SerializeToString(response, false).ToUtf8Bytes());

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

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

            throw new NotSupportedException("ContentType not supported: " + contentType);
        }
Ejemplo n.º 9
0
        public object GetRequest(IHttpRequest httpReq, string operationName)
        {
            var requestType = GetRequestType(operationName, httpReq.ServicePath);

            AssertOperationExists(operationName, requestType);

            if (httpReq.ContentLength <= 0)
            {
                throw new ArgumentNullException("Missing request body");
            }

            try
            {
                // Deserialize to soap11 envelope
                var soap11Envelope = WrappedXmlSerializer.DeserializeFromStream <Envelope>(httpReq.InputStream);

                // check existence of the request xml element
                if (soap11Envelope != null && soap11Envelope.Body != null && soap11Envelope.Body.Any.Count > 0)
                {
                    // Get request xml element
                    var requestXmlElement = soap11Envelope.Body.Any[0];
                    if (requestXmlElement != null)
                    {
                        // Get request xml
                        var requestXml = requestXmlElement.OuterXml;
                        if (!string.IsNullOrEmpty(requestXml))
                        {
                            // Deserialize to object of request type
                            return(WrappedXmlSerializer.DeserializeFromString(requestXml, requestType));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                var msg = "Could not deserialize soap11 request into instance of {0}'\nError: {1}"
                          .Fmt(requestType, ex);
                throw new SerializationException(msg);
            }


            throw new ArgumentNullException("Invalid soap11 request message");
        }
Ejemplo n.º 10
0
        public object DeserializeFromString(string contentType, Type type, string request)
        {
            var contentTypeAttr = ContentType.GetEndpointAttributes(contentType);

            switch (contentTypeAttr)
            {
            case EndpointAttributes.Xml:
                return(WrappedXmlSerializer.DeserializeFromString(request, type));

            case EndpointAttributes.Json:
                return(WrappedJsonDeserializer.Instance.DeserializeFromString(request, type));

            case EndpointAttributes.Jsv:
                return(TypeSerializer.DeserializeFromString(request, type));

            default:
                throw new NotSupportedException("ContentType not supported: " + contentType);
            }
        }
Ejemplo n.º 11
0
        protected override string CreateMessage(Type dtoType)
        {
            var requestObj = ReflectionUtils.PopulateObject(dtoType.CreateInstance());

            return(WrappedXmlSerializer.SerializeToString(requestObj, true));
        }