/// <summary>
        /// Retrieve a value from the session which has been saved in JSON format.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="session"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        /// <remarks>Save the value using SetJson.</remarks>
        public static T GetJSon <T>(this ISession session, string key)
        {
            string sessionData = null;
            T      result      = default(T);

            using (JSONSerialiser js = new JSONSerialiser())
            {
                sessionData = session.GetString(key).FromBase64();
                result      = sessionData == null ? default(T) : js.Deserialize <T>(sessionData);
            }

            return(result);
        }
示例#2
0
        public void JSONSerialiser_Deserialisation_DoesntThrowOnValidData()
        {
            IBaseMessage message        = ISerializerTestsData.GenerateMessage();
            ISerializer  jsonSerialiser = new JSONSerialiser();

            byte[] messageBytes = jsonSerialiser.Serialize(message);

            IBaseMessage messageDeserialized = jsonSerialiser.Deserialize(messageBytes);

            Assert.AreEqual(message.To, messageDeserialized.To);
            Assert.AreEqual(message.From, messageDeserialized.From);
            Assert.AreEqual(message.Type, messageDeserialized.Type);
            Assert.AreEqual(message.Version, messageDeserialized.Version);
        }
        public override void LoadJsonConfig()
        {
            base.LoadJsonConfig();
            string configPath = base.ContentRootPath + base.ConfigFileName;

            // In dev mode, load a different file.
            if (base.IsDevelopment == true)
            {
                configPath = base.ContentRootPath + "appsettings.Development.json";
            }

            using (JSONSerialiser js = new JSONSerialiser())
            {
                this.AppConfiguration = js.Deserialize <AppConfigurationModel>(new Uri(configPath));
            }
        }
        public ActionResult <CategoryRowApiO> Patch([FromBody] CategoryRowApiO category)
        {
            try
            {
                if (category == default(CategoryRowApiO))
                {
                    // See if a partial has been sent.
                    using (StreamReader sr = new StreamReader(Request.Body))
                    {
                        string requestBody = sr.ReadToEnd();

                        if (requestBody != null)
                        {
                            if (requestBody.Length > 0)
                            {
                                using (JSONSerialiser serialiser = new JSONSerialiser())
                                {
                                    // The request may contain a partial so work around this.
                                    category = serialiser.Deserialize <CategoryRowApiO>(requestBody);
                                }
                            }
                        }
                    }
                }

                if (category != default(CategoryRowApiO))
                {
                    if (ModelState.IsValid)
                    {
                        CategoryRowApiO result = this.CategoryService.Update(category);
                        this.CategoryService.Commit();

                        // Updated
                        Response.StatusCode = 200; // OK
                        return(result);
                    }
                    else
                    {
                        throw new ModelStateException(string.Format("Validation Failed, the {0} contains invalid data.", category.GetType().ToString()), ModelState);
                    }
                }
                else
                {
                    throw new ArgumentException("No json body could be found.");
                }
            }
            catch (ArgumentNullException e)
            {
                // expectation failed - field is missing
                Response.AddBody(e.Message);
                return(new StatusCodeResult(417));
            }
            catch (ModelStateException e)
            {
                // not acceptable
                Response.AddBody(e.Message);
                return(new StatusCodeResult(406));
            }
            catch (RecordNotFoundException e)
            {
                Response.AddBody(e.Message);
                return(new StatusCodeResult(204)); // no content
            }
            catch (Exception e)
            {
                // application error internal server error
                Response.AddBody(e.Message);
                return(new StatusCodeResult(500));
            }
        }
示例#5
0
        public void JSONSerialiser_Deserialisation_ThrowsOnNull()
        {
            ISerializer jsonSerialiser = new JSONSerialiser();

            Assert.ThrowsException <ArgumentNullException>(() => jsonSerialiser.Deserialize(null));
        }