/// <summary> /// Reads a typed array. /// </summary> /// <param name="T">Element type.</param> /// <param name="Provider">Database provider object.</param> /// <param name="Reader">Binary reader.</param> /// <param name="FieldDataType">Field data type.</param> /// <returns>String value.</returns> /// <exception cref="ArgumentException">If the <paramref name="FieldDataType"/> was invalid.</exception> public static Array ReadArray(Type T, MongoDBProvider Provider, IBsonReader Reader, BsonType FieldDataType) { switch (FieldDataType) { case BsonType.Array: List <object> Elements = new List <object>(); IObjectSerializer S = Provider.GetObjectSerializer(T ?? typeof(GenericObject)); Reader.ReadStartArray(); while (Reader.State != BsonReaderState.EndOfArray) { BsonType?ElementType = null; if (Reader.State == BsonReaderState.Type) { ElementType = Reader.ReadBsonType(); if (ElementType == BsonType.EndOfDocument) { break; } } Elements.Add(S.Deserialize(Reader, ElementType, true)); } Reader.ReadEndArray(); if (T is null) { return(Elements.ToArray()); } int c = Elements.Count; Array Result = Array.CreateInstance(T, c); Array.Copy(Elements.ToArray(), 0, Result, 0, c); return(Result); case BsonType.Binary: byte[] Bin = Reader.ReadBytes(); if (T is null || T == typeof(byte)) { return(Bin); } c = Bin.Length; Result = Array.CreateInstance(T, c); Array.Copy(Bin, 0, Result, 0, c); return(Result); case BsonType.Null: Reader.ReadNull(); return(null); default: throw new Exception("Array expected."); } }
static IList ReadArray(IBsonReader bsonReader) { var array = new ArrayList(); bsonReader.ReadStartArray(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { array.Add(ReadObject(bsonReader)); } bsonReader.ReadEndArray(); return(array); }
public void TestArrayEmpty() { var json = "[]"; using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.Array, _bsonReader.ReadBsonType()); _bsonReader.ReadStartArray(); Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType()); _bsonReader.ReadEndArray(); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } Assert.Equal(json, BsonSerializer.Deserialize<BsonArray>(json).ToJson()); }
public void TestArrayEmpty() { var json = "[]"; using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.Array, _bsonReader.ReadBsonType()); _bsonReader.ReadStartArray(); Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType()); _bsonReader.ReadEndArray(); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } Assert.Equal(json, BsonSerializer.Deserialize <BsonArray>(json).ToJson()); }
public void TestArrayOneElement() { var json = "[1]"; using (_bsonReader = new JsonReader(json)) { Assert.AreEqual(BsonType.Array, _bsonReader.ReadBsonType()); _bsonReader.ReadStartArray(); Assert.AreEqual(BsonType.Int32, _bsonReader.ReadBsonType()); Assert.AreEqual(1, _bsonReader.ReadInt32()); Assert.AreEqual(BsonType.EndOfDocument, _bsonReader.ReadBsonType()); _bsonReader.ReadEndArray(); Assert.AreEqual(BsonReaderState.Done, _bsonReader.State); } Assert.AreEqual(json, BsonSerializer.Deserialize <BsonArray>(json).ToJson()); }
/// <inheritdoc/> public override bool Read() { switch (_wrappedReader.State) { case BsonReaderState.Closed: case BsonReaderState.Done: return(false); case BsonReaderState.EndOfArray: _wrappedReader.ReadEndArray(); SetCurrentToken(Newtonsoft.Json.JsonToken.EndArray); return(true); case BsonReaderState.EndOfDocument: _wrappedReader.ReadEndDocument(); SetCurrentToken(Newtonsoft.Json.JsonToken.EndObject); return(true); case BsonReaderState.Initial: case BsonReaderState.Type: if (_wrappedReader.State == BsonReaderState.Initial && _wrappedReader.IsAtEndOfFile()) { SetCurrentToken(Newtonsoft.Json.JsonToken.None); return(false); } else { _wrappedReader.ReadBsonType(); return(Read()); } case BsonReaderState.Name: var name = _wrappedReader.ReadName(); name = _propertyNameBinder?.ReadName(name) ?? name; SetCurrentToken(Newtonsoft.Json.JsonToken.PropertyName, name); return(true); case BsonReaderState.Value: ReadValue(); return(true); default: throw new Newtonsoft.Json.JsonReaderException(string.Format("Unexpected IBsonReader state: {0}.", _wrappedReader.State)); } }
public void TestNestedArray() { var json = "{ \"a\" : [1, 2] }"; using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType()); _bsonReader.ReadStartDocument(); Assert.Equal(BsonType.Array, _bsonReader.ReadBsonType()); Assert.Equal("a", _bsonReader.ReadName()); _bsonReader.ReadStartArray(); Assert.Equal(1, _bsonReader.ReadInt32()); Assert.Equal(2, _bsonReader.ReadInt32()); _bsonReader.ReadEndArray(); _bsonReader.ReadEndDocument(); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } Assert.Equal(json, BsonSerializer.Deserialize <BsonDocument>(json).ToJson()); }
/// <summary> /// 安全地读入数组字段 /// </summary> /// <param name="bsonReader">Bson读取器</param> /// <param name="expected">字段名</param> /// <param name="read">字段名缓存</param> /// <param name="parser">数组元素读取器</param> /// <returns>读取结果</returns> public static List <T> ReadArray <T>(this IBsonReader bsonReader, string expected, ref string read, Func <IBsonReader, T> parser) { if (!ReadPrep(bsonReader, expected, ref read)) { return(null); } var lst = new List <T>(); bsonReader.ReadStartArray(); while (!bsonReader.IsEndOfArray()) { lst.Add(parser(bsonReader)); } bsonReader.ReadEndArray(); return(lst); }
/// <summary> /// Reads a typed array. /// </summary> /// <typeparam name="T">Element type.</typeparam> /// <param name="Provider">Database provider object.</param> /// <param name="Reader">Binary reader.</param> /// <param name="FieldDataType">Field data type.</param> /// <returns>String value.</returns> /// <exception cref="ArgumentException">If the <paramref name="FieldDataType"/> was invalid.</exception> public static T[] ReadArray <T>(MongoDBProvider Provider, IBsonReader Reader, BsonType FieldDataType) { switch (FieldDataType) { case BsonType.Array: List <T> Elements = new List <T>(); IObjectSerializer S = Provider.GetObjectSerializer(typeof(T)); Reader.ReadStartArray(); while (Reader.State != BsonReaderState.EndOfArray) { BsonType?ElementType = null; if (Reader.State == BsonReaderState.Type) { ElementType = Reader.ReadBsonType(); if (ElementType == BsonType.EndOfDocument) { break; } } Elements.Add((T)S.Deserialize(Reader, ElementType, true)); } Reader.ReadEndArray(); return(Elements.ToArray()); case BsonType.Binary: object Temp = Reader.ReadBytes(); return((T[])Temp); case BsonType.Null: Reader.ReadNull(); return(null); default: throw new Exception("Array expected."); } }
private JArray GenerateJArray(IBsonReader reader, JToken parent) { var jarray = new JArray(); reader.ReadStartArray(); do { if (reader.State == BsonReaderState.Type) { reader.ReadBsonType(); } if (reader.State == BsonReaderState.Value) { var value = GenerateJToken(reader, jarray); jarray.Add(value); } } while (reader.State != BsonReaderState.EndOfArray); reader.ReadEndArray(); return(jarray); }
public void TestNestedArray() { var json = "{ \"a\" : [1, 2] }"; using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType()); _bsonReader.ReadStartDocument(); Assert.Equal(BsonType.Array, _bsonReader.ReadBsonType()); Assert.Equal("a", _bsonReader.ReadName()); _bsonReader.ReadStartArray(); Assert.Equal(1, _bsonReader.ReadInt32()); Assert.Equal(2, _bsonReader.ReadInt32()); _bsonReader.ReadEndArray(); _bsonReader.ReadEndDocument(); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } Assert.Equal(json, BsonSerializer.Deserialize<BsonDocument>(json).ToJson()); }
public override bool Read() { if (bsonReader.State == BsonReaderState.Initial || bsonReader.State == BsonReaderState.ScopeDocument || bsonReader.State == BsonReaderState.Type) { bsonReader.ReadBsonType(); } if (bsonReader.State == BsonReaderState.Name) { SetToken(NewtonsoftJsonToken.PropertyName, bsonReader.ReadName().UnescapeBson()); } else if (bsonReader.State == BsonReaderState.Value) { switch (bsonReader.CurrentBsonType) { case BsonType.Document: SetToken(NewtonsoftJsonToken.StartObject); bsonReader.ReadStartDocument(); break; case BsonType.Array: SetToken(NewtonsoftJsonToken.StartArray); bsonReader.ReadStartArray(); break; case BsonType.Undefined: SetToken(NewtonsoftJsonToken.Undefined); bsonReader.ReadUndefined(); break; case BsonType.Null: SetToken(NewtonsoftJsonToken.Null); bsonReader.ReadNull(); break; case BsonType.String: SetToken(NewtonsoftJsonToken.String, bsonReader.ReadString()); break; case BsonType.Binary: SetToken(NewtonsoftJsonToken.Bytes, bsonReader.ReadBinaryData().Bytes); break; case BsonType.Boolean: SetToken(NewtonsoftJsonToken.Boolean, bsonReader.ReadBoolean()); break; case BsonType.DateTime: SetToken(NewtonsoftJsonToken.Date, bsonReader.ReadDateTime()); break; case BsonType.Int32: SetToken(NewtonsoftJsonToken.Integer, bsonReader.ReadInt32()); break; case BsonType.Int64: SetToken(NewtonsoftJsonToken.Integer, bsonReader.ReadInt64()); break; case BsonType.Double: SetToken(NewtonsoftJsonToken.Float, bsonReader.ReadDouble()); break; case BsonType.Decimal128: SetToken(NewtonsoftJsonToken.Float, Decimal128.ToDouble(bsonReader.ReadDecimal128())); break; default: throw new NotSupportedException(); } } else if (bsonReader.State == BsonReaderState.EndOfDocument) { SetToken(NewtonsoftJsonToken.EndObject); bsonReader.ReadEndDocument(); } else if (bsonReader.State == BsonReaderState.EndOfArray) { SetToken(NewtonsoftJsonToken.EndArray); bsonReader.ReadEndArray(); } if (bsonReader.State == BsonReaderState.Initial) { return(true); } return(!bsonReader.IsAtEndOfFile()); }
public static bool TryDeserializeToAny( this IBsonReader reader, IEnumerable <Type> nominalTypes, [MaybeNull] out object[] values ) { values = default; var success = false; // TODO: Prioritize primitives (including string), value types, then objects. var orderedTypes = nominalTypes .OrderBy(t => GetTypePriority(t)) .ToArray(); // Oddly enough BsonSerializer.Deserialize is fine with primitive // arrays but not arrays of documents. Go figure. if (reader.GetCurrentBsonType() == BsonType.Array) { var tempList = new List <object>(); success = true; reader.ReadStartArray(); while (reader.State != BsonReaderState.EndOfArray) { object?value = null; foreach (var nominalType in nominalTypes) { try { value = BsonSerializer.Deserialize( reader, nominalType ); break; } catch { } } if (value is null) { success = false; } else { tempList.Add(value); } } reader.ReadEndArray(); values = tempList.ToArray(); success = values.Any(); } else { values = new object[1]; foreach (var nominalType in nominalTypes) { try { values[0] = BsonSerializer.Deserialize( reader, nominalType ); success = true; break; } catch { } } } return(success); }