0

I'm currently setting-up my application, and i need to manage a json file (that contain some settings) in my windows form. Once you open it, you can easy choose different settings, and once you've done, you can save it (which mean i need to overwrite the existing one json file settings, and replace it with new one!


I tried to follow this guide for correctly create my json file! But i met 2 problems:

  1. This solution mentioned up, create square brackets (which i don't need!)
  2. Seems to create all settings on one line. Is that correct or it could make some problem in the future?

Some materials....

My application:

Original json file:

Json file generated with my code:

My class:

    public class data
    {
        public bool avx { get; set; }
        public bool memory_pool { get; set; }
        public bool smt { get; set; }
        public bool spectre { get; set; }
        public bool unlock_menu { get; set; }
        public bool vinput { get; set; }
        public double cpu_memory_pool_fraction { get; set; }
        public double gpu_memory_pool_fraction { get; set; }
    }

My code:

private void btn_save_Click(object sender, EventArgs e)
{
    string settings_path = general_path + "\\plugins\\cyber_engine_tweaks\\" + "config.json"; //path

    bool avx_set = cb_avx.Checked;
    bool smt_set = cb_smt.Checked;
    bool memory_set = cb_memory.Checked;
    bool spectre_set = cb_spectre.Checked;
    bool debug_set = cb_debug.Checked;
    bool vinput_set = cb_vinput.Checked;

    List<data> _data = new List<data>();
    _data.Add(new data()
    {
        avx = avx_set,
        cpu_memory_pool_fraction = 0.5,
        gpu_memory_pool_fraction = 1.0,
        memory_pool = memory_set,
        smt = smt_set,
        spectre = spectre_set,
        unlock_menu = debug_set,
        vinput = vinput_set
    });

    using (StreamWriter file = File.CreateText(settings_path))
    {
        JsonSerializer serializer = new JsonSerializer();
        //serialize object directly into file stream
        serializer.Serialize(file, _data);
    }
}
Zenek
  • 110
  • 1
  • 4
  • 12

3 Answers3

1
  1. Square brackets are because you send an array:
    Instead of List<data> _data = new List<data>(); _data.Add(new data()...
    try var data = new data()... serializer.Serialize(file, data)

  2. All settings on one line is normal.

1

Very nice detailed answer here: Can JSON start with "["?

TLDR:

  1. It's not a Json without either {} indicating an object or [] indicating an array. So no, you can't have a json with multiple keys without one.
  2. The newlines are optional in Json. Since most json objects are used for transfering over the wire, there is no need for newlines (which take up unessecary bytes).
Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
1

Issues: 1 . This solution mentioned up, create square brackets (which i don't need!)

Solution:

Create your data object like:

        //List<data> _data = new List<data>();
        data _data = new data 
        { 
            avx = avx_set,
            cpu_memory_pool_fraction = 0.5,
            gpu_memory_pool_fraction = 1.0,
            memory_pool = memory_set,
            smt = smt_set,
            spectre = spectre_set,
            unlock_menu = debug_set,
            vinput = vinput_set
        };

2 . Seems to create all settings on one line. Is that correct or it could make some problem in the future?

Solution: I think it's a format issue. Can igonore

  • Thank you for the reply @Presad Ramireddy! Unfortunately this solution doesn't work for me (or probably i miss something?) the compiler show me this error: `The type or namespace name 'data' could not be found (are you missing a using directive or an assembly reference?)` – Zenek Dec 16 '20 at 11:35
  • Here 'data' is a reference tobject that is your "data" class that is : public class data { public bool avx { get; set; } public bool memory_pool { get; set; } public bool smt { get; set; } public bool spectre { get; set; } public bool unlock_menu { get; set; } public bool vinput { get; set; } public double cpu_memory_pool_fraction { get; set; } public double gpu_memory_pool_fraction { get; set; } } – Prasad Ramireddy Dec 16 '20 at 12:03
  • yes, i know, but i don't understand why i still get this error while the class is correctly set-up and working ... do you have any idea about it? – Zenek Dec 16 '20 at 12:11