aboutsummaryrefslogtreecommitdiff
path: root/src/ProtocolBuffers/Serialization/JsonTextCursor.cs
diff options
context:
space:
mode:
authorcsharptest <roger@csharptest.net>2011-06-10 23:54:53 -0500
committerrogerk <devnull@localhost>2011-06-10 23:54:53 -0500
commit7fc785c1d463c4424cf3e3c8d7fbe796e0197b70 (patch)
tree1ea510abf9dbf61d709e26ba1845b944bb87b8ef /src/ProtocolBuffers/Serialization/JsonTextCursor.cs
parentea5fd37d1dbf8229e358d47095f93d362e73cc5d (diff)
downloadprotobuf-7fc785c1d463c4424cf3e3c8d7fbe796e0197b70.tar.gz
protobuf-7fc785c1d463c4424cf3e3c8d7fbe796e0197b70.tar.bz2
protobuf-7fc785c1d463c4424cf3e3c8d7fbe796e0197b70.zip
Reader/Writer implementations changed to use static factories
Diffstat (limited to 'src/ProtocolBuffers/Serialization/JsonTextCursor.cs')
-rw-r--r--src/ProtocolBuffers/Serialization/JsonTextCursor.cs133
1 files changed, 98 insertions, 35 deletions
diff --git a/src/ProtocolBuffers/Serialization/JsonTextCursor.cs b/src/ProtocolBuffers/Serialization/JsonTextCursor.cs
index 6a8652c8..a0b468f6 100644
--- a/src/ProtocolBuffers/Serialization/JsonTextCursor.cs
+++ b/src/ProtocolBuffers/Serialization/JsonTextCursor.cs
@@ -9,52 +9,115 @@ namespace Google.ProtocolBuffers.Serialization
/// <summary>
/// JSon Tokenizer used by JsonFormatReader
/// </summary>
- class JsonTextCursor
+ abstract class JsonCursor
{
public enum JsType { String, Number, Object, Array, True, False, Null }
- private readonly char[] _buffer;
- private int _bufferPos;
- private readonly TextReader _input;
- private int _lineNo, _linePos;
-
- public JsonTextCursor(char[] input)
+ #region Buffering implementations
+ class JsonStreamCursor : JsonCursor
{
- _input = null;
- _buffer = input;
- _bufferPos = 0;
- _next = Peek();
- _lineNo = 1;
- }
+ private readonly byte[] _buffer;
+ private int _bufferPos;
+ private readonly Stream _input;
- public JsonTextCursor(TextReader input)
- {
- _input = input;
- _next = Peek();
- _lineNo = 1;
+ public JsonStreamCursor(Stream input)
+ {
+ _input = input;
+ _next = _input.ReadByte();
+ }
+ public JsonStreamCursor(byte[] input)
+ {
+ _input = null;
+ _buffer = input;
+ _next = _buffer[_bufferPos];
+ }
+
+ protected override int Peek()
+ {
+ if (_input != null)
+ return _next;
+ else if (_bufferPos < _buffer.Length)
+ return _buffer[_bufferPos];
+ else
+ return -1;
+ }
+
+ protected override int Read()
+ {
+ if (_input != null)
+ {
+ int result = _next;
+ _next = _input.ReadByte();
+ return result;
+ }
+ else if (_bufferPos < _buffer.Length)
+ return _buffer[_bufferPos++];
+ else
+ return -1;
+ }
}
- private int Peek()
+ class JsonTextCursor : JsonCursor
{
- if (_input != null)
- return _input.Peek();
- else if (_bufferPos < _buffer.Length)
- return _buffer[_bufferPos];
- else
- return -1;
+ private readonly char[] _buffer;
+ private int _bufferPos;
+ private readonly TextReader _input;
+
+ public JsonTextCursor(char[] input)
+ {
+ _input = null;
+ _buffer = input;
+ _bufferPos = 0;
+ _next = Peek();
+ }
+
+ public JsonTextCursor(TextReader input)
+ {
+ _input = input;
+ _next = Peek();
+ }
+
+ protected override int Peek()
+ {
+ if (_input != null)
+ return _input.Peek();
+ else if (_bufferPos < _buffer.Length)
+ return _buffer[_bufferPos];
+ else
+ return -1;
+ }
+
+ protected override int Read()
+ {
+ if (_input != null)
+ return _input.Read();
+ else if (_bufferPos < _buffer.Length)
+ return _buffer[_bufferPos++];
+ else
+ return -1;
+ }
}
+ #endregion
+
+ protected int _next;
+ private int _lineNo, _linePos;
- private int Read()
+ public static JsonCursor CreateInstance(byte[] input) { return new JsonStreamCursor(input); }
+ public static JsonCursor CreateInstance(Stream input) { return new JsonStreamCursor(input); }
+ public static JsonCursor CreateInstance(string input) { return new JsonTextCursor(input.ToCharArray()); }
+ public static JsonCursor CreateInstance(TextReader input) { return new JsonTextCursor(input); }
+
+ protected JsonCursor()
{
- if (_input != null)
- return _input.Read();
- else if (_bufferPos < _buffer.Length)
- return _buffer[_bufferPos++];
- else
- return -1;
+ _lineNo = 1;
+ _linePos = 0;
}
+
+ /// <summary>Returns the next character without actually 'reading' it</summary>
+ protected abstract int Peek();
+ /// <summary>Reads the next character in the input</summary>
+ protected abstract int Read();
- int _next;
public Char NextChar { get { SkipWhitespace(); return (char)_next; } }
#region Assert(...)
@@ -62,8 +125,8 @@ namespace Google.ProtocolBuffers.Serialization
private string CharDisplay(int ch)
{
return ch == -1 ? "EOF" :
- (ch > 32 && ch < 127) ? String.Format("'{0}'", (char)ch) :
- String.Format("'\\u{0:x4}'", ch);
+ (ch > 32 && ch < 127) ? String.Format("'{0}'", (char)ch) :
+ String.Format("'\\u{0:x4}'", ch);
}
[System.Diagnostics.DebuggerNonUserCode]
private void Assert(bool cond, char expected)