예제 #1
0
        private static bool SetSpecialTypes <TResponse>(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))
            {
                using (var ms = memoryStreamFactory.Create(bytes))
                {
                    var body = LowLevelRequestResponseSerializer.Instance.Deserialize <DynamicDictionary>(ms);
                    cs = new DynamicResponse(body) as TResponse;
                }
            }
            return(cs != null);
        }
예제 #2
0
 /// <summary>
 /// Extension method that serializes an instance of <typeparamref name="T"/> to a string.
 /// </summary>
 /// <param name="memoryStreamFactory">
 /// A factory yielding MemoryStream instances, defaults to <see cref="RecyclableMemoryStreamFactory"/>
 /// that yields memory streams backed by pooled byte arrays.
 /// </param>
 public static string SerializeToString <T>(
     this IElasticsearchSerializer serializer,
     T data,
     IMemoryStreamFactory memoryStreamFactory,
     SerializationFormatting formatting = SerializationFormatting.None
     )
 {
     memoryStreamFactory ??= RecyclableMemoryStreamFactory.Default;
     using (var ms = memoryStreamFactory.Create())
     {
         serializer.Serialize(data, ms, formatting);
         return(ms.Utf8String());
     }
 }
예제 #3
0
 /// <summary>
 /// Extension method that serializes an instance of <typeparamref name="T"/> to a byte array.
 /// </summary>
 /// <param name="memoryStreamFactory">
 /// A factory yielding MemoryStream instances, defaults to <see cref="RecyclableMemoryStreamFactory"/>
 /// that yields memory streams backed by pooled byte arrays.
 /// </param>
 public static byte[] SerializeToBytes <T>(
     this IOpenSearchSerializer serializer,
     T data,
     IMemoryStreamFactory memoryStreamFactory,
     SerializationFormatting formatting = SerializationFormatting.None
     )
 {
     memoryStreamFactory ??= RecyclableMemoryStreamFactory.Default;
     using (var ms = memoryStreamFactory.Create())
     {
         serializer.Serialize(data, ms, formatting);
         return(ms.ToArray());
     }
 }
 /// <summary>
 /// Extension method that serializes an instance of <typeparamref name="T"/> to a string.
 /// </summary>
 /// <param name="memoryStreamFactory">
 /// A factory yielding MemoryStream instances, defaults to <see cref="RecyclableMemoryStreamFactory"/>
 /// that yields memory streams backed by pooled byte arrays.
 /// </param>
 public static string SerializeToString <T>(
     this ITransportSerializer serializer,
     T data,
     IMemoryStreamFactory memoryStreamFactory,
     SerializationFormatting formatting = SerializationFormatting.None
     )
 {
     memoryStreamFactory ??= TransportConfiguration.DefaultMemoryStreamFactory;
     using (var ms = memoryStreamFactory.Create())
     {
         serializer.Serialize(data, ms, formatting);
         return(ms.Utf8String());
     }
 }
        /// <summary>
        /// Writes a <see cref="JToken" /> to a <see cref="MemoryStream" /> using <see cref="ConnectionSettingsAwareSerializer.ExpectedEncoding" />
        /// </summary>
        public static MemoryStream ToStream(this JToken token, IMemoryStreamFactory memoryStreamFactory)
        {
            var ms = memoryStreamFactory.Create();

            using (var streamWriter = new StreamWriter(ms, ConnectionSettingsAwareSerializer.ExpectedEncoding,
                                                       ConnectionSettingsAwareSerializer.DefaultBufferSize, true))
                using (var writer = new JsonTextWriter(streamWriter))
                {
                    token.WriteTo(writer);
                    writer.Flush();
                    ms.Position = 0;
                    return(ms);
                }
        }
예제 #6
0
 /// <summary>
 /// Extension method that serializes an instance of <typeparamref name="T"/> to a byte array.
 /// </summary>
 /// <param name="memoryStreamFactory">
 /// A factory yielding MemoryStream instances, defaults to <see cref="RecyclableMemoryStreamFactory"/>
 /// that yields memory streams backed by pooled byte arrays.
 /// </param>
 public static byte[] SerializeToBytes <T>(
     this IElasticsearchSerializer serializer,
     T data,
     IMemoryStreamFactory memoryStreamFactory,
     SerializationFormatting formatting = SerializationFormatting.None
     )
 {
     memoryStreamFactory ??= ConnectionConfiguration.DefaultMemoryStreamFactory;
     using (var ms = memoryStreamFactory.Create())
     {
         serializer.Serialize(data, ms, formatting);
         return(ms.ToArray());
     }
 }
 public static byte[] SerializeToBytes <T>(
     this IElasticsearchSerializer serializer,
     T data,
     IMemoryStreamFactory memoryStreamFactory = null,
     SerializationFormatting formatting       = SerializationFormatting.Indented
     )
 {
     memoryStreamFactory = memoryStreamFactory ?? RecyclableMemoryStreamFactory.Default;
     using (var ms = memoryStreamFactory.Create())
     {
         serializer.Serialize(data, ms, formatting);
         return(ms.ToArray());
     }
 }
        public static MemoryStream ToStream(
            this JToken token,
            IMemoryStreamFactory memoryStreamFactory = null)
        {
            var ms = memoryStreamFactory?.Create() ?? new MemoryStream();

            using (var streamWriter = new StreamWriter(ms, InternalSerializer.ExpectedEncoding, InternalSerializer.DefaultBufferSize, leaveOpen: true))
                using (var writer = new JsonTextWriter(streamWriter))
                {
                    token.WriteTo(writer);
                    writer.Flush();
                    ms.Position = 0;
                    return(ms);
                }
        }
예제 #9
0
        public void BadResponse <TResponse>(ref TResponse response, IApiCallDetails callDetails, RequestData data,
                                            ElasticsearchClientException exception
                                            )
            where TResponse : class, IElasticsearchResponse, new()
        {
            if (response == null)
            {
                //make sure we copy over the error body in case we disabled direct streaming.
                var s = callDetails?.ResponseBodyInBytes == null ? Stream.Null : _memoryStreamFactory.Create(callDetails.ResponseBodyInBytes);
                var m = callDetails?.ResponseMimeType ?? RequestData.MimeType;
                response = ResponseBuilder.ToResponse <TResponse>(data, exception, callDetails?.HttpStatusCode, null, s, m);
            }

            response.ApiCall.AuditTrail = AuditTrail;
        }
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            var formatting = serializer.Formatting == Formatting.Indented
                                ? SerializationFormatting.Indented
                                : SerializationFormatting.None;

            using (var ms = _memoryStreamFactory.Create())
                using (var streamReader = new StreamReader(ms, ConnectionSettingsAwareSerializerBase.ExpectedEncoding))
                    using (var reader = new JsonTextReader(streamReader))
                    {
                        _builtInSerializer.Serialize(value, ms, formatting);
                        ms.Position = 0;
                        var token = reader.ReadTokenWithDateParseHandlingNone();
                        writer.WriteToken(token.CreateReader(), true);
                    }
        }
        public static async Task <MemoryStream> ToStreamAsync(
            this JToken token,
            IMemoryStreamFactory memoryStreamFactory = null,
            CancellationToken cancellationToken      = default(CancellationToken))
        {
            var ms = memoryStreamFactory?.Create() ?? new MemoryStream();

            using (var streamWriter = new StreamWriter(ms, InternalSerializer.ExpectedEncoding, InternalSerializer.DefaultBufferSize, leaveOpen: true))
                using (var writer = new JsonTextWriter(streamWriter))
                {
                    await token.WriteToAsync(writer, cancellationToken).ConfigureAwait(false);

                    await writer.FlushAsync(cancellationToken).ConfigureAwait(false);

                    ms.Position = 0;
                    return(ms);
                }
        }
예제 #12
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);
        }
예제 #13
0
        public async Task <InlineResponse200> DetectPlateAsync(string pathToPlateImage)
        {
            if (string.IsNullOrEmpty(pathToPlateImage))
            {
                throw new ArgumentException("Argument should not be null or empty");
            }

            using (Image image = _imageWrapper.GetImageFromFile(pathToPlateImage))
            {
                using (MemoryStream m = _streamFactory.Create())
                {
                    image.Save(m, image.RawFormat);
                    var    imageBytes   = m.ToArray();
                    string base64String = Convert.ToBase64String(imageBytes);

                    return(await _defaultApi.RecognizeBytesAsync(base64String, secretKey, country, recognizeVehicle, state, returnImage, topn, prewarp));
                }
            }
        }
예제 #14
0
        /// <inheritdoc />
        public XmlSchema ReadFromByteArray(byte[] array)
        {
            if (array == null)
            {
                throw new ArgumentNullException(nameof(array));
            }

            if (array.LongLength == 0)
            {
                throw new ArgumentException("Byte array must not be empty.", nameof(array));
            }

            XmlSchema result;

            try
            {
                using (var stream = memoryStreamFactory.Create(array, false))
                {
                    result = ReadFromStream(stream);
                }
            }
            catch (Exception e)
            {
                var sb = new StringBuilder();
                sb.AppendLine("Could not create a memory stream from the specified byte array.");
                sb.AppendLine();
                sb.AppendLine("Exception: ");
                sb.AppendLine(e.Message);
                sb.AppendLine();
                sb.AppendLine("Stacktrace: ");
                sb.AppendLine(e.StackTrace);
                throw new InvalidOperationException(sb.ToString(), e);
            }

            return(result);
        }
 internal T AsUsingRequestResponseSerializer <T>()
 {
     using (var ms = _memoryStreamFactory.Create(Bytes))
         return(_requestResponseSerializer.Deserialize <T>(ms));
 }
예제 #16
0
 /// <inheritdoc />
 public T As <T>()
 {
     using (var ms = _memoryStreamFactory.Create(Bytes))
         return(_serializer.Deserialize <T>(ms));
 }