AbstractTextReader.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System;
  2. using System.Globalization;
  3. using System.Xml;
  4. namespace Google.ProtocolBuffers.Serialization
  5. {
  6. /// <summary>
  7. /// Provides a base class for text-parsing readers
  8. /// </summary>
  9. public abstract class AbstractTextReader : AbstractReader
  10. {
  11. /// <summary> Constructs a new reader </summary>
  12. protected AbstractTextReader() { }
  13. /// <summary>
  14. /// Reads a typed field as a string
  15. /// </summary>
  16. protected abstract bool ReadAsText(ref string textValue, Type type);
  17. /// <summary>
  18. /// Returns true if it was able to read a String from the input
  19. /// </summary>
  20. protected override bool Read(ref string value)
  21. {
  22. string text = null;
  23. if (ReadAsText(ref text, typeof(string)))
  24. {
  25. value = text;
  26. return true;
  27. }
  28. return false;
  29. }
  30. /// <summary>
  31. /// Returns true if it was able to read a Boolean from the input
  32. /// </summary>
  33. protected override bool Read(ref bool value)
  34. {
  35. string text = null;
  36. if (ReadAsText(ref text, typeof(bool)))
  37. {
  38. value = XmlConvert.ToBoolean(text);
  39. return true;
  40. }
  41. return false;
  42. }
  43. /// <summary>
  44. /// Returns true if it was able to read a Int32 from the input
  45. /// </summary>
  46. protected override bool Read(ref int value)
  47. {
  48. string text = null;
  49. if (ReadAsText(ref text, typeof(int)))
  50. {
  51. value = XmlConvert.ToInt32(text);
  52. return true;
  53. }
  54. return false;
  55. }
  56. /// <summary>
  57. /// Returns true if it was able to read a UInt32 from the input
  58. /// </summary>
  59. protected override bool Read(ref uint value)
  60. {
  61. string text = null;
  62. if (ReadAsText(ref text, typeof(uint)))
  63. {
  64. value = XmlConvert.ToUInt32(text);
  65. return true;
  66. }
  67. return false;
  68. }
  69. /// <summary>
  70. /// Returns true if it was able to read a Int64 from the input
  71. /// </summary>
  72. protected override bool Read(ref long value)
  73. {
  74. string text = null;
  75. if (ReadAsText(ref text, typeof(long)))
  76. {
  77. value = XmlConvert.ToInt64(text);
  78. return true;
  79. }
  80. return false;
  81. }
  82. /// <summary>
  83. /// Returns true if it was able to read a UInt64 from the input
  84. /// </summary>
  85. protected override bool Read(ref ulong value)
  86. {
  87. string text = null;
  88. if (ReadAsText(ref text, typeof(ulong)))
  89. {
  90. value = XmlConvert.ToUInt64(text);
  91. return true;
  92. }
  93. return false;
  94. }
  95. /// <summary>
  96. /// Returns true if it was able to read a Single from the input
  97. /// </summary>
  98. protected override bool Read(ref float value)
  99. {
  100. string text = null;
  101. if (ReadAsText(ref text, typeof(float)))
  102. {
  103. value = XmlConvert.ToSingle(text);
  104. return true;
  105. }
  106. return false;
  107. }
  108. /// <summary>
  109. /// Returns true if it was able to read a Double from the input
  110. /// </summary>
  111. protected override bool Read(ref double value)
  112. {
  113. string text = null;
  114. if (ReadAsText(ref text, typeof(double)))
  115. {
  116. value = XmlConvert.ToDouble(text);
  117. return true;
  118. }
  119. return false;
  120. }
  121. /// <summary>
  122. /// Provides decoding of bytes read from the input stream
  123. /// </summary>
  124. protected virtual ByteString DecodeBytes(string bytes)
  125. {
  126. return ByteString.FromBase64(bytes);
  127. }
  128. /// <summary>
  129. /// Returns true if it was able to read a ByteString from the input
  130. /// </summary>
  131. protected override bool Read(ref ByteString value)
  132. {
  133. string text = null;
  134. if (ReadAsText(ref text, typeof(ByteString)))
  135. {
  136. value = DecodeBytes(text);
  137. return true;
  138. }
  139. return false;
  140. }
  141. /// <summary>
  142. /// returns true if it was able to read a single value into the value reference. The value
  143. /// stored may be of type System.String, System.Int32, or an IEnumLite from the IEnumLiteMap.
  144. /// </summary>
  145. protected override bool ReadEnum(ref object value)
  146. {
  147. string text = null;
  148. if (ReadAsText(ref text, typeof(Enum)))
  149. {
  150. int number;
  151. if (FrameworkPortability.TryParseInt32(text, NumberStyles.Integer, FrameworkPortability.InvariantCulture, out number))
  152. {
  153. value = number;
  154. return true;
  155. }
  156. value = text;
  157. return true;
  158. }
  159. return false;
  160. }
  161. }
  162. }