private static async Task HandleErrorResponse(HttpResponseMessage response)
        {
            var content = await response.Content.ReadAsStringAsync();

            if (!string.IsNullOrWhiteSpace(content))
            {
                ErrorResponse errorResponse = null;
                try
                {
                    errorResponse = JsonSerializationHelper.Deserialize <ErrorResponse>(content);
                }
                catch (Exception)
                {
                    throw new ResponseParseException("An error occurred while deserializing the response", content);
                }
                if (errorResponse.error == null)
                {
                    throw new ResponseParseException("An error occurred while deserializing the response", content);
                }

                switch (errorResponse.error.code)
                {
                case ReadableErrorTopCode.Conflict: throw new MorphApiConflictException(errorResponse.error.message);

                case ReadableErrorTopCode.NotFound: throw new MorphApiNotFoundException(errorResponse.error.message);

                case ReadableErrorTopCode.Forbidden: throw new MorphApiForbiddenException(errorResponse.error.message);

                case ReadableErrorTopCode.Unauthorized: throw new MorphApiUnauthorizedException(errorResponse.error.message);

                case ReadableErrorTopCode.BadArgument: throw new MorphApiBadArgumentException(FieldErrorsMapper.MapFromDto(errorResponse.error), errorResponse.error.message);

                default: throw new MorphClientGeneralException(errorResponse.error.code, errorResponse.error.message);
                }
            }

            else
            {
                switch (response.StatusCode)
                {
                case HttpStatusCode.Conflict: throw new MorphApiConflictException(response.ReasonPhrase ?? "Conflict");

                case HttpStatusCode.NotFound: throw new MorphApiNotFoundException(response.ReasonPhrase ?? "Not found");

                case HttpStatusCode.Forbidden: throw new MorphApiForbiddenException(response.ReasonPhrase ?? "Forbidden");

                case HttpStatusCode.Unauthorized: throw new MorphApiUnauthorizedException(response.ReasonPhrase ?? "Unauthorized");

                case HttpStatusCode.BadRequest: throw new MorphClientGeneralException("Unknown", response.ReasonPhrase ?? "Unknown error");

                default: throw new ResponseParseException(response.ReasonPhrase, null);
                }
            }
        }
        private T ReadResponse <T>(WebRequest client) where T : class
        {
            var webResponse  = (HttpWebResponse)GetResponse(client);
            var resultStream = new MemoryStream();

            var responseStream = webResponse.GetResponseStream();

            if (responseStream != null)
            {
                StreamHelper.CopyTo(responseStream, resultStream);
            }

            try
            {
                requestHandlers.ForEach(p => p.ProcessResponse(webResponse, resultStream));

                resultStream.Position = 0;

                if (typeof(T) == typeof(HttpWebResponse))
                {
                    return(webResponse as T);
                }

                if (typeof(T) == typeof(Stream))
                {
                    return(resultStream as T);
                }

                var str = Encoding.UTF8.GetString(resultStream.ToArray());

                if (typeof(T) == typeof(string))
                {
                    return(str as T);
                }

                if (typeof(T) == typeof(JObject))
                {
                    return(JObject.Parse(str) as T);
                }

                return(JsonSerializationHelper.Deserialize(str, typeof(T)) as T);
            }
            catch (Exception)
            {
                resultStream.Dispose();
                throw;
            }
        }
        public Config GetConfig()
        {
            WritableSettingsStore userSettingsStore = GetSettingsStore();
            string configJson;

            if (userSettingsStore.PropertyExists(SettingsStoreCollectionPath, SettingsStorePropertyName))
            {
                configJson = userSettingsStore.GetString(SettingsStoreCollectionPath, SettingsStorePropertyName);
                var config = JsonSerializationHelper.Deserialize <Config>(configJson);
                return(config);
            }

            return(new Config {
                ConfigItems = new ConfigItem[0]
            });
        }
Exemple #4
0
        public void TestObjectDeserialization()
        {
            var guid  = Guid.NewGuid();
            var group = JsonSerializationHelper.Deserialize <Group>("{Persons:[{\"Id\":\"" + guid + "\",Name:\"Jens\",\"Age\":1337,\"IsAlive\":true},{\"Name\":\"Ole\",Age:-10,IsAlive:false}],IsActive:true,Title:\"My group\"}");

            Assert.IsNotNull(group);

            Assert.AreEqual("My group", group.Title);
            Assert.IsTrue(group.IsActive);

            Assert.IsNotNull(group.Persons);
            Assert.AreEqual(2, group.Persons.Count);

            Assert.IsTrue(group.Persons.Any(p => p.Age == 1337 && p.Name == "Jens" && p.IsAlive && p.Id == guid));
            Assert.IsTrue(group.Persons.Any(p => p.Age == -10 && p.Name == "Ole" && !p.IsAlive));
        }
        protected static async Task <T> HandleResponse <T>(HttpResponseMessage response)
        {
            if (response.IsSuccessStatusCode)
            {
                var content = await response.Content.ReadAsStringAsync();

                var result = JsonSerializationHelper.Deserialize <T>(content);
                return(result);
            }
            else
            {
                await HandleErrorResponse(response);

                return(default(T));
            }
        }
Exemple #6
0
        public void TestNumericDeserialization()
        {
            Assert.AreEqual((double)1.337, JsonSerializationHelper.Deserialize <double>("1.337"));
            Assert.AreEqual((decimal)1.337, JsonSerializationHelper.Deserialize <decimal>("1.337"));
            Assert.AreEqual((float)1.337, JsonSerializationHelper.Deserialize <float>("1.337"));

            Assert.AreEqual((double)-1.337, JsonSerializationHelper.Deserialize <double>("-1.337"));
            Assert.AreEqual((decimal) - 1.337, JsonSerializationHelper.Deserialize <decimal>("-1.337"));
            Assert.AreEqual((float)-1.337, JsonSerializationHelper.Deserialize <float>("-1.337"));

            Assert.AreEqual((double)0, JsonSerializationHelper.Deserialize <double>("0"));
            Assert.AreEqual((int)0, JsonSerializationHelper.Deserialize <int>("0"));

            Assert.AreEqual((int)1337, JsonSerializationHelper.Deserialize <int>("1337"));
            Assert.AreEqual((short)1337, JsonSerializationHelper.Deserialize <short>("1337"));
            Assert.AreEqual((long)1337, JsonSerializationHelper.Deserialize <long>("1337"));

            Assert.AreEqual((int)-1337, JsonSerializationHelper.Deserialize <int>("-1337"));
            Assert.AreEqual((short)-1337, JsonSerializationHelper.Deserialize <short>("-1337"));
            Assert.AreEqual((long)-1337, JsonSerializationHelper.Deserialize <long>("-1337"));
        }
Exemple #7
0
 /// <summary>
 ///     Creates an instance of the object from its serialized string representation.
 /// </summary>
 /// <param name="objbyte">String representation of the object from the Redis server.</param>
 /// <returns>
 ///     Returns a newly constructed object.
 /// </returns>
 public object Deserialize(RedisValue objbyte)
 {
     return(JsonSerializationHelper.Deserialize(objbyte));
 }