blob: 83a8dca585570e44ffdc74487e9f0908d733a5f0 [file] [log] [blame]
csharptest2b868842011-06-10 14:41:47 -05001using System;
2using System.Globalization;
3using System.Xml;
4
5namespace 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;
csharptest74c5e0c2011-07-14 13:06:22 -050023 if (ReadAsText(ref text, typeof (string)))
csharptest2b868842011-06-10 14:41:47 -050024 {
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;
csharptest74c5e0c2011-07-14 13:06:22 -050037 if (ReadAsText(ref text, typeof (bool)))
csharptest2b868842011-06-10 14:41:47 -050038 {
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;
csharptest74c5e0c2011-07-14 13:06:22 -050051 if (ReadAsText(ref text, typeof (int)))
csharptest2b868842011-06-10 14:41:47 -050052 {
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;
csharptest74c5e0c2011-07-14 13:06:22 -050066 if (ReadAsText(ref text, typeof (uint)))
csharptest2b868842011-06-10 14:41:47 -050067 {
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;
csharptest74c5e0c2011-07-14 13:06:22 -050080 if (ReadAsText(ref text, typeof (long)))
csharptest2b868842011-06-10 14:41:47 -050081 {
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;
csharptest74c5e0c2011-07-14 13:06:22 -050095 if (ReadAsText(ref text, typeof (ulong)))
csharptest2b868842011-06-10 14:41:47 -050096 {
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;
csharptest74c5e0c2011-07-14 13:06:22 -0500109 if (ReadAsText(ref text, typeof (float)))
csharptest2b868842011-06-10 14:41:47 -0500110 {
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;
csharptest74c5e0c2011-07-14 13:06:22 -0500123 if (ReadAsText(ref text, typeof (double)))
csharptest2b868842011-06-10 14:41:47 -0500124 {
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>
csharptest74c5e0c2011-07-14 13:06:22 -0500134 protected virtual ByteString DecodeBytes(string bytes)
135 {
136 return ByteString.FromBase64(bytes);
137 }
csharptest2b868842011-06-10 14:41:47 -0500138
139 /// <summary>
140 /// Returns true if it was able to read a ByteString from the input
141 /// </summary>
142 protected override bool Read(ref ByteString value)
143 {
144 string text = null;
csharptest74c5e0c2011-07-14 13:06:22 -0500145 if (ReadAsText(ref text, typeof (ByteString)))
csharptest2b868842011-06-10 14:41:47 -0500146 {
147 value = DecodeBytes(text);
148 return true;
149 }
150 return false;
151 }
152
153 /// <summary>
154 /// returns true if it was able to read a single value into the value reference. The value
155 /// stored may be of type System.String, System.Int32, or an IEnumLite from the IEnumLiteMap.
156 /// </summary>
157 protected override bool ReadEnum(ref object value)
158 {
159 string text = null;
csharptest74c5e0c2011-07-14 13:06:22 -0500160 if (ReadAsText(ref text, typeof (Enum)))
csharptest2b868842011-06-10 14:41:47 -0500161 {
162 int number;
163 if (int.TryParse(text, NumberStyles.Integer, CultureInfo.InvariantCulture, out number))
164 {
165 value = number;
166 return true;
167 }
168 value = text;
169 return true;
170 }
171 return false;
172 }
173 }
174}