User Tools

Site Tools

blog:2023-06-19_c_json_serialization_and_deserialization_test



2023-06-19 C#: JSON Serialization and Deserialization Test

  • The code samples in this article:
    • Serialization Cert, Key, and Key2 into JSON string
    • Put the JSON string to JSON string text box
    • Deserialization JSON string from JSON string text box to Cert, Key, and Key2

Code

  •     public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            public static CLASS_CrrtAndKeys VAR_CertAndKeys;
            public static String VAR_TestJsonString = String.Empty;
    
            private void button_ToJson_Click(object sender, EventArgs e)
            {
                // Convert to JSON String
                VAR_CertAndKeys = new CLASS_CrrtAndKeys();
    
                if (checkBox_Cert.Checked) VAR_CertAndKeys.Cert = textBox_Cert.Text;
                if (checkBox_Key1.Checked) VAR_CertAndKeys.Key1 = textBox_Key1.Text;
                if (checkBox_Key2.Checked) VAR_CertAndKeys.Key2 = textBox_Key2.Text;
                VAR_TestJsonString = JsonSerializer.Serialize(VAR_CertAndKeys);
                textBox_JsonString.Text = VAR_TestJsonString;
                richTextBox_Log.AppendText("JSON serialize....\n");
                richTextBox_Log.AppendText("  " + VAR_TestJsonString + "\n");
                richTextBox_Log.ScrollToCaret();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                VAR_CertAndKeys = new CLASS_CrrtAndKeys();
            }
    
            private void button_ParseJson_Click(object sender, EventArgs e)
            {
                // Parse 
                String _JsonStr = textBox_JsonString.Text;
                CLASS_CrrtAndKeys VAR_CertAndKeysDecode = JsonSerializer.Deserialize<CLASS_CrrtAndKeys>(_JsonStr);
                richTextBox_Log.AppendText("JSON deserialize....\n");
                richTextBox_Log.AppendText("  Certification: " + VAR_CertAndKeysDecode.Cert + "\n");
                richTextBox_Log.AppendText("  Key 1: " + VAR_CertAndKeysDecode.Key1 + "\n");
                richTextBox_Log.AppendText("  Key 2: " + VAR_CertAndKeysDecode.Key2 + "\n");
                richTextBox_Log.ScrollToCaret();
            }
        }
    
        public class CLASS_CrrtAndKeys
        {
            public string Cert { get; set; }
            public string Key1 { get; set; }
            public string Key2 { get; set; }
        }

TAGS

  • 66 person(s) visited this page until now.

Permalink blog/2023-06-19_c_json_serialization_and_deserialization_test.txt · Last modified: 2023/06/19 14:00 by jethro

oeffentlich