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