Jimi2000

ObjectModel Serialization

Dec 8th, 2019
501
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.25 KB | None | 0 0
  1.     public class MyItems
  2.     {
  3.         [JsonProperty("MyItems")]
  4.         public IList<MyItemModel> Items { get; set; }
  5.     }
  6.  
  7.     public class MyItemModel
  8.     {
  9.         public MyItem ItemMap { get; set; }
  10.         public GeneralSettings Settings { get; set; }
  11.     }
  12.  
  13.     public class MyItem
  14.     {
  15.         public string MapName { get; set; }
  16.         public string MapDescription { get; set; }
  17.         public string MapComments { get; set; }
  18.         public string MapVersion { get; set; }
  19.     }
  20.  
  21.     public class GeneralSettings
  22.     {
  23.         public Color Foreground { get; set; }
  24.         public Color Background { get; set; }
  25.     }
  26.  
  27.     // -------------------------------------------------
  28.     // Serialize and deserialize the Root ObjectModel
  29.     // -------------------------------------------------
  30.  
  31.     MyItems items = new MyItems();
  32.     items.Items = new List<MyItemModel>() {
  33.         new MyItemModel() {
  34.             ItemMap = new MyItem() { MapName = "MyMapName", MapComments = "Comment" },
  35.             Settings = new GeneralSettings() { Foreground = Color.Red, Background = Color.White }
  36.         }
  37.     }
  38.  
  39.     var serialized = JsonConvert.SerializeObject(items);
  40.     var deserialized = JsonConvert.DeserializeObject<MyItems>(serialized);
Advertisement
Add Comment
Please, Sign In to add comment