blob: 59b9057beeebfaad903da70740c7c610ec627fc6 [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 {
csharptest0f3540e2011-08-05 20:40:14 -050012 /// <summary> Constructs a new reader </summary>
13 protected AbstractTextReader() { }
14 /// <summary> Constructs a new child reader </summary>
15 protected AbstractTextReader(AbstractTextReader copyFrom)
16 : base(copyFrom)
17 { }
18
csharptest2b868842011-06-10 14:41:47 -050019 /// <summary>
20 /// Reads a typed field as a string
21 /// </summary>
22 protected abstract bool ReadAsText(ref string textValue, Type type);
23
24 /// <summary>
25 /// Returns true if it was able to read a String from the input
26 /// </summary>
27 protected override bool Read(ref string value)
28 {
29 string text = null;
csharptest74c5e0c2011-07-14 13:06:22 -050030 if (ReadAsText(ref text, typeof (string)))
csharptest2b868842011-06-10 14:41:47 -050031 {
32 value = text;
33 return true;
34 }
35 return false;
36 }
37
38 /// <summary>
39 /// Returns true if it was able to read a Boolean from the input
40 /// </summary>
41 protected override bool Read(ref bool value)
42 {
43 string text = null;
csharptest74c5e0c2011-07-14 13:06:22 -050044 if (ReadAsText(ref text, typeof (bool)))
csharptest2b868842011-06-10 14:41:47 -050045 {
46 value = XmlConvert.ToBoolean(text);
47 return true;
48 }
49 return false;
50 }
51
52 /// <summary>
53 /// Returns true if it was able to read a Int32 from the input
54 /// </summary>
55 protected override bool Read(ref int value)
56 {
57 string text = null;
csharptest74c5e0c2011-07-14 13:06:22 -050058 if (ReadAsText(ref text, typeof (int)))
csharptest2b868842011-06-10 14:41:47 -050059 {
60 value = XmlConvert.ToInt32(text);
61 return true;
62 }
63 return false;
64 }
65
66 /// <summary>
67 /// Returns true if it was able to read a UInt32 from the input
68 /// </summary>
69 [CLSCompliant(false)]
70 protected override bool Read(ref uint value)
71 {
72 string text = null;
csharptest74c5e0c2011-07-14 13:06:22 -050073 if (ReadAsText(ref text, typeof (uint)))
csharptest2b868842011-06-10 14:41:47 -050074 {
75 value = XmlConvert.ToUInt32(text);
76 return true;
77 }
78 return false;
79 }
80
81 /// <summary>
82 /// Returns true if it was able to read a Int64 from the input
83 /// </summary>
84 protected override bool Read(ref long value)
85 {
86 string text = null;
csharptest74c5e0c2011-07-14 13:06:22 -050087 if (ReadAsText(ref text, typeof (long)))
csharptest2b868842011-06-10 14:41:47 -050088 {
89 value = XmlConvert.ToInt64(text);
90 return true;
91 }
92 return false;
93 }
94
95 /// <summary>
96 /// Returns true if it was able to read a UInt64 from the input
97 /// </summary>
98 [CLSCompliant(false)]
99 protected override bool Read(ref ulong value)
100 {
101 string text = null;
csharptest74c5e0c2011-07-14 13:06:22 -0500102 if (ReadAsText(ref text, typeof (ulong)))
csharptest2b868842011-06-10 14:41:47 -0500103 {
104 value = XmlConvert.ToUInt64(text);
105 return true;
106 }
107 return false;
108 }
109
110 /// <summary>
111 /// Returns true if it was able to read a Single from the input
112 /// </summary>
113 protected override bool Read(ref float value)
114 {
115 string text = null;
csharptest74c5e0c2011-07-14 13:06:22 -0500116 if (ReadAsText(ref text, typeof (float)))
csharptest2b868842011-06-10 14:41:47 -0500117 {
118 value = XmlConvert.ToSingle(text);
119 return true;
120 }
121 return false;
122 }
123
124 /// <summary>
125 /// Returns true if it was able to read a Double from the input
126 /// </summary>
127 protected override bool Read(ref double value)
128 {
129 string text = null;
csharptest74c5e0c2011-07-14 13:06:22 -0500130 if (ReadAsText(ref text, typeof (double)))
csharptest2b868842011-06-10 14:41:47 -0500131 {
132 value = XmlConvert.ToDouble(text);
133 return true;
134 }
135 return false;
136 }
137
138 /// <summary>
139 /// Provides decoding of bytes read from the input stream
140 /// </summary>
csharptest74c5e0c2011-07-14 13:06:22 -0500141 protected virtual ByteString DecodeBytes(string bytes)
142 {
143 return ByteString.FromBase64(bytes);
144 }
csharptest2b868842011-06-10 14:41:47 -0500145
146 /// <summary>
147 /// Returns true if it was able to read a ByteString from the input
148 /// </summary>
149 protected override bool Read(ref ByteString value)
150 {
151 string text = null;
csharptest74c5e0c2011-07-14 13:06:22 -0500152 if (ReadAsText(ref text, typeof (ByteString)))
csharptest2b868842011-06-10 14:41:47 -0500153 {
154 value = DecodeBytes(text);
155 return true;
156 }
157 return false;
158 }
159
160 /// <summary>
161 /// returns true if it was able to read a single value into the value reference. The value
162 /// stored may be of type System.String, System.Int32, or an IEnumLite from the IEnumLiteMap.
163 /// </summary>
164 protected override bool ReadEnum(ref object value)
165 {
166 string text = null;
csharptest74c5e0c2011-07-14 13:06:22 -0500167 if (ReadAsText(ref text, typeof (Enum)))
csharptest2b868842011-06-10 14:41:47 -0500168 {
169 int number;
170 if (int.TryParse(text, NumberStyles.Integer, CultureInfo.InvariantCulture, out number))
171 {
172 value = number;
173 return true;
174 }
175 value = text;
176 return true;
177 }
178 return false;
179 }
180 }
181}