aboutsummaryrefslogtreecommitdiff
path: root/src/ProtocolBuffers.Serialization/AbstractTextReader.cs
diff options
context:
space:
mode:
authorcsharptest <roger@csharptest.net>2011-08-05 20:40:14 -0500
committerrogerk <devnull@localhost>2011-08-05 20:40:14 -0500
commit0f3540e24b7b5cf66b59b97cb824fd5449fb77b4 (patch)
treef07ad11ac6c814df0f6b8091eae023cf5f5d5cc1 /src/ProtocolBuffers.Serialization/AbstractTextReader.cs
parent304ff3a83823fdfce6e2eac40cc44e2b4b1fd3c3 (diff)
downloadprotobuf-0f3540e24b7b5cf66b59b97cb824fd5449fb77b4.tar.gz
protobuf-0f3540e24b7b5cf66b59b97cb824fd5449fb77b4.tar.bz2
protobuf-0f3540e24b7b5cf66b59b97cb824fd5449fb77b4.zip
Split off the Serialization namespace into a new assembly.
Diffstat (limited to 'src/ProtocolBuffers.Serialization/AbstractTextReader.cs')
-rw-r--r--src/ProtocolBuffers.Serialization/AbstractTextReader.cs181
1 files changed, 181 insertions, 0 deletions
diff --git a/src/ProtocolBuffers.Serialization/AbstractTextReader.cs b/src/ProtocolBuffers.Serialization/AbstractTextReader.cs
new file mode 100644
index 00000000..59b9057b
--- /dev/null
+++ b/src/ProtocolBuffers.Serialization/AbstractTextReader.cs
@@ -0,0 +1,181 @@
+using System;
+using System.Globalization;
+using System.Xml;
+
+namespace Google.ProtocolBuffers.Serialization
+{
+ /// <summary>
+ /// Provides a base class for text-parsing readers
+ /// </summary>
+ public abstract class AbstractTextReader : AbstractReader
+ {
+ /// <summary> Constructs a new reader </summary>
+ protected AbstractTextReader() { }
+ /// <summary> Constructs a new child reader </summary>
+ protected AbstractTextReader(AbstractTextReader copyFrom)
+ : base(copyFrom)
+ { }
+
+ /// <summary>
+ /// Reads a typed field as a string
+ /// </summary>
+ protected abstract bool ReadAsText(ref string textValue, Type type);
+
+ /// <summary>
+ /// Returns true if it was able to read a String from the input
+ /// </summary>
+ protected override bool Read(ref string value)
+ {
+ string text = null;
+ if (ReadAsText(ref text, typeof (string)))
+ {
+ value = text;
+ return true;
+ }
+ return false;
+ }
+
+ /// <summary>
+ /// Returns true if it was able to read a Boolean from the input
+ /// </summary>
+ protected override bool Read(ref bool value)
+ {
+ string text = null;
+ if (ReadAsText(ref text, typeof (bool)))
+ {
+ value = XmlConvert.ToBoolean(text);
+ return true;
+ }
+ return false;
+ }
+
+ /// <summary>
+ /// Returns true if it was able to read a Int32 from the input
+ /// </summary>
+ protected override bool Read(ref int value)
+ {
+ string text = null;
+ if (ReadAsText(ref text, typeof (int)))
+ {
+ value = XmlConvert.ToInt32(text);
+ return true;
+ }
+ return false;
+ }
+
+ /// <summary>
+ /// Returns true if it was able to read a UInt32 from the input
+ /// </summary>
+ [CLSCompliant(false)]
+ protected override bool Read(ref uint value)
+ {
+ string text = null;
+ if (ReadAsText(ref text, typeof (uint)))
+ {
+ value = XmlConvert.ToUInt32(text);
+ return true;
+ }
+ return false;
+ }
+
+ /// <summary>
+ /// Returns true if it was able to read a Int64 from the input
+ /// </summary>
+ protected override bool Read(ref long value)
+ {
+ string text = null;
+ if (ReadAsText(ref text, typeof (long)))
+ {
+ value = XmlConvert.ToInt64(text);
+ return true;
+ }
+ return false;
+ }
+
+ /// <summary>
+ /// Returns true if it was able to read a UInt64 from the input
+ /// </summary>
+ [CLSCompliant(false)]
+ protected override bool Read(ref ulong value)
+ {
+ string text = null;
+ if (ReadAsText(ref text, typeof (ulong)))
+ {
+ value = XmlConvert.ToUInt64(text);
+ return true;
+ }
+ return false;
+ }
+
+ /// <summary>
+ /// Returns true if it was able to read a Single from the input
+ /// </summary>
+ protected override bool Read(ref float value)
+ {
+ string text = null;
+ if (ReadAsText(ref text, typeof (float)))
+ {
+ value = XmlConvert.ToSingle(text);
+ return true;
+ }
+ return false;
+ }
+
+ /// <summary>
+ /// Returns true if it was able to read a Double from the input
+ /// </summary>
+ protected override bool Read(ref double value)
+ {
+ string text = null;
+ if (ReadAsText(ref text, typeof (double)))
+ {
+ value = XmlConvert.ToDouble(text);
+ return true;
+ }
+ return false;
+ }
+
+ /// <summary>
+ /// Provides decoding of bytes read from the input stream
+ /// </summary>
+ protected virtual ByteString DecodeBytes(string bytes)
+ {
+ return ByteString.FromBase64(bytes);
+ }
+
+ /// <summary>
+ /// Returns true if it was able to read a ByteString from the input
+ /// </summary>
+ protected override bool Read(ref ByteString value)
+ {
+ string text = null;
+ if (ReadAsText(ref text, typeof (ByteString)))
+ {
+ value = DecodeBytes(text);
+ return true;
+ }
+ return false;
+ }
+
+ /// <summary>
+ /// returns true if it was able to read a single value into the value reference. The value
+ /// stored may be of type System.String, System.Int32, or an IEnumLite from the IEnumLiteMap.
+ /// </summary>
+ protected override bool ReadEnum(ref object value)
+ {
+ string text = null;
+ if (ReadAsText(ref text, typeof (Enum)))
+ {
+ int number;
+ if (int.TryParse(text, NumberStyles.Integer, CultureInfo.InvariantCulture, out number))
+ {
+ value = number;
+ return true;
+ }
+ value = text;
+ return true;
+ }
+ return false;
+ }
+ }
+} \ No newline at end of file