question

MIndraRuslan-8520 avatar image
0 Votes"
MIndraRuslan-8520 asked karenpayneoregon edited

SerializeObject json file

Hi All,

I have the following JSON file. My question is, how to SerializeObject If I uncheck the checkbox on the app, it will update the 'checkboxvalue' to 'false' and updated the json file as well. thanks.
{
"ListItem": [
{
"Id": 1,
"Item": "Item1",
"CheckboxValue": "true"
},
{
"Id": 2,
"Item": "Item2",
"CheckboxValue": "True"
}
]
}

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MIndraRuslan-8520 avatar image
0 Votes"
MIndraRuslan-8520 answered karenpayneoregon edited

Hi Karen
Thanks for the share. I'm new to this kind of stuff.
I have the following code but always get stuck. So basically, I try to update the value of boolean if the checkbox state change.
90229-image.png



image.png (116.3 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You need to work with my code sample which works which uses data binding to controls and even though that is best you can do this in code if you take time to learn what I have given you so it will not be new to you any more.

The entire project is in the repository below

https://github.com/karenpayneoregon/forum-questions/tree/april2021/SimpleJsonExample


91023-kpmvp.png


0 Votes 0 ·
kpmvp.png (2.0 KiB)
karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered karenpayneoregon edited

Hello,

Here I'm using Newtonsoft Json.net NuGet package.

90007-figure1.png

 [
   {
     "id": 1,
     "item": "value 1",
     "CheckboxValue": true
   },
   {
     "id": 2,
     "item": "value 2",
     "CheckboxValue": false
   }
 ]

Container for json

 using Newtonsoft.Json;
    
 namespace WindowsFormsApp1
 {
     public class ListItem
     {
         [JsonProperty("id")]
         public int Id { get; set; }
         [JsonProperty("item")]
         public string Item { get; set; }
         public bool CheckboxValue { get; set; }
     }
 }

Basic operations class enough to show read, get current and save. If json file does not exists created it.

 using System;
 using System.Collections.Generic;
 using System.IO;
 using Newtonsoft.Json;
    
 namespace WindowsFormsApp1
 {
     public class Operations
     {
         public static string FileName = 
             Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "items.json");
    
         public static List<ListItem> Items => new List<ListItem>()
         {
             new ListItem() {Id = 1, Item = "value 1", CheckboxValue = true},
             new ListItem() {Id = 1, Item = "value 2", CheckboxValue = false}
         };
    
         public static void SaveMockup()
         {
             using (var file = File.CreateText(FileName))
             {
                 var serializer = new JsonSerializer
                 {
                     Formatting = Formatting.Indented
                 };
    
                 serializer.Serialize(file, Items);
             }
         }
    
         public static List<ListItem> Read()
         {
             using (var reader = new StreamReader(FileName))
             {
                 var json = reader.ReadToEnd();
                 return JsonConvert.DeserializeObject<List<ListItem>>(json);
             }
         }
    
         public static void Save(List<ListItem> listItems)
         {
             using (var file = File.CreateText(FileName))
             {
                 var serializer = new JsonSerializer
                 {
                     Formatting = Formatting.Indented
                 };
    
                 serializer.Serialize(file, listItems);
             }
         }
     }
 }

Form code

 using System;
 using System.Collections.Generic;
 using System.IO;
 using System.Windows.Forms;
    
 namespace WindowsFormsApp1
 {
     public partial class Form1 : Form
     {
         private readonly BindingSource _bindingSource = new BindingSource();
         public Form1()
         {
             InitializeComponent();
    
             if (!File.Exists(Operations.FileName))
             {
                 Operations.SaveMockup();
             }
                
         }
    
         private void Form1_Load(object sender, EventArgs e)
         {
             _bindingSource.DataSource = Operations.Read();
             bindingNavigator1.BindingSource = _bindingSource;
                
                
             CheckBoxItem.DataBindings.Add("Checked", _bindingSource, "CheckboxValue");
                
             IdLabel.DataBindings.Add("Text", _bindingSource, "Id");
         }
    
         private void CurrentButton_Click(object sender, EventArgs e)
         {
             var current = (ListItem) _bindingSource.Current;
             MessageBox.Show(current.CheckboxValue.ToString());
         }
    
         private void SaveButton_Click(object sender, EventArgs e)
         {
             Operations.Save((List<ListItem>) _bindingSource.DataSource);
         }
     }
 }

91061-kpmvp.png



figure1.png (5.2 KiB)
kpmvp.png (2.0 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.