1
\$\begingroup\$

Is my implementation good? The program works as planned, but I wonder if some parts of the code can be made cleaner or more efficient. The file output looks like this:

{
  "TestValue": {
    "Key": "Cat",
    "Description": "dog"
  },
  "testValue2": {
    "Key": "Cat2",
    "Description": "dog2"
  }
}


Here is the class that produces that output:

namespace review
{
 public class FileValue : Dictionary<string, TableValue>
    {

        private string path = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, @"..\..\Data\Values.json"));

        public void Add(string key, string data, string description)
        {
            TableValue value;
            value.data = data;
            value.description = description;
            Add(key, value);
        }

        /// <summary>
        /// This will create the file if it doesn’t exist, then add three values, the key, the data and description from a dictionary. If the file exist the values will be added properly for json formatting.  
        /// </summary>
        public void WriteToFile()
        {
            if (!File.Exists(path))
            {
                string json = JsonConvert.SerializeObject(this, Formatting.Indented);
                File.AppendAllText(path, json);
            }

            else
            {
                string json = JsonConvert.SerializeObject(this, Formatting.Indented);
                json.Remove(json.Length - 1); // Remove the last } to add new data
                File.AppendAllText(path, Environment.NewLine + "," + json);
            }
        }

        /// <summary>
        /// This will read from a file and parse the data into a object
        /// </summary>
        /// <returns></returns>
        public FileValue ReadFromFile()
        {
            using (StreamReader file = File.OpenText(path))
            {
                using (JsonTextReader reader = new JsonTextReader(file))
                {
                    JObject jsonObj = (JObject)JToken.ReadFrom(reader);
                    FileValue dict = JsonConvert.DeserializeObject<FileValue>(jsonObj.ToString());
                    return dict;
                }
            }
        }
    }

    public struct TableValue
    {
        public string data;
        public string description;
    }
}
\$\endgroup\$
  • \$\begingroup\$ This implementation would not work for nested JSON objects, e.g. Is that part of the spec? What's the problem you're trying to solve? \$\endgroup\$ – Nick Udell Dec 18 '17 at 20:53
  • 1
    \$\begingroup\$ There was data that needed to be stored till the end of a test and could not be held in memory. So here I just extended the dictionary class to hold the values and make reading them back in easier. Turned the values into json objects then wrote them to a file. When reading them back in it is serialized back into object automatically. \$\endgroup\$ – KamRon Dec 18 '17 at 21:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.