// private methods private bool IsCSharpNullRepresentation(IBsonReader bsonReader) { var bookmark = bsonReader.GetBookmark(); bsonReader.ReadStartDocument(); var bsonType = bsonReader.ReadBsonType(); if (bsonType == BsonType.Boolean) { var name = bsonReader.ReadName(); if (name == "_csharpnull" || name == "$csharpnull") { var value = bsonReader.ReadBoolean(); if (value) { bsonType = bsonReader.ReadBsonType(); if (bsonType == BsonType.EndOfDocument) { bsonReader.ReadEndDocument(); return(true); } } } } bsonReader.ReturnToBookmark(bookmark); return(false); }
private void DeserializeDataPoints(IBsonReader reader, ref Measurement m) { IDictionary <string, DataPoint> datapoints; DataPoint dp; string key; datapoints = new Dictionary <string, DataPoint>(); reader.ReadStartDocument(); while (reader.State != BsonReaderState.EndOfDocument) { dp = new DataPoint(); key = reader.ReadName(); reader.ReadStartDocument(); while (reader.CurrentBsonType != BsonType.EndOfDocument) { this.DeserializeDataPointAttribute(reader.ReadName(), reader, ref dp); reader.ReadBsonType(); } reader.ReadEndDocument(); reader.ReadBsonType(); datapoints[key] = dp; } reader.ReadEndDocument(); m.Data = datapoints; }
/// <summary> /// Deserializes an object from a binary source. /// </summary> /// <param name="Reader">Binary deserializer.</param> /// <param name="DataType">Optional datatype. If not provided, will be read from the binary source.</param> /// <param name="Embedded">If the object is embedded into another.</param> /// <returns>Deserialized object.</returns> public override object Deserialize(IBsonReader Reader, BsonType?DataType, bool Embedded) { if (!DataType.HasValue) { DataType = Reader.ReadBsonType(); } switch (DataType.Value) { case BsonType.DateTime: return((DateTimeOffset?)ObjectSerializer.UnixEpoch.AddMilliseconds(Reader.ReadDateTime())); case BsonType.String: return((DateTimeOffset?)DateTimeOffset.Parse(Reader.ReadString())); case BsonType.Document: DateTime TP = DateTime.MinValue; TimeSpan TZ = TimeSpan.Zero; Reader.ReadStartDocument(); while (Reader.State == BsonReaderState.Type) { BsonType BsonType = Reader.ReadBsonType(); if (BsonType == BsonType.EndOfDocument) { break; } string FieldName = Reader.ReadName(); switch (FieldName) { case "tp": TP = ObjectSerializer.UnixEpoch.AddMilliseconds(Reader.ReadDateTime()); break; case "tz": TZ = TimeSpan.Parse(Reader.ReadString()); break; } } Reader.ReadEndDocument(); return((DateTimeOffset?)new DateTimeOffset(TP, TZ)); case BsonType.MinKey: Reader.ReadMinKey(); return((DateTimeOffset?)DateTimeOffset.MinValue); case BsonType.MaxKey: Reader.ReadMaxKey(); return((DateTimeOffset?)DateTimeOffset.MaxValue); case BsonType.Null: Reader.ReadNull(); return(null); default: throw new Exception("Expected a nullable DateTimeOffset value."); } }
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 TestDocumentEmpty() { var json = "{ }"; using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType()); _bsonReader.ReadStartDocument(); Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType()); _bsonReader.ReadEndDocument(); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } Assert.Equal(json, BsonSerializer.Deserialize <BsonDocument>(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()); }
/// <summary> /// Deserializes an object from a binary source. /// </summary> /// <param name="Reader">Binary deserializer.</param> /// <param name="DataType">Optional datatype. If not provided, will be read from the binary source.</param> /// <param name="Embedded">If the object is embedded into another.</param> /// <returns>Deserialized object.</returns> public override object Deserialize(IBsonReader Reader, BsonType?DataType, bool Embedded) { if (!DataType.HasValue) { DataType = Reader.ReadBsonType(); } switch (DataType.Value) { case BsonType.Boolean: return(Reader.ReadBoolean()); case BsonType.Decimal128: return(Reader.ReadDecimal128() != 0); case BsonType.Double: return(Reader.ReadDouble() != 0); case BsonType.Int32: return(Reader.ReadInt32() != 0); case BsonType.Int64: return(Reader.ReadInt64() != 0); case BsonType.MinKey: Reader.ReadMinKey(); return(false); case BsonType.MaxKey: Reader.ReadMaxKey(); return(true); case BsonType.Null: Reader.ReadNull(); return(null); default: throw new Exception("Expected a boolean value."); } }
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()); }
/// <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."); } }
/// <inheritdoc/> public Type GetActualType(IBsonReader bsonReader, Type nominalType) { ThrowIfNominalTypeIsIncorrect(nominalType); var bookmark = bsonReader.GetBookmark(); bsonReader.ReadStartDocument(); ObjectId id = default; while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var fieldName = bsonReader.ReadName(); if (fieldName == ElementName) { var partitioned = bsonReader.ReadBoolean(); bsonReader.ReturnToBookmark(bookmark); return(partitioned ? typeof(Partitioned.PartitionedStreamProcessorState) : typeof(StreamProcessorState)); } else if (fieldName == "_id") { id = bsonReader.ReadObjectId(); } else { bsonReader.SkipValue(); } } bsonReader.ReturnToBookmark(bookmark); throw new StreamProcessorStateDocumentIsMissingPartitionedField(id); }
/// <summary> /// Deserializes an object from a binary source. /// </summary> /// <param name="Reader">Binary deserializer.</param> /// <param name="DataType">Optional datatype. If not provided, will be read from the binary source.</param> /// <param name="Embedded">If the object is embedded into another.</param> /// <returns>Deserialized object.</returns> public override object Deserialize(IBsonReader Reader, BsonType?DataType, bool Embedded) { if (!DataType.HasValue) { DataType = Reader.ReadBsonType(); } switch (DataType.Value) { case BsonType.Boolean: return(this.ToNullable(Enum.ToObject(this.enumType, Reader.ReadBoolean() ? 1 : 0))); case BsonType.Decimal128: return(this.ToNullable(Enum.ToObject(this.enumType, (int)Reader.ReadDecimal128()))); case BsonType.Double: return(this.ToNullable(Enum.ToObject(this.enumType, Reader.ReadDouble()))); case BsonType.Int32: return(this.ToNullable(Enum.ToObject(this.enumType, Reader.ReadInt32()))); case BsonType.Int64: return(this.ToNullable(Enum.ToObject(this.enumType, Reader.ReadInt64()))); case BsonType.String: return(this.ToNullable(Enum.Parse(this.enumType, Reader.ReadString()))); case BsonType.Null: Reader.ReadNull(); return(null); default: throw new Exception("Expected an enum value."); } }
private JObject GenerateJObject(IBsonReader reader, JToken parent) { var jobject = new JObject(); reader.ReadStartDocument(); string propertyName = null; do { if (reader.State == BsonReaderState.Type) { reader.ReadBsonType(); } if (reader.State == BsonReaderState.Name) { propertyName = reader.ReadName(); } if (reader.State == BsonReaderState.Value) { var value = GenerateJToken(reader, jobject); jobject.Add(propertyName, value); } } while (reader.State != BsonReaderState.EndOfDocument); reader.ReadEndDocument(); return(jobject); }
/// <summary> /// Deserializes an object from a binary source. /// </summary> /// <param name="Reader">Binary deserializer.</param> /// <param name="DataType">Optional datatype. If not provided, will be read from the binary source.</param> /// <param name="Embedded">If the object is embedded into another.</param> /// <returns>Deserialized object.</returns> public override object Deserialize(IBsonReader Reader, BsonType?DataType, bool Embedded) { if (!DataType.HasValue) { DataType = Reader.ReadBsonType(); } switch (DataType.Value) { case BsonType.Decimal128: return((char?)Reader.ReadDecimal128()); case BsonType.Double: return((char?)Reader.ReadDouble()); case BsonType.Int32: return((char?)Reader.ReadInt32()); case BsonType.Int64: return((char?)Reader.ReadInt64()); case BsonType.MinKey: Reader.ReadMinKey(); return((char?)char.MinValue); case BsonType.MaxKey: Reader.ReadMaxKey(); return((char?)char.MaxValue); case BsonType.Null: Reader.ReadNull(); return(null); case BsonType.String: string s = Reader.ReadString(); return((char?)(string.IsNullOrEmpty(s) ? (char?)0 : s[0])); default: throw new Exception("Expected a nullable char value."); } }
// private methods private CollectionNamespace DeserializeCollectionNamespace(IBsonReader reader) { string collectionName = null; string databaseName = null; reader.ReadStartDocument(); while (reader.ReadBsonType() != 0) { var fieldName = reader.ReadName(); switch (fieldName) { case "db": databaseName = reader.ReadString(); break; case "coll": collectionName = reader.ReadString(); break; default: throw new FormatException($"Invalid field name: \"{fieldName}\"."); } } reader.ReadEndDocument(); var databaseNamespace = new DatabaseNamespace(databaseName); return(new CollectionNamespace(databaseNamespace, collectionName)); }
/// <summary> /// Positions the reader to a string element by name. /// </summary> /// <param name="reader">The reader.</param> /// <param name="name">The name of the element.</param> /// <returns>True if the element was found.</returns> public static string FindStringElement(this IBsonReader reader, string name) { BsonType bsonType; while ((bsonType = reader.ReadBsonType()) != BsonType.EndOfDocument) { if (bsonType == BsonType.String) { var elementName = reader.ReadName(); if (elementName == name) { return(reader.ReadString()); } else { reader.SkipValue(); } } else { reader.SkipName(); reader.SkipValue(); } } return(null); }
public Type GetActualType(IBsonReader bsonReader, Type nominalType) { var bookmark = bsonReader.GetBookmark(); bsonReader.ReadStartDocument(); var actualType = nominalType; while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var name = bsonReader.ReadName(); if (name == "OnlyInB") { actualType = typeof(B); break; } else if (name == "OnlyInC") { actualType = typeof(C); break; } bsonReader.SkipValue(); } bsonReader.ReturnToBookmark(bookmark); return(actualType); }
/// <summary> /// Deserialises a value from key/value pairs. /// </summary> /// <param name="context">The deserialisation context.</param> /// <param name="args">The deserialisation arguments.</param> /// <returns> /// A deserialised value. /// </returns> public override TStruct Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) { //boxing is required for SetValue to work var obj = (object)(new TStruct()); Type actualType = args.NominalType; IBsonReader bsonReader = context.Reader; bsonReader.ReadStartDocument(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var name = bsonReader.ReadName(); var field = actualType.GetField(name); if (field != null) { var value = BsonSerializer.Deserialize(bsonReader, field.FieldType); field.SetValue(obj, value); } var prop = actualType.GetProperty(name); if (prop != null) { var value = BsonSerializer.Deserialize(bsonReader, prop.PropertyType); prop.SetValue(obj, value, null); } } bsonReader.ReadEndDocument(); return((TStruct)obj); }
/// <summary> /// Deserializes an object from a binary source. /// </summary> /// <param name="Reader">Binary deserializer.</param> /// <param name="DataType">Optional datatype. If not provided, will be read from the binary source.</param> /// <param name="Embedded">If the object is embedded into another.</param> /// <returns>Deserialized object.</returns> public override object Deserialize(IBsonReader Reader, BsonType?DataType, bool Embedded) { if (!DataType.HasValue) { DataType = Reader.ReadBsonType(); } switch (DataType.Value) { case BsonType.Boolean: return(Reader.ReadBoolean() ? (byte?)1 : (byte?)0); case BsonType.Decimal128: return((byte?)Reader.ReadDecimal128()); case BsonType.Double: return((byte?)Reader.ReadDouble()); case BsonType.Int32: return((byte?)Reader.ReadInt32()); case BsonType.Int64: return((byte?)Reader.ReadInt64()); case BsonType.String: return((byte?)byte.Parse(Reader.ReadString())); case BsonType.MinKey: Reader.ReadMinKey(); return((byte?)byte.MinValue); case BsonType.MaxKey: Reader.ReadMaxKey(); return((byte?)byte.MaxValue); case BsonType.Null: Reader.ReadNull(); return(null); default: throw new Exception("Expected a nullable byte value."); } }
public override TValue Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) { //boxing is required for SetValue to work object obj = new TValue(); Type actualType = args.NominalType; IBsonReader bsonReader = context.Reader; bsonReader.ReadStartDocument(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { string name = bsonReader.ReadName(Utf8NameDecoder.Instance); FieldInfo field = actualType.GetField(name); if (field != null) { object value = BsonSerializer.Deserialize(bsonReader, field.FieldType); field.SetValue(obj, value); } } bsonReader.ReadEndDocument(); return((TValue)obj); }
/// <summary> /// 安全地判断是否读到数组结尾 /// </summary> /// <param name="bsonReader">Bson读取器</param> /// <returns>是否读到数组结尾</returns> private static bool IsEndOfArray(this IBsonReader bsonReader) { if (bsonReader.State == BsonReaderState.Type) { bsonReader.ReadBsonType(); } return(bsonReader.State == BsonReaderState.EndOfArray); }
public void TestDocumentOneElement() { var json = "{ \"x\" : 1 }"; using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType()); _bsonReader.ReadStartDocument(); Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType()); Assert.Equal("x", _bsonReader.ReadName()); Assert.Equal(1, _bsonReader.ReadInt32()); Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType()); _bsonReader.ReadEndDocument(); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } Assert.Equal(json, BsonSerializer.Deserialize <BsonDocument>(json).ToJson()); }
public void TestJavaScriptWithScope() { string json = "{ \"$code\" : \"function f() { return n; }\", \"$scope\" : { \"n\" : 1 } }"; using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.JavaScriptWithScope, _bsonReader.ReadBsonType()); Assert.Equal("function f() { return n; }", _bsonReader.ReadJavaScriptWithScope()); _bsonReader.ReadStartDocument(); Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType()); Assert.Equal("n", _bsonReader.ReadName()); Assert.Equal(1, _bsonReader.ReadInt32()); _bsonReader.ReadEndDocument(); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } Assert.Equal(json, BsonSerializer.Deserialize <BsonJavaScriptWithScope>(json).ToJson()); }
/// <summary> /// Deserializes an object from a binary source. /// </summary> /// <param name="Reader">Binary deserializer.</param> /// <param name="DataType">Optional datatype. If not provided, will be read from the binary source.</param> /// <param name="Embedded">If the object is embedded into another.</param> /// <returns>Deserialized object.</returns> public override object Deserialize(IBsonReader Reader, BsonType?DataType, bool Embedded) { if (!DataType.HasValue) { DataType = Reader.ReadBsonType(); } return(ReadArray <T>(this.provider, Reader, DataType.Value)); }
public void TestDecimal128ExtendedJson(string json, string expectedValueString, string expectedJson) { using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.Decimal128, _bsonReader.ReadBsonType()); Assert.Equal(Decimal128.Parse(expectedValueString), _bsonReader.ReadDecimal128()); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } Assert.Equal(expectedJson, BsonSerializer.Deserialize <BsonDecimal128>(json).ToJson()); }
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 void TestString() { var json = "\"abc\""; using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.String, _bsonReader.ReadBsonType()); Assert.Equal("abc", _bsonReader.ReadString()); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } Assert.Equal(json, BsonSerializer.Deserialize <string>(json).ToJson()); }
public void TestInt64ConstructorUnqutoed() { var json = "NumberLong(123)"; using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.Int64, _bsonReader.ReadBsonType()); Assert.Equal(123, _bsonReader.ReadInt64()); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } Assert.Equal(json, BsonSerializer.Deserialize <long>(json).ToJson()); }
public void TestJavaScript() { string json = "{ \"$code\" : \"function f() { return 1; }\" }"; using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.JavaScript, _bsonReader.ReadBsonType()); Assert.Equal("function f() { return 1; }", _bsonReader.ReadJavaScript()); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } Assert.Equal(json, BsonSerializer.Deserialize <BsonJavaScript>(json).ToJson()); }
public void TestNull() { var json = "null"; using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.Null, _bsonReader.ReadBsonType()); _bsonReader.ReadNull(); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } Assert.Equal(json, BsonSerializer.Deserialize <BsonNull>(json).ToJson()); }
public void TestMinKeyKeyword() { var json = "MinKey"; using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.MinKey, _bsonReader.ReadBsonType()); _bsonReader.ReadMinKey(); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } Assert.Equal(json, BsonSerializer.Deserialize <BsonMinKey>(new StringReader(json)).ToJson()); }
public void TestInt32Constructor(string json) { using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType()); Assert.Equal(123, _bsonReader.ReadInt32()); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } var canonicalJson = "123"; Assert.Equal(canonicalJson, BsonSerializer.Deserialize <int>(new StringReader(json)).ToJson()); }
public Type GetActualType(IBsonReader bsonReader, Type nominalType) { var bookmark = bsonReader.GetBookmark(); bsonReader.ReadStartDocument(); var actualType = nominalType; while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var name = bsonReader.ReadName(); if (name == "OnlyInB") { actualType = typeof(B); break; } else if (name == "OnlyInC") { actualType = typeof(C); break; } bsonReader.SkipValue(); } bsonReader.ReturnToBookmark(bookmark); return actualType; }
public void TestDateTimeMaxBson() { var json = "new Date(9223372036854775807)"; using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.DateTime, _bsonReader.ReadBsonType()); Assert.Equal(9223372036854775807, _bsonReader.ReadDateTime()); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } Assert.Equal(json, BsonSerializer.Deserialize<BsonDateTime>(json).ToJson()); }
public void TestGuid() { var guid = new Guid("B5F21E0C2A0D42D6AD03D827008D8AB6"); var json = "CSUUID(\"B5F21E0C2A0D42D6AD03D827008D8AB6\")"; using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.Binary, _bsonReader.ReadBsonType()); var binaryData = _bsonReader.ReadBinaryData(); Assert.True(binaryData.Bytes.SequenceEqual(guid.ToByteArray())); Assert.Equal(BsonBinarySubType.UuidLegacy, binaryData.SubType); Assert.Equal(GuidRepresentation.CSharpLegacy, binaryData.GuidRepresentation); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } var expected = "CSUUID(\"b5f21e0c-2a0d-42d6-ad03-d827008d8ab6\")"; Assert.Equal(expected, BsonSerializer.Deserialize<Guid>(json).ToJson()); }
public void TestDouble() { var json = "1.5"; using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.Double, _bsonReader.ReadBsonType()); Assert.Equal(1.5, _bsonReader.ReadDouble()); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } Assert.Equal(json, BsonSerializer.Deserialize<double>(json).ToJson()); }
public void TestInt32() { var json = "123"; using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType()); Assert.Equal(123, _bsonReader.ReadInt32()); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } Assert.Equal(json, BsonSerializer.Deserialize<int>(json).ToJson()); }
public void TestHexData() { var expectedBytes = new byte[] { 0x01, 0x23 }; var json = "HexData(0, \"123\")"; using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.Binary, _bsonReader.ReadBsonType()); var bytes = _bsonReader.ReadBytes(); Assert.True(expectedBytes.SequenceEqual(bytes)); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } var expectedJson = "new BinData(0, \"ASM=\")"; Assert.Equal(expectedJson, BsonSerializer.Deserialize<byte[]>(json).ToJson()); }
public void TestRegularExpressionStrict() { var json = "{ \"$regex\" : \"pattern\", \"$options\" : \"imxs\" }"; using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.RegularExpression, _bsonReader.ReadBsonType()); var regex = _bsonReader.ReadRegularExpression(); Assert.Equal("pattern", regex.Pattern); Assert.Equal("imxs", regex.Options); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } var settings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict }; Assert.Equal(json, BsonSerializer.Deserialize<BsonRegularExpression>(json).ToJson(settings)); }
public void TestInt32Constructor(string json) { using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType()); Assert.Equal(123, _bsonReader.ReadInt32()); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } var canonicalJson = "123"; Assert.Equal(canonicalJson, BsonSerializer.Deserialize<int>(new StringReader(json)).ToJson()); }
public void TestNull() { var json = "null"; using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.Null, _bsonReader.ReadBsonType()); _bsonReader.ReadNull(); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } Assert.Equal(json, BsonSerializer.Deserialize<BsonNull>(json).ToJson()); }
public void TestNestedDocument() { var json = "{ \"a\" : { \"b\" : 1, \"c\" : 2 } }"; using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType()); _bsonReader.ReadStartDocument(); Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType()); Assert.Equal("a", _bsonReader.ReadName()); _bsonReader.ReadStartDocument(); Assert.Equal("b", _bsonReader.ReadName()); Assert.Equal(1, _bsonReader.ReadInt32()); Assert.Equal("c", _bsonReader.ReadName()); Assert.Equal(2, _bsonReader.ReadInt32()); _bsonReader.ReadEndDocument(); _bsonReader.ReadEndDocument(); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } Assert.Equal(json, BsonSerializer.Deserialize<BsonDocument>(json).ToJson()); }
public void TestTimestampExtendedJsonNewRepresentation() { var json = "{ \"$timestamp\" : { \"t\" : 1, \"i\" : 2 } }"; using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.Timestamp, _bsonReader.ReadBsonType()); Assert.Equal(new BsonTimestamp(1, 2).Value, _bsonReader.ReadTimestamp()); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } var canonicalJson = "Timestamp(1, 2)"; Assert.Equal(canonicalJson, BsonSerializer.Deserialize<BsonTimestamp>(new StringReader(json)).ToJson()); }
public void TestDateTimeShell(string json, long expectedResult, string canonicalJson) { using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.DateTime, _bsonReader.ReadBsonType()); Assert.Equal(expectedResult, _bsonReader.ReadDateTime()); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } var jsonSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Shell }; Assert.Equal(canonicalJson, BsonSerializer.Deserialize<DateTime>(json).ToJson(jsonSettings)); }
public void TestDecimal128ExtendedJson(string json, string expectedValueString, string expectedJson) { using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.Decimal128, _bsonReader.ReadBsonType()); Assert.Equal(Decimal128.Parse(expectedValueString), _bsonReader.ReadDecimal128()); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } Assert.Equal(expectedJson, BsonSerializer.Deserialize<BsonDecimal128>(json).ToJson()); }
public void TestJavaScriptWithScope() { string json = "{ \"$code\" : \"function f() { return n; }\", \"$scope\" : { \"n\" : 1 } }"; using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.JavaScriptWithScope, _bsonReader.ReadBsonType()); Assert.Equal("function f() { return n; }", _bsonReader.ReadJavaScriptWithScope()); _bsonReader.ReadStartDocument(); Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType()); Assert.Equal("n", _bsonReader.ReadName()); Assert.Equal(1, _bsonReader.ReadInt32()); _bsonReader.ReadEndDocument(); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } Assert.Equal(json, BsonSerializer.Deserialize<BsonJavaScriptWithScope>(json).ToJson()); }
public void TestObjectIdStrict() { var json = "{ \"$oid\" : \"4d0ce088e447ad08b4721a37\" }"; using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.ObjectId, _bsonReader.ReadBsonType()); var objectId = _bsonReader.ReadObjectId(); Assert.Equal("4d0ce088e447ad08b4721a37", objectId.ToString()); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } var jsonSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict }; Assert.Equal(json, BsonSerializer.Deserialize<ObjectId>(json).ToJson(jsonSettings)); }
public void TestDocumentTwoElements() { var json = "{ \"x\" : 1, \"y\" : 2 }"; using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType()); _bsonReader.ReadStartDocument(); Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType()); Assert.Equal("x", _bsonReader.ReadName()); Assert.Equal(1, _bsonReader.ReadInt32()); Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType()); Assert.Equal("y", _bsonReader.ReadName()); Assert.Equal(2, _bsonReader.ReadInt32()); Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType()); _bsonReader.ReadEndDocument(); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } Assert.Equal(json, BsonSerializer.Deserialize<BsonDocument>(json).ToJson()); }
public void TestMinKeyKeyword() { var json = "MinKey"; using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.MinKey, _bsonReader.ReadBsonType()); _bsonReader.ReadMinKey(); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } Assert.Equal(json, BsonSerializer.Deserialize<BsonMinKey>(new StringReader(json)).ToJson()); }
public void TestJavaScript() { string json = "{ \"$code\" : \"function f() { return 1; }\" }"; using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.JavaScript, _bsonReader.ReadBsonType()); Assert.Equal("function f() { return 1; }", _bsonReader.ReadJavaScript()); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } Assert.Equal(json, BsonSerializer.Deserialize<BsonJavaScript>(json).ToJson()); }
public void TestRegularExpressionShell() { var json = "/pattern/imxs"; using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.RegularExpression, _bsonReader.ReadBsonType()); var regex = _bsonReader.ReadRegularExpression(); Assert.Equal("pattern", regex.Pattern); Assert.Equal("imxs", regex.Options); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } Assert.Equal(json, BsonSerializer.Deserialize<BsonRegularExpression>(json).ToJson()); }
public void TestDateTimeStrict() { var json = "{ \"$date\" : 0 }"; using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.DateTime, _bsonReader.ReadBsonType()); Assert.Equal(0, _bsonReader.ReadDateTime()); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } var jsonSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict }; Assert.Equal(json, BsonSerializer.Deserialize<DateTime>(json).ToJson(jsonSettings)); }
public void TestInt64ExtendedJson() { var json = "{ \"$numberLong\" : \"123\" }"; using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.Int64, _bsonReader.ReadBsonType()); Assert.Equal(123, _bsonReader.ReadInt64()); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } var canonicalJson = "NumberLong(123)"; Assert.Equal(canonicalJson, BsonSerializer.Deserialize<long>(new StringReader(json)).ToJson()); }
public void TestDocumentEmpty() { var json = "{ }"; using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType()); _bsonReader.ReadStartDocument(); Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType()); _bsonReader.ReadEndDocument(); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } Assert.Equal(json, BsonSerializer.Deserialize<BsonDocument>(json).ToJson()); }
public void TestInt64ConstructorUnqutoed() { var json = "NumberLong(123)"; using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.Int64, _bsonReader.ReadBsonType()); Assert.Equal(123, _bsonReader.ReadInt64()); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } Assert.Equal(json, BsonSerializer.Deserialize<long>(json).ToJson()); }
public void TestStringEmpty() { var json = "\"\""; using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.String, _bsonReader.ReadBsonType()); Assert.Equal("", _bsonReader.ReadString()); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } Assert.Equal(json, BsonSerializer.Deserialize<string>(json).ToJson()); }
public void TestMinKeyExtendedJsonWithCapitalK() { var json = "{ \"$minKey\" : 1 }"; using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.MinKey, _bsonReader.ReadBsonType()); _bsonReader.ReadMinKey(); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } var canonicalJson = "MinKey"; Assert.Equal(canonicalJson, BsonSerializer.Deserialize<BsonMinKey>(new StringReader(json)).ToJson()); }
public void TestSymbol() { var json = "{ \"$symbol\" : \"symbol\" }"; using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.Symbol, _bsonReader.ReadBsonType()); Assert.Equal("symbol", _bsonReader.ReadSymbol()); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } Assert.Equal(json, BsonSerializer.Deserialize<BsonSymbol>(json).ToJson()); }
public void TestTimestampConstructor() { var json = "Timestamp(1, 2)"; using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.Timestamp, _bsonReader.ReadBsonType()); Assert.Equal(new BsonTimestamp(1, 2).Value, _bsonReader.ReadTimestamp()); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } Assert.Equal(json, BsonSerializer.Deserialize<BsonTimestamp>(new StringReader(json)).ToJson()); }
public void TestObjectIdShell() { var json = "ObjectId(\"4d0ce088e447ad08b4721a37\")"; using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.ObjectId, _bsonReader.ReadBsonType()); var objectId = _bsonReader.ReadObjectId(); Assert.Equal("4d0ce088e447ad08b4721a37", objectId.ToString()); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } Assert.Equal(json, BsonSerializer.Deserialize<ObjectId>(json).ToJson()); }
public void TestDateTimeStrictIso8601() { var json = "{ \"$date\" : \"1970-01-01T00:00:00Z\" }"; using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.DateTime, _bsonReader.ReadBsonType()); Assert.Equal(0, _bsonReader.ReadDateTime()); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } var expected = "{ \"$date\" : 0 }"; // it's still not ISO8601 on the way out var jsonSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict }; Assert.Equal(expected, BsonSerializer.Deserialize<DateTime>(json).ToJson(jsonSettings)); }