fork download
  1. using System;
  2. using System.Xml;
  3. using System.Diagnostics;
  4. using System.Text.Json;
  5. using System.Xml.Linq;
  6. using System.Xml.Serialization;
  7.  
  8.  
  9. public class Message {
  10. public Message(int from, int to, string msg)
  11. {
  12. Debug.Assert( !string.IsNullOrEmpty( msg ) );
  13.  
  14. this.From = from;
  15. this.To = to;
  16. this.Msg = msg;
  17. }
  18.  
  19. public int From { get; }
  20. public int To { get; }
  21. public string Msg { get; }
  22.  
  23. public override string ToString()
  24. {
  25. return $"From: {From} -> To: {To}: \"{Msg}\"";
  26. }
  27. }
  28.  
  29.  
  30. // ## JSON
  31. public class JsonMessage {
  32. public JsonMessage(Message m)
  33. {
  34. this.Message = m;
  35. }
  36.  
  37. public void Save(string fn)
  38. {
  39. File.WriteAllText( fn, JsonSerializer.Serialize( this.Message ) );
  40. }
  41.  
  42. public static Message Load(string fn)
  43. {
  44. return JsonSerializer.Deserialize<Message>(
  45. File.ReadAllText( fn )
  46. ?? throw new IOException( "error reading file: " + fn ) )
  47. ?? throw new JsonException( "error reading JSON from: " + fn );
  48. }
  49.  
  50. public Message Message { get; }
  51. }
  52.  
  53.  
  54. // ## XML 1 (doesn't work, no parameterless constructor in Message)
  55. public class XmlMessage {
  56. public XmlMessage(Message m)
  57. {
  58. this.Message = m;
  59. }
  60.  
  61. public Message Message { get; }
  62.  
  63. public void Save(string fn)
  64. {
  65. using ( var f = File.OpenWrite( fn ) ) {
  66. new XmlSerializer( this.Message.GetType() )
  67. .Serialize( f, this.Message );
  68. }
  69. }
  70.  
  71. public static Message Load(string fn)
  72. {
  73. using ( var f = File.OpenRead( fn ) ) {
  74. return (Message) new XmlSerializer( typeof( Message ) )
  75. .Deserialize( f )
  76. ?? throw new XmlException( "error reading XML from: " + fn );
  77. }
  78. }
  79. }
  80.  
  81.  
  82. // ## XML 2 (bespoken XML)
  83. public class XmlMessage2 {
  84. public XmlMessage2(Message m)
  85. {
  86. this.Message = m;
  87. }
  88.  
  89. public Message Message { get; }
  90.  
  91. public void Save(string fn)
  92. {
  93. new XElement("Message",
  94. new XAttribute( "from", this.Message.From ),
  95. new XAttribute( "to", this.Message.To ),
  96. this.Message.Msg ).Save( fn );
  97. }
  98.  
  99. public static Message Load(string fn)
  100. {
  101. var msgElem = XElement.Load( fn ) ?? throw new IOException( "reading " + fn );
  102. int from = (int?) msgElem.Attribute( "from" ) ?? throw new XmlException( "missing from" );
  103. int to = (int?) msgElem.Attribute( "to" ) ?? throw new XmlException( "missing to" );
  104.  
  105. return new Message( from, to, msgElem.Value );
  106. }
  107. }
  108.  
  109.  
  110. // ## XML 3 (using a DTO class)
  111. public class MessageDTO {
  112. public int From { get; set; }
  113. public int To { get; set; }
  114. public string Msg { get; set; }
  115.  
  116. public override string ToString()
  117. {
  118. return $"[MessageDTO [From: {From}][To: {To}]: \"{Msg}\"]";
  119. }
  120.  
  121. public Message ToMessage()
  122. {
  123. return new Message( this.From, this.To, this.Msg );
  124. }
  125.  
  126. public static MessageDTO FromMessage(Message m)
  127. {
  128. return new MessageDTO { From = m.From, To = m.To, Msg = m.Msg };
  129. }
  130. }
  131.  
  132.  
  133. public class XmlMessage3 {
  134. public XmlMessage3(Message m)
  135. {
  136. this.Message = m;
  137. }
  138.  
  139. public Message Message { get; }
  140.  
  141. public void Save(string fn)
  142. {
  143. using ( var f = File.OpenWrite( fn ) ) {
  144. new XmlSerializer( typeof( MessageDTO ) )
  145. .Serialize(
  146. f, MessageDTO.FromMessage( this.Message ) );
  147. }
  148. }
  149.  
  150. public static Message Load(string fn)
  151. {
  152. using ( var f = File.OpenRead( fn ) ) {
  153. return ( (MessageDTO) new XmlSerializer( typeof( MessageDTO ) )
  154. .Deserialize( f ) ).ToMessage()
  155. ?? throw new XmlException( "error reading XML from: " + fn );
  156. }
  157. }
  158. }
  159.  
  160.  
  161. public class TestSerialization
  162. {
  163. public static void Main()
  164. {
  165. var m1 = new Message( 455123132, 555789789, "¿Peli y manta esta noche?" );
  166.  
  167. new JsonMessage( m1 ).Save( "msg.json" );
  168. Console.WriteLine( JsonMessage.Load( "msg.json" ) );
  169.  
  170. new XmlMessage2( m1 ).Save( "msg.xml" );
  171. Console.WriteLine( XmlMessage2.Load( "msg.xml" ) );
  172.  
  173. new XmlMessage3( m1 ).Save( "msg.xml" );
  174. Console.WriteLine( XmlMessage3.Load( "msg.xml" ) );
  175. }
  176. }
  177.  
Success #stdin #stdout 0.16s 44004KB
stdin
Standard input is empty
stdout
From: 455123132 -> To: 555789789: "¿Peli y manta esta noche?"
From: 455123132 -> To: 555789789: "¿Peli y manta esta noche?"
From: 455123132 -> To: 555789789: "¿Peli y manta esta noche?"