Esempio n. 1
0
        /// <summary>
        /// Updates the JSON in the file with the new object's information.
        /// </summary>
        /// <param name="settings">Object to save</param>
        /// <param name="section">Section name to update or add</param>
        /// <returns>JSON version of the object to save</returns>
        private JToken UpdateExistingJson(object settings, string section)
        {
            var jsonSection = JToken.FromObject(settings);

            var existingJson = ReadFromFile();

            JToken settingsData;

            try
            {
                settingsData = JToken.Parse(existingJson);
            }
            catch (JsonReaderException ex)
            {
                ErrorLog.Exception("Exception parsing JSON to JToken", ex);

                settingsData = new JObject();
            }

            if (settingsData.SelectToken(section) == null)
            {
                settingsData.AddAfterSelf(settings);
            }
            else
            {
                settingsData[section] = jsonSection;
            }
            return(settingsData);
        }
 public static void SetValue(this JObject obj, string property, object value)
 {
     if (obj.ContainsKey(property))
     {
         obj.Property(property).Remove();
     }
     obj.AddAfterSelf(new JProperty(property, value));
 }