Example #1
0
 /// <summary>
 /// 读取对象。
 /// </summary>
 /// <typeparam name="TData">可序列化对象的类型。</typeparam>
 /// <param name="stream">序列化的流。</param>
 /// <returns>序列化对象。</returns>
 protected override TData Reading <TData>(Stream stream)
 {
     using (var stream2 = new StreamReader(stream, this.Encoding))
     {
         var json = stream2.ReadToEnd();
         return(_serializer.Deserialize <TData>(json));
     }
 }
        protected virtual async Task <T> GetResponseItem <T>(string path, HttpResponseMessage response)
        {
            var json = await GetResponseString(response);

            if (response.StatusCode != HttpStatusCode.OK)
            {
                throw new RestServiceException(GetRestServiceExceptionMessage(path, response.StatusCode));
            }

            return(JSerializer.Deserialize <T>(json));
        }
        protected override async Task <T> GetResponseItem <T>(string path, HttpResponseMessage response)
        {
            var json = await GetResponseString(response);

            if (response.StatusCode != HttpStatusCode.OK)
            {
                var errorResponse = JSerializer.Deserialize <ErrorResponse>(json);
                if (errorResponse == null)
                {
                    throw new RestServiceException(GetRestServiceExceptionMessage(path, response.StatusCode));
                }

                throw new RestServiceException(errorResponse.Payload.Message, errorResponse);
            }

            return(JSerializer.Deserialize <T>(json));
        }
        protected override async Task <T> GetResponseItem <T>(HttpResponseMessage response)
        {
            var content = await response.Content.ReadAsStringAsync();

            if (response.StatusCode != HttpStatusCode.OK)
            {
                var errorResponse = JSerializer.Deserialize <ErrorResponse>(content);
                if (errorResponse == null)
                {
                    throw new RestServiceException(
                              GetRestServiceExceptionMessage(response.RequestMessage.RequestUri.ToString(),
                                                             response.StatusCode, content));
                }

                throw new RestServiceException(errorResponse.Payload.Message, errorResponse);
            }

            return(JSerializer.Deserialize <T>(content));
        }
        static Dictionary <string, string> LoadCookie()
        {
            string content = string.Empty;

            if (File.Exists(CookieName))
            {
                content = File.ReadAllText(CookieName, Encoding.UTF8);
            }

            if (!string.IsNullOrEmpty(content))
            {
                Dictionary <string, string> temp = JSerializer.Deserialize <Dictionary <string, string> >(content);

                if (null != temp)
                {
                    return(temp);
                }
            }

            return(new Dictionary <string, string>());
        }
        private IWsMessage DeserializeMessage(string message)
        {
            var eventType = JSerializer.Deserialize <WsMessage>(message).Event;

            switch (eventType)
            {
            case EventType.OrderBook:
                return(JSerializer.Deserialize <OrderBookMessage>(message));

            case EventType.Candle:
                return(JSerializer.Deserialize <CandleMessage>(message));

            case EventType.InstrumentInfo:
                return(JSerializer.Deserialize <InstrumentInfoMessage>(message));

            case EventType.Error:
                return(JSerializer.Deserialize <ErrorMessage>(message));

            default:
                throw new ArgumentException(nameof(eventType));
            }
        }
Example #7
0
        public static T FromJson <T>(this string json)
        {
            Check.Argument.IsNotEmpty(json, "json");

            return(JSerializer.Deserialize <T>(json));
        }