aboutsummaryrefslogtreecommitdiff
path: root/csharp/src/ProtocolBuffers.Serialization/AbstractTextReader.cs
blob: 41578fabb7e9f6cb675eac5f992c0f2663daf18f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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>
        /// 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>
        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>
        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 (FrameworkPortability.TryParseInt32(text, NumberStyles.Integer, FrameworkPortability.InvariantCulture, out number))
                {
                    value = number;
                    return true;
                }
                value = text;
                return true;
            }
            return false;
        }
    }
}