Example #1
0
        private static bool SetSpecialTypes <TResponse>(byte[] bytes, out TResponse cs)
            where TResponse : class, IElasticsearchResponse, new()
        {
            cs = null;
            var responseType = typeof(TResponse);

            if (!SpecialTypes.Contains(responseType))
            {
                return(false);
            }

            if (responseType == typeof(StringResponse))
            {
                cs = new StringResponse(bytes.Utf8String()) as TResponse;
            }
            else if (responseType == typeof(byte[]))
            {
                cs = new BytesResponse(bytes) as TResponse;
            }
            else if (responseType == typeof(VoidResponse))
            {
                cs = StaticVoid as TResponse;
            }
            else if (responseType == typeof(DynamicResponse))
            {
                using (var ms = new MemoryStream(bytes))
                {
                    var body = LowLevelRequestResponseSerializer.Instance.Deserialize <DynamicBody>(ms);
                    cs = new DynamicResponse(body) as TResponse;
                }
            }
            return(cs != null);
        }
 public override object DeserializeObject(object value, Type type)
 {
     if (type == typeof(DynamicResponse))
     {
         var dict = base.DeserializeObject(value, typeof(IDictionary <string, object>)) as IDictionary <string, object>;
         return(dict == null ? null : DynamicResponse.Create(dict));
     }
     if (type == typeof(ServerError))
     {
         var dict = base.DeserializeObject(value, typeof(IDictionary <string, object>)) as IDictionary <string, object>;
         return(ServerError.Create(dict, this));
     }
     if (type == typeof(Error))
     {
         var dict = base.DeserializeObject(value, typeof(IDictionary <string, object>)) as IDictionary <string, object>;
         return(Error.Create(dict, this));
     }
     if (type == typeof(RootCause))
     {
         var dict = base.DeserializeObject(value, typeof(IDictionary <string, object>)) as IDictionary <string, object>;
         return(RootCause.Create(dict, this));
     }
     if (type == typeof(CausedBy))
     {
         var dict = base.DeserializeObject(value, typeof(IDictionary <string, object>)) as IDictionary <string, object>;
         return(CausedBy.Create(dict, this));
     }
     return(base.DeserializeObject(value, type));
 }
Example #3
0
        private static bool SetSpecialTypes <TResponse>(string mimeType, byte[] bytes, IMemoryStreamFactory memoryStreamFactory, out TResponse cs)
            where TResponse : class, IElasticsearchResponse, new()
        {
            cs = null;
            var responseType = typeof(TResponse);

            if (!SpecialTypes.Contains(responseType))
            {
                return(false);
            }

            if (responseType == typeof(StringResponse))
            {
                cs = new StringResponse(bytes.Utf8String()) as TResponse;
            }
            else if (responseType == typeof(BytesResponse))
            {
                cs = new BytesResponse(bytes) as TResponse;
            }
            else if (responseType == typeof(VoidResponse))
            {
                cs = new VoidResponse() as TResponse;
            }
            else if (responseType == typeof(DynamicResponse))
            {
                //if not json store the result under "body"
                if (mimeType != RequestData.MimeType)
                {
                    var dictionary = new DynamicDictionary();
                    dictionary["body"] = new DynamicValue(bytes.Utf8String());
                    cs = new DynamicResponse(dictionary) as TResponse;
                }
                else
                {
                    using (var ms = memoryStreamFactory.Create(bytes))
                    {
                        var body = LowLevelRequestResponseSerializer.Instance.Deserialize <DynamicDictionary>(ms);
                        cs = new DynamicResponse(body) as TResponse;
                    }
                }
            }
            return(cs != null);
        }