Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hello, I am (de)serializing the json strings to txt file. In my code object "c2" successfully does that where object "c1" fails to serialize and deserialize. Since object c1 fails, its boolean flags do not change.

I searched for solutions, I saw some people merge the strings, and some use streamwrite which I don't use it in my code.

What I have tried:

this is my constructor where I perform deserialization

public Form1()
        {
            InitializeComponent();
            input += ".txt";

            //default path + new filename
            path_combined = Path.Combine(root, input);


            if (!File.Exists(path_combined))
            {

                using (var stream = File.Create(path_combined))
                {
                    if (new FileInfo(path_combined).Length > 0)
                    {
                        //flag situation
                        string json2 = File.ReadAllText(path_combined);
                        FormatJson(json2);
                        c1 = JsonConvert.DeserializeObject<Class1>(json2);


                        //Method used or not
                        string json = File.ReadAllText(path_combined);
                        FormatJson(json);
                        c2 = JsonConvert.DeserializeObject<Class1>(json);
                    }
                }

            }
            else
            {
                //flag situation
                string json2 = File.ReadAllText(path_combined);
                FormatJson(json2);
                c1 = JsonConvert.DeserializeObject<Class1>(json2);


                //Method used or not
                string json = File.ReadAllText(path_combined);
                FormatJson(json);
                c2 = JsonConvert.DeserializeObject<Class1>(json);

            }

          

        }


This is the serialization part when I click a button, it updates flags.
private void Button1_Click(object sender, EventArgs e)
        {

            c1.flag = true;
            c1.flag2 = true;

            c2.M1 = true;

            string json = JsonConvert.SerializeObject(c1, Formatting.Indented);
            File.WriteAllText(path_combined, json);

            string json2 = JsonConvert.SerializeObject(c2, Formatting.Indented);
            File.WriteAllText(path_combined, json2);

        }
Posted
Updated 23-Jul-19 20:06pm
v2

1 solution

Look at what you are doing:
C#
string json = JsonConvert.SerializeObject(c1, Formatting.Indented);
File.WriteAllText(path_combined, json);

string json2 = JsonConvert.SerializeObject(c2, Formatting.Indented);
File.WriteAllText(path_combined, json2);
WriteAllText does what it says: creates a new file containing exactly the string it is passed. If a file of the same name exists, it is deleted first. So the file will only ever contain the one set of JSON values for class2, the class1 data will be discarded.
 
Share this answer
 
Comments
Danny96 24-Jul-19 2:35am    
thank you
OriginalGriff 24-Jul-19 3:13am    
You're welcome!

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900