internal void Constructor(ComponentJson owner) { this.Type = GetType().Name; if (owner == null) { Root = this; } else { Root = owner.Root; owner.List.Add(this); } Root.RootIdCount += 1; Id = Root.RootIdCount; }
static void Main() { // Create Button object to serialize var buttonSource = new Button(null) { TextHtml = "Hello World!", DateTime = DateTime.Now }; List <Row> rowList = new List <Row>(); rowList.Add(new Person() { Name = "Marc" }); rowList.Add(new Person() { Name = "Andrew" }); var person = new Person() { Hello = "ldcsdcl", Name = "John", Value2 = 232M, Value1 = 88, Value3 = 9.5 }; buttonSource.My = new My2(buttonSource) { X = "X", Y = "Y", Row = person, Type2 = typeof(int), GridCell = new GridCell() { Text = "Language" }, RowList = rowList }; ((My2)buttonSource.My).RowList2.Add("j", new Row() { Hello = "Myd2" }); ((My2)buttonSource.My).RowList2.Add("k", new Person() { Hello = "My3", Name = "Mc" }); ((My2)buttonSource.My).TypeList.Add(typeof(string)); ((My2)buttonSource.My).TypeList.Add(typeof(int)); ((My2)buttonSource.My).TypeList.Add(typeof(List <>)); ((My2)buttonSource.My).TypeList.Add(null); buttonSource.Row = new Person { Value1 = 23 }; buttonSource.Person = new Person { Value1 = 23 }; ((My2)buttonSource.My).ListX.Add("4", null); ((My2)buttonSource.My).ListX.Add("5", typeof(string)); ((My2)buttonSource.My).ListX2.Add(null); ((My2)buttonSource.My).ListX2.Add(typeof(Dictionary <,>)); int performanceCount = 1; // 5000; string jsonNewtonsoftSource = null; for (int i = 0; i < performanceCount; i++) { // Serialize with Newtonsoft and inheritance jsonNewtonsoftSource = Newtonsoft.Json.JsonConvert.SerializeObject(buttonSource, new Newtonsoft.Json.JsonSerializerSettings() { TypeNameHandling = Newtonsoft.Json.TypeNameHandling.All }); // Deserialize with Newtonsoft and inheritance Newtonsoft.Json.JsonConvert.DeserializeObject(jsonNewtonsoftSource, new Newtonsoft.Json.JsonSerializerSettings() { TypeNameHandling = Newtonsoft.Json.TypeNameHandling.All }); } // Serialize with System.Text.Json (Init) var options = new JsonSerializerOptions(); options.Converters.Add(new Factory()); options.WriteIndented = true; // Serialize with System.Text.Json string jsonSource = null; ComponentJson buttonDest = null; for (int i = 0; i < performanceCount; i++) { jsonSource = JsonSerializer.Serialize(buttonSource, options); // Deserialize with System.Text.Json buttonDest = JsonSerializer.Deserialize <ComponentJson>(jsonSource, options); var factory = options.Converters.OfType <Factory>().Single(); foreach (var item in factory.ComponentJsonReferenceList) { PropertyInfo propertyInfo = item.PropertyInfo; ComponentJson componentJson = item.ComponentJson; ComponentJson componentJsonReference = factory.ComponentJsonList[item.Id]; propertyInfo.SetValue(componentJson, componentJsonReference); } } // Serialize System.Text.Json deserialized object with Newtonsoft and inheritance again. var jsonNewtonsoftDest = Newtonsoft.Json.JsonConvert.SerializeObject(buttonDest, new Newtonsoft.Json.JsonSerializerSettings() { TypeNameHandling = Newtonsoft.Json.TypeNameHandling.All }); // Compare the two if (jsonNewtonsoftSource != jsonNewtonsoftDest) { throw new Exception("Serialization and deserialization failed!"); } Console.WriteLine(jsonSource); }
public Button(ComponentJson owner) : base(owner) { }
public My2(ComponentJson owner) : base(owner) { }
public ComponentJson(ComponentJson owner) { Constructor(owner); }
public Page(ComponentJson owner) : base(owner) { }