/ Published in: C#
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
// Purpose of this code: to test and understand JSON usage in C#. // What it does: // Defines a datatype that is nested, with an array and enum // Creates and initializes and object // Converts to JSON string and back into a 2nd object and deep-equals the two (should be the same). // modifies the JSON string (only formatting) and converts back into a 3rd object and compares (should be the same). // modifies the data in the JSON string and converts back into a 4th object and compares (should be different). // Creates a totally different string with the same data as the initial object. Converts string to object. (should be the same). #region Declare a type that: [1] is nested, [2] has an enum, [3] has an array. public enum EmpType // example enum { salaried, hourly, contract, }; public class MyEmployee { public string name { get; set; } public string id { get; set; } public int age { get; set; } public bool ok { get; set; } public EmpType empType { get; set; } } public class MyTopLevel { public string s { get; set; } public List<MyEmployee> list; public int i { get; set; } // Equals effectively performs a deep equals with a slight chance of returning a false equals. public bool Equals(MyTopLevel that) { // Note, there's a one in 4,294,967,295 chance that this will provide a false equals // http://mikehadlow.blogspot.com/2006/11/using-memorystream-and-binaryformatter.html return this.GetHashCode() == that.GetHashCode(); } } #endregion // Allocate and initialize data MyTopLevel myObj_1 = new MyTopLevel { s = "asdf", { }, i = 1234 }; #region Serialize serializer.WriteObject(ms1, myObj_1); string str = Encoding.Default.GetString(ms1.ToArray()); MessageBox.Show(str); #endregion #region Deserialization string str2 = str.Replace(",", ",\n");// This will change the formatting of the string without changing the data. MyTopLevel myObj_2 = serializer.ReadObject(ms) as MyTopLevel; ms.Close(); // Test the reconstituted object - it should be deep-equal to the first object. System.Diagnostics.Debug.Assert(myObj_1.Equals(myObj_2)); #endregion #region Test !Equals using an object created from modified data string str = str.Replace("aaa", "aa"); // change an element of the serialized data to test .Equals(). MyTopLevel myObj_3 = serializer.ReadObject(ms) as MyTopLevel; ms.Close(); // Test the reconstitued object. It should not be deep-equal to the first object because of the str.Replace(). System.Diagnostics.Debug.Assert(!myObj_1.Equals(myObj_3)); #endregion // Generate a string expecting to create an object identical (deep equal) to the first object. // Omit empType 0 expecting it to result in the default of zero. // Add indentation and white spaces. str = "{" + " \"i\":1234,\n" + " \"list\":\n" + " [\n" + " {\"age\":30, \"empType\":2, \"id\":\"111\", \"name\":\"aaa\", \"ok\":true },\n" + " {\"age\":31, \"empType\":1, \"id\":\"Cohen\", \"name\":\"bbb\", \"ok\":false},\n" + " {\"age\":20, \"id\":\"Biton\", \"name\":\"ccc\", \"ok\":true }\n" + " ],\n" + " \"s\":\"asdf\"\n" + "}\n"; MessageBox.Show(str); MyTopLevel myObj_4 = serializer.ReadObject(ms) as MyTopLevel; ms.Close(); // Test the reconstitued object. It should not be deep-equal to the first object because of the str.Replace(). System.Diagnostics.Debug.Assert(!myObj_1.Equals(myObj_4));