AbstractTextReader.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. [CLSCompliant(false)]
  60. protected override bool Read(ref uint value)
  61. {
  62. string text = null;
  63. if (ReadAsText(ref text, typeof(uint)))
  64. {
  65. value = XmlConvert.ToUInt32(text);
  66. return true;
  67. }
  68. return false;
  69. }
  70. /// <summary>
  71. /// Returns true if it was able to read a Int64 from the input
  72. /// </summary>
  73. protected override bool Read(ref long value)
  74. {
  75. string text = null;
  76. if (ReadAsText(ref text, typeof(long)))
  77. {
  78. value = XmlConvert.ToInt64(text);
  79. return true;
  80. }
  81. return false;
  82. }
  83. /// <summary>
  84. /// Returns true if it was able to read a UInt64 from the input
  85. /// </summary>
  86. [CLSCompliant(false)]
  87. protected override bool Read(ref ulong value)
  88. {
  89. string text = null;
  90. if (ReadAsText(ref text, typeof(ulong)))
  91. {
  92. value = XmlConvert.ToUInt64(text);
  93. return true;
  94. }
  95. return false;
  96. }
  97. /// <summary>
  98. /// Returns true if it was able to read a Single from the input
  99. /// </summary>
  100. protected override bool Read(ref float value)
  101. {
  102. string text = null;
  103. if (ReadAsText(ref text, typeof(float)))
  104. {
  105. value = XmlConvert.ToSingle(text);
  106. return true;
  107. }
  108. return false;
  109. }
  110. /// <summary>
  111. /// Returns true if it was able to read a Double from the input
  112. /// </summary>
  113. protected override bool Read(ref double value)
  114. {
  115. string text = null;
  116. if (ReadAsText(ref text, typeof(double)))
  117. {
  118. value = XmlConvert.ToDouble(text);
  119. return true;
  120. }
  121. return false;
  122. }
  123. /// <summary>
  124. /// Provides decoding of bytes read from the input stream
  125. /// </summary>
  126. protected virtual ByteString DecodeBytes(string bytes)
  127. {
  128. return ByteString.FromBase64(bytes);
  129. }
  130. /// <summary>
  131. /// Returns true if it was able to read a ByteString from the input
  132. /// </summary>
  133. protected override bool Read(ref ByteString value)
  134. {
  135. string text = null;
  136. if (ReadAsText(ref text, typeof(ByteString)))
  137. {
  138. value = DecodeBytes(text);
  139. return true;
  140. }
  141. return false;
  142. }
  143. /// <summary>
  144. /// returns true if it was able to read a single value into the value reference. The value
  145. /// stored may be of type System.String, System.Int32, or an IEnumLite from the IEnumLiteMap.
  146. /// </summary>
  147. protected override bool ReadEnum(ref object value)
  148. {
  149. string text = null;
  150. if (ReadAsText(ref text, typeof(Enum)))
  151. {
  152. int number;
  153. if (int.TryParse(text, NumberStyles.Integer, CultureInfo.InvariantCulture, out number))
  154. {
  155. value = number;
  156. return true;
  157. }
  158. value = text;
  159. return true;
  160. }
  161. return false;
  162. }
  163. }
  164. }