-Additional BSON work and tests
This commit is contained in:
@@ -34,6 +34,19 @@ using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Newtonsoft.Json.Bson
|
||||
{
|
||||
internal enum BsonReaderState
|
||||
{
|
||||
Normal,
|
||||
ReferenceStart,
|
||||
ReferenceRef,
|
||||
ReferenceId,
|
||||
CodeWScopeStart,
|
||||
CodeWScopeCode,
|
||||
CodeWScopeScope,
|
||||
CodeWScopeScopeObject,
|
||||
CodeWScopeScopeEnd
|
||||
}
|
||||
|
||||
public class BsonReader : JsonReader
|
||||
{
|
||||
private readonly BinaryReader _reader;
|
||||
@@ -41,13 +54,24 @@ namespace Newtonsoft.Json.Bson
|
||||
private readonly List<ContainerContext> _stack;
|
||||
|
||||
private BsonType _currentElementType;
|
||||
|
||||
private BsonReaderState _bsonReaderState;
|
||||
|
||||
private class ContainerContext
|
||||
{
|
||||
public JTokenType Type { get; private set; }
|
||||
public int Length { get; set; }
|
||||
public int Position { get; set; }
|
||||
private int _position;
|
||||
public int Position
|
||||
{
|
||||
get { return _position; }
|
||||
set
|
||||
{
|
||||
if (value > Length && Length != 0)
|
||||
throw new JsonReaderException("Read past end of current container context.");
|
||||
|
||||
_position = value;
|
||||
}
|
||||
}
|
||||
|
||||
public ContainerContext(JTokenType type)
|
||||
{
|
||||
@@ -87,89 +111,23 @@ namespace Newtonsoft.Json.Bson
|
||||
{
|
||||
try
|
||||
{
|
||||
switch (CurrentState)
|
||||
switch (_bsonReaderState)
|
||||
{
|
||||
case State.Start:
|
||||
{
|
||||
JsonToken token = (!_rootTypeIsArray) ? JsonToken.StartObject : JsonToken.StartArray;
|
||||
JTokenType type = (!_rootTypeIsArray) ? JTokenType.Object : JTokenType.Array;
|
||||
|
||||
SetToken(token);
|
||||
ContainerContext newContext = new ContainerContext(type);
|
||||
_stack.Add(newContext);
|
||||
newContext.Length = ReadInt32();
|
||||
return true;
|
||||
}
|
||||
case State.ObjectStart:
|
||||
{
|
||||
SetToken(JsonToken.PropertyName, ReadElement());
|
||||
return true;
|
||||
}
|
||||
case State.Complete:
|
||||
break;
|
||||
case State.Property:
|
||||
{
|
||||
ReadType(_currentElementType);
|
||||
return true;
|
||||
}
|
||||
case State.Object:
|
||||
break;
|
||||
case State.ArrayStart:
|
||||
ReadElement();
|
||||
ReadType(_currentElementType);
|
||||
return true;
|
||||
case State.Array:
|
||||
break;
|
||||
case State.Closed:
|
||||
break;
|
||||
case State.PostValue:
|
||||
ContainerContext context = GetCurrentContext();
|
||||
if (context == null)
|
||||
return false;
|
||||
|
||||
int lengthMinusEnd = context.Length - 1;
|
||||
|
||||
if (context.Position < lengthMinusEnd)
|
||||
{
|
||||
if (context.Type == JTokenType.Array)
|
||||
{
|
||||
ReadElement();
|
||||
ReadType(_currentElementType);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
SetToken(JsonToken.PropertyName, ReadElement());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else if (context.Position == lengthMinusEnd)
|
||||
{
|
||||
if (ReadByte() != 0)
|
||||
throw new JsonReaderException("Unexpected end of object byte value.");
|
||||
|
||||
_stack.RemoveAt(_stack.Count - 1);
|
||||
if (_stack.Count != 0)
|
||||
MovePosition(context.Length);
|
||||
|
||||
JsonToken endToken = (context.Type == JTokenType.Object) ? JsonToken.EndObject : JsonToken.EndArray;
|
||||
SetToken(endToken);
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
case State.ConstructorStart:
|
||||
break;
|
||||
case State.Constructor:
|
||||
break;
|
||||
case State.Error:
|
||||
break;
|
||||
case State.Finished:
|
||||
break;
|
||||
case BsonReaderState.Normal:
|
||||
return ReadNormal();
|
||||
case BsonReaderState.ReferenceStart:
|
||||
case BsonReaderState.ReferenceRef:
|
||||
case BsonReaderState.ReferenceId:
|
||||
return ReadReference();
|
||||
case BsonReaderState.CodeWScopeStart:
|
||||
case BsonReaderState.CodeWScopeCode:
|
||||
case BsonReaderState.CodeWScopeScope:
|
||||
case BsonReaderState.CodeWScopeScopeObject:
|
||||
case BsonReaderState.CodeWScopeScopeEnd:
|
||||
return ReadCodeWScope();
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
throw new JsonReaderException("Unexpected state: {0}".FormatWith(CultureInfo.InvariantCulture, _bsonReaderState));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
catch (EndOfStreamException)
|
||||
{
|
||||
@@ -177,6 +135,189 @@ namespace Newtonsoft.Json.Bson
|
||||
}
|
||||
}
|
||||
|
||||
private bool ReadCodeWScope()
|
||||
{
|
||||
switch (_bsonReaderState)
|
||||
{
|
||||
case BsonReaderState.CodeWScopeStart:
|
||||
SetToken(JsonToken.PropertyName, "$code");
|
||||
_bsonReaderState = BsonReaderState.CodeWScopeCode;
|
||||
return true;
|
||||
case BsonReaderState.CodeWScopeCode:
|
||||
// total CodeWScope size - not used
|
||||
ReadInt32();
|
||||
|
||||
SetToken(JsonToken.String, ReadLengthString());
|
||||
_bsonReaderState = BsonReaderState.CodeWScopeScope;
|
||||
return true;
|
||||
case BsonReaderState.CodeWScopeScope:
|
||||
if (CurrentState == State.PostValue)
|
||||
{
|
||||
SetToken(JsonToken.PropertyName, "$scope");
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
SetToken(JsonToken.StartObject);
|
||||
_bsonReaderState = BsonReaderState.CodeWScopeScopeObject;
|
||||
|
||||
ContainerContext newContext = new ContainerContext(JTokenType.Object);
|
||||
_stack.Add(newContext);
|
||||
newContext.Length = ReadInt32();
|
||||
|
||||
return true;
|
||||
}
|
||||
case BsonReaderState.CodeWScopeScopeObject:
|
||||
bool result = ReadNormal();
|
||||
if (result && TokenType == JsonToken.EndObject)
|
||||
_bsonReaderState = BsonReaderState.CodeWScopeScopeEnd;
|
||||
|
||||
return result;
|
||||
case BsonReaderState.CodeWScopeScopeEnd:
|
||||
SetToken(JsonToken.EndObject);
|
||||
_bsonReaderState = BsonReaderState.Normal;
|
||||
return true;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
|
||||
private bool ReadReference()
|
||||
{
|
||||
switch (CurrentState)
|
||||
{
|
||||
case State.ObjectStart:
|
||||
{
|
||||
SetToken(JsonToken.PropertyName, "$ref");
|
||||
_bsonReaderState = BsonReaderState.ReferenceRef;
|
||||
return true;
|
||||
}
|
||||
case State.Property:
|
||||
{
|
||||
if (_bsonReaderState == BsonReaderState.ReferenceRef)
|
||||
{
|
||||
SetToken(JsonToken.String, ReadLengthString());
|
||||
return true;
|
||||
}
|
||||
else if (_bsonReaderState == BsonReaderState.ReferenceId)
|
||||
{
|
||||
SetToken(JsonToken.Bytes, ReadBytes(12));
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new JsonReaderException("Unexpected state when reading BSON reference: " + _bsonReaderState);
|
||||
}
|
||||
}
|
||||
case State.PostValue:
|
||||
{
|
||||
if (_bsonReaderState == BsonReaderState.ReferenceRef)
|
||||
{
|
||||
SetToken(JsonToken.PropertyName, "$id");
|
||||
_bsonReaderState = BsonReaderState.ReferenceId;
|
||||
return true;
|
||||
}
|
||||
else if (_bsonReaderState == BsonReaderState.ReferenceId)
|
||||
{
|
||||
SetToken(JsonToken.EndObject);
|
||||
_bsonReaderState = BsonReaderState.Normal;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new JsonReaderException("Unexpected state when reading BSON reference: " + _bsonReaderState);
|
||||
}
|
||||
}
|
||||
default:
|
||||
throw new JsonReaderException("Unexpected state when reading BSON reference: " + CurrentState);
|
||||
}
|
||||
}
|
||||
|
||||
private bool ReadNormal()
|
||||
{
|
||||
switch (CurrentState)
|
||||
{
|
||||
case State.Start:
|
||||
{
|
||||
JsonToken token = (!_rootTypeIsArray) ? JsonToken.StartObject : JsonToken.StartArray;
|
||||
JTokenType type = (!_rootTypeIsArray) ? JTokenType.Object : JTokenType.Array;
|
||||
|
||||
SetToken(token);
|
||||
ContainerContext newContext = new ContainerContext(type);
|
||||
_stack.Add(newContext);
|
||||
newContext.Length = ReadInt32();
|
||||
return true;
|
||||
}
|
||||
case State.ObjectStart:
|
||||
{
|
||||
SetToken(JsonToken.PropertyName, ReadElement());
|
||||
return true;
|
||||
}
|
||||
case State.Complete:
|
||||
case State.Closed:
|
||||
return false;
|
||||
case State.Property:
|
||||
{
|
||||
ReadType(_currentElementType);
|
||||
return true;
|
||||
}
|
||||
case State.ArrayStart:
|
||||
ReadElement();
|
||||
ReadType(_currentElementType);
|
||||
return true;
|
||||
case State.PostValue:
|
||||
ContainerContext context = GetCurrentContext();
|
||||
if (context == null)
|
||||
return false;
|
||||
|
||||
int lengthMinusEnd = context.Length - 1;
|
||||
|
||||
if (context.Position < lengthMinusEnd)
|
||||
{
|
||||
if (context.Type == JTokenType.Array)
|
||||
{
|
||||
ReadElement();
|
||||
ReadType(_currentElementType);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
SetToken(JsonToken.PropertyName, ReadElement());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else if (context.Position == lengthMinusEnd)
|
||||
{
|
||||
if (ReadByte() != 0)
|
||||
throw new JsonReaderException("Unexpected end of object byte value.");
|
||||
|
||||
_stack.RemoveAt(_stack.Count - 1);
|
||||
if (_stack.Count != 0)
|
||||
MovePosition(context.Length);
|
||||
|
||||
JsonToken endToken = (context.Type == JTokenType.Object) ? JsonToken.EndObject : JsonToken.EndArray;
|
||||
SetToken(endToken);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new JsonReaderException("Read past end of current container context.");
|
||||
}
|
||||
case State.ConstructorStart:
|
||||
break;
|
||||
case State.Constructor:
|
||||
break;
|
||||
case State.Error:
|
||||
break;
|
||||
case State.Finished:
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private byte ReadByte()
|
||||
{
|
||||
MovePosition(1);
|
||||
@@ -191,46 +332,76 @@ namespace Newtonsoft.Json.Bson
|
||||
SetToken(JsonToken.Float, ReadDouble());
|
||||
break;
|
||||
case BsonType.String:
|
||||
case BsonType.Symbol:
|
||||
SetToken(JsonToken.String, ReadLengthString());
|
||||
break;
|
||||
case BsonType.Object:
|
||||
break;
|
||||
{
|
||||
SetToken(JsonToken.StartObject);
|
||||
|
||||
ContainerContext newContext = new ContainerContext(JTokenType.Object);
|
||||
_stack.Add(newContext);
|
||||
newContext.Length = ReadInt32();
|
||||
break;
|
||||
}
|
||||
case BsonType.Array:
|
||||
break;
|
||||
{
|
||||
SetToken(JsonToken.StartArray);
|
||||
|
||||
ContainerContext newContext = new ContainerContext(JTokenType.Array);
|
||||
_stack.Add(newContext);
|
||||
newContext.Length = ReadInt32();
|
||||
break;
|
||||
}
|
||||
case BsonType.Binary:
|
||||
SetToken(JsonToken.Bytes, ReadBinary());
|
||||
break;
|
||||
case BsonType.Undefined:
|
||||
SetToken(JsonToken.Undefined);
|
||||
break;
|
||||
case BsonType.Oid:
|
||||
byte[] oid = ReadBytes(12);
|
||||
SetToken(JsonToken.Bytes, oid);
|
||||
break;
|
||||
case BsonType.Boolean:
|
||||
bool b = Convert.ToBoolean(ReadByte());
|
||||
SetToken(JsonToken.Boolean, b);
|
||||
break;
|
||||
case BsonType.Date:
|
||||
long ticks = ReadInt64();
|
||||
DateTime dateTime = JsonConvert.ConvertJavaScriptTicksToDateTime(ticks);
|
||||
SetToken(JsonToken.Date, dateTime);
|
||||
break;
|
||||
case BsonType.Null:
|
||||
SetToken(JsonToken.Null);
|
||||
break;
|
||||
case BsonType.Regex:
|
||||
string expression = ReadString();
|
||||
string modifiers = ReadString();
|
||||
|
||||
string regex = @"/" + expression + @"/" + modifiers;
|
||||
SetToken(JsonToken.String, regex);
|
||||
break;
|
||||
case BsonType.Reference:
|
||||
SetToken(JsonToken.StartObject);
|
||||
_bsonReaderState = BsonReaderState.ReferenceStart;
|
||||
break;
|
||||
case BsonType.Code:
|
||||
break;
|
||||
case BsonType.Symbol:
|
||||
SetToken(JsonToken.String, ReadLengthString());
|
||||
break;
|
||||
case BsonType.CodeWScope:
|
||||
SetToken(JsonToken.StartObject);
|
||||
_bsonReaderState = BsonReaderState.CodeWScopeStart;
|
||||
break;
|
||||
case BsonType.Integer:
|
||||
SetToken(JsonToken.Integer, (long)ReadInt32());
|
||||
break;
|
||||
case BsonType.TimeStamp:
|
||||
break;
|
||||
case BsonType.MinKey:
|
||||
break;
|
||||
case BsonType.MaxKey:
|
||||
case BsonType.Long:
|
||||
SetToken(JsonToken.Integer, ReadInt64());
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException("type");
|
||||
throw new ArgumentOutOfRangeException("type", "Unexpected BsonType value: " + type);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -282,6 +453,12 @@ namespace Newtonsoft.Json.Bson
|
||||
return _reader.ReadInt32();
|
||||
}
|
||||
|
||||
private long ReadInt64()
|
||||
{
|
||||
MovePosition(8);
|
||||
return _reader.ReadInt64();
|
||||
}
|
||||
|
||||
private BsonType ReadType()
|
||||
{
|
||||
MovePosition(1);
|
||||
|
||||
Reference in New Issue
Block a user