Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Save Your Settings in a File in JSON and using LINQ for Read and Save

4.83/5 (15 votes)
25 Sep 2023CPOL1 min read 27.9K  
Save Key and Value in JSONformat using C# and LINQ
If you often find yourself creating standalone projects that need to save Global app information for example form settings, maybe this article will be helpful. I often find myself reinventing the wheel, instead of using this class for reading writing and updating a key - value.

Introduction

Saving to a Key-Value file is a simple and useful operation for any type of software, although, from my experience, you can easily run into agility problems when retrieving this information.

Using this class and with little effort, you can view, create, modify a key. Using LINQ (one of the wonders of the world), you can search for a specific key or display all of them.

A boon for anyone beginning to write code.

Background

You can add this simple class into your project and immediately start saving the information to a Json format file disguising it with any extension.

Once you've inserted this class, you'll need to include the Newtonsoft.Json nuget library with a simple CTRL+.
Then, using Linq and with very little effort, you can read all the information one by one or all at once.

Using the Code

So let's start: in our project, we insert a new class:

C#
using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO;
using System.Linq;
 
namespace SettingsFileClass
{
    public class SettingsInJsonFile
      {
          // Here, you have to set the network path where you want to insert the file
          // For example, you can use c:\MyTestApp\Settings.ini
          //                         c:\program files\MyTestApplication\MainConfig.config
          // Or you can use Isolated storage for location your file
          public const string FilePath = @"D:\temp\settings.json";
          // You can choose if you prefer indented or none (more space saving)
          public const Formatting MyFormatting = Formatting.Indented;
 
          /// <summary>
          /// Class Settings
          /// </summary>
          internal string _key;
          public string Key { get { return _key; } set { _key = value.ToUpper(); } }
          public string? Value { get; set; }
 
          /// <summary>
          /// Set a new value in a Key if exists. You can use for create a new key using
          /// Optional value (by default, return false if Key not exist
          /// </summary>
          /// <param name="Key">This string will be saved only uppercase</param>
          /// <param name="NewValue">New value in string type</param>
          /// <param name="CreateIfNew">bool true = create, false = not create</param>
          /// <returns></returns>
          internal static bool Set(string Key, string NewValue, bool CreateIfNew = false)
          {
              Key = Key.ToUpper();
              var Item = GetObject(Key);
              var _data = GetAll().ToList();
 
              if (Item == null)
              {
                  //Not found -> user choice if create
                  if (CreateIfNew)
                  {
                      //Add new key in Data Items
                      _data.Add(new SettingsInJsonFile()
                      {
                          Key = Key,
                          Value = NewValue
                      });
                  }
                  else
                  {
                      //Not found, not allow to create
                      return false;
                  }
              }
              else
                  _data.First(x => x.Key == Key).Value = NewValue; //Update value
 
              string json = JsonConvert.SerializeObject(_data.ToArray(), MyFormatting);
              //write string to destination
              File.WriteAllText(FilePath, json);
 
              return true;
          }
          internal static bool Delete(string KeyToDelete)
          {
              if (Exist(KeyToDelete))
              {
                  var _data = GetAll().Where
                      (x => x.Key != KeyToDelete.ToUpper()).ToArray();
                  string json = JsonConvert.SerializeObject(_data, MyFormatting);
 
                  //write string to destination
                  File.WriteAllText(FilePath, json);
 
                  return true;
              }
              return false;
          }
          internal static List<SettingsInJsonFile> GetAll()
          {
              //Create file Settings if not exists
              if (!File.Exists(FilePath))
              {
                  //Creation of new file with Initialization ->"[]"
                  using StreamWriter wr = File.CreateText(FilePath);
                  wr.WriteLine("[]");
              }
 
              using StreamReader r = new(FilePath);
              string json = r.ReadToEnd();
              if (string.IsNullOrEmpty(json)) json = "[]";
              var Response = JsonConvert.DeserializeObject
                             <List<SettingsInJsonFile>>(json);
              return Response;
          }
 
          internal static string GetValue(string Key) => 
                   GetAll().FirstOrDefault(x => x.Key == Key.ToUpper()).Value;
          internal static SettingsInJsonFile? GetObject(string Key) => 
                   GetAll().FirstOrDefault(x => x.Key == Key.ToUpper());
          internal static bool Exist(string Key) => GetAll().FirstOrDefault
                   (x => x.Key == Key.ToUpper()) != null;
 
      }
}

The file result in the file is, for example:

JavaScript
[
    {"Key":"key","Value":"07/10/2022 13:04:21"},
    {"Key":"backcolor","Value":"#FFFFFF"}
    {"Key":"forecolor","Value":"#000000"}
]

Points of Interest

This simple class is powerful enough for small text files used for local storage. I would not recommend it for more complex scenarios or where the number of variables begin to grow exponentially.

The function "CREATE" create Keys, but these keys are Keysensitive, so a good check to add would be to create all keys as uppercase only.

History

  • 7th October, 2022: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)