public void TestBsonJavaScriptWithScopeEquals() { BsonJavaScriptWithScope lhs = new BsonJavaScriptWithScope("n = 1", new BsonDocument { { "x", "2" } }); BsonJavaScriptWithScope rhs = new BsonJavaScriptWithScope("n = 1", new BsonDocument { { "x", "2" } }); Assert.AreNotSame(lhs, rhs); Assert.AreEqual(lhs, rhs); Assert.AreEqual(lhs.GetHashCode(), rhs.GetHashCode()); }
public void TestMapJavaScriptWithScope() { var value = new BsonJavaScriptWithScope("code", new BsonDocument()); var bsonValue = (BsonJavaScriptWithScope)BsonTypeMapper.MapToBsonValue(value); Assert.AreSame(value, bsonValue); var bsonJavaScriptWithScope = (BsonJavaScriptWithScope)BsonTypeMapper.MapToBsonValue(value, BsonType.JavaScriptWithScope); Assert.AreSame(value, bsonJavaScriptWithScope); }
public void TestAsBsonJavaScriptWithScode() { var scope = new BsonDocument("x", 1); BsonValue s = ""; BsonValue v = new BsonJavaScriptWithScope("code", scope); var js = v.AsBsonJavaScriptWithScope; Assert.AreEqual("code", js.Code); Assert.AreEqual(1, js.Scope.ElementCount); Assert.AreEqual("name", js.Scope.GetElement(0).Name); Assert.AreEqual(1, js.Scope["name"].AsInt32); Assert.AreSame(v.AsBsonJavaScript, v.AsBsonJavaScriptWithScope); Assert.Throws<InvalidCastException>(() => { var x = s.AsBsonJavaScriptWithScope; }); }
public void TestBsonJavaScriptWithScopeEquals() { BsonJavaScriptWithScope lhs = new BsonJavaScriptWithScope("n = 1", new BsonDocument { { "x", "2" } }); BsonJavaScriptWithScope rhs = new BsonJavaScriptWithScope("n = 1", new BsonDocument { { "x", "2" } }); Assert.NotSame(lhs, rhs); Assert.Equal(lhs, rhs); Assert.Equal(lhs.GetHashCode(), rhs.GetHashCode()); }
public void TestAsBsonJavaScriptWithScode() { var scope = new BsonDocument("x", 1); BsonValue s = ""; BsonValue v = new BsonJavaScriptWithScope("code", scope); var js = v.AsBsonJavaScriptWithScope; Assert.AreEqual("code", js.Code); Assert.AreEqual(1, js.Scope.ElementCount); Assert.AreEqual("x", js.Scope.GetElement(0).Name); Assert.AreEqual(1, js.Scope["x"].AsInt32); Assert.AreSame(v.AsBsonJavaScript, v.AsBsonJavaScriptWithScope); Assert.Throws <InvalidCastException>(() => { var x = s.AsBsonJavaScriptWithScope; }); }
public void TestBsonJavaScriptWithScope() { var scope = new BsonDocument(); var value = new BsonJavaScriptWithScope("code", scope); Assert.Throws <InvalidCastException>(() => Convert.ToBoolean(value)); Assert.Throws <InvalidCastException>(() => Convert.ToByte(value)); Assert.Throws <InvalidCastException>(() => Convert.ToChar(value)); Assert.Throws <InvalidCastException>(() => Convert.ToDateTime(value)); Assert.Throws <InvalidCastException>(() => Convert.ToDecimal(value)); Assert.Throws <InvalidCastException>(() => Convert.ToDouble(value)); Assert.Throws <InvalidCastException>(() => Convert.ToInt16(value)); Assert.Throws <InvalidCastException>(() => Convert.ToInt32(value)); Assert.Throws <InvalidCastException>(() => Convert.ToInt64(value)); Assert.Throws <InvalidCastException>(() => Convert.ToSByte(value)); Assert.Throws <InvalidCastException>(() => Convert.ToSingle(value)); Assert.Throws <InvalidCastException>(() => Convert.ToString(value)); Assert.Throws <InvalidCastException>(() => Convert.ToUInt16(value)); Assert.Throws <InvalidCastException>(() => Convert.ToUInt32(value)); Assert.Throws <InvalidCastException>(() => Convert.ToUInt64(value)); }
public void TestBsonJavaScriptWithScopeEquals() { var a = new BsonJavaScriptWithScope("script 1", new BsonDocument("x", 1)); var b = new BsonJavaScriptWithScope("script 1", new BsonDocument("x", 1)); var c = new BsonJavaScriptWithScope("script 2", new BsonDocument("x", 1)); var d = new BsonJavaScriptWithScope("script 2", new BsonDocument("x", 2)); var n = (BsonJavaScript)null; Assert.IsTrue(object.Equals(a, b)); Assert.IsFalse(object.Equals(a, c)); Assert.IsFalse(object.Equals(a, d)); Assert.IsFalse(object.Equals(c, d)); Assert.IsFalse(object.Equals(a, BsonNull.Value)); Assert.IsFalse(a.Equals(n)); Assert.IsFalse(a.Equals(null)); Assert.IsTrue(a == b); Assert.IsFalse(a == c); Assert.IsFalse(b == d); Assert.IsFalse(c == d); Assert.IsFalse(a == BsonNull.Value); Assert.IsFalse(a == null); Assert.IsFalse(null == a); Assert.IsTrue(n == null); Assert.IsTrue(null == n); Assert.IsFalse(a != b); Assert.IsTrue(a != c); Assert.IsTrue(b != d); Assert.IsTrue(c != d); Assert.IsTrue(a != BsonNull.Value); Assert.IsTrue(a != null); Assert.IsTrue(null != a); Assert.IsFalse(n != null); Assert.IsFalse(null != n); }
public TestClass( BsonJavaScriptWithScope value ) { this.B = value; this.V = value; }
private static BsonJavaScriptWithScope ConvertJavaScriptWithScope( BsonJavaScriptWithScope script ) { return new BsonJavaScriptWithScope(script.Code, ConvertDocument(script.Scope)); }
// private methods private void ReadValue() { object jsonDotNetValue; switch (_wrappedReader.GetCurrentBsonType()) { case BsonType.Array: _wrappedReader.ReadStartArray(); SetCurrentToken(Newtonsoft.Json.JsonToken.StartArray); return; case BsonType.Binary: var bsonBinaryData = _wrappedReader.ReadBinaryData(); switch (bsonBinaryData.SubType) { case BsonBinarySubType.UuidLegacy: var guidRepresentation = GuidRepresentation.Unspecified; var bsonReader = _wrappedReader as BsonReader; if (bsonReader != null) { guidRepresentation = bsonReader.Settings.GuidRepresentation; } jsonDotNetValue = GuidConverter.FromBytes(bsonBinaryData.Bytes, guidRepresentation); break; case BsonBinarySubType.UuidStandard: jsonDotNetValue = GuidConverter.FromBytes(bsonBinaryData.Bytes, GuidRepresentation.Standard); break; default: jsonDotNetValue = bsonBinaryData.Bytes; break; } SetCurrentToken(Newtonsoft.Json.JsonToken.Bytes, jsonDotNetValue, bsonBinaryData); return; case BsonType.Boolean: var booleanValue = _wrappedReader.ReadBoolean(); SetCurrentToken(Newtonsoft.Json.JsonToken.Boolean, booleanValue, (BsonBoolean)booleanValue); return; case BsonType.DateTime: var bsonDateTime = new BsonDateTime(_wrappedReader.ReadDateTime()); if (bsonDateTime.IsValidDateTime) { jsonDotNetValue = bsonDateTime.ToUniversalTime(); } else { jsonDotNetValue = bsonDateTime.MillisecondsSinceEpoch; } SetCurrentToken(Newtonsoft.Json.JsonToken.Date, jsonDotNetValue, bsonDateTime); return; case BsonType.Document: _wrappedReader.ReadStartDocument(); SetCurrentToken(Newtonsoft.Json.JsonToken.StartObject); return; case BsonType.Double: var bsonDouble = new BsonDouble(_wrappedReader.ReadDouble()); switch (FloatParseHandling) { case Newtonsoft.Json.FloatParseHandling.Decimal: jsonDotNetValue = Convert.ToDecimal(bsonDouble); break; case Newtonsoft.Json.FloatParseHandling.Double: jsonDotNetValue = bsonDouble.Value; break; default: throw new NotSupportedException(string.Format("Unexpected FloatParseHandling value: {0}.", FloatParseHandling)); } SetCurrentToken(Newtonsoft.Json.JsonToken.Float, jsonDotNetValue, bsonDouble); return; case BsonType.Int32: var bsonInt32 = (BsonInt32)_wrappedReader.ReadInt32(); SetCurrentToken(Newtonsoft.Json.JsonToken.Integer, (long)bsonInt32.Value, bsonInt32); return; case BsonType.Int64: var bsonInt64 = (BsonInt64)_wrappedReader.ReadInt64(); SetCurrentToken(Newtonsoft.Json.JsonToken.Integer, bsonInt64.Value, bsonInt64); return; case BsonType.JavaScript: { var code = _wrappedReader.ReadJavaScript(); var bsonJavaScript = new BsonJavaScript(code); SetCurrentToken(Newtonsoft.Json.JsonToken.String, code, bsonJavaScript); } return; case BsonType.JavaScriptWithScope: { var code = _wrappedReader.ReadJavaScriptWithScope(); var context = BsonDeserializationContext.CreateRoot(_wrappedReader); var scope = BsonDocumentSerializer.Instance.Deserialize <BsonDocument>(context); var bsonJavaScriptWithScope = new BsonJavaScriptWithScope(code, scope); SetCurrentToken(Newtonsoft.Json.JsonToken.String, code, bsonJavaScriptWithScope); } return; case BsonType.MaxKey: _wrappedReader.ReadMaxKey(); SetCurrentToken(Newtonsoft.Json.JsonToken.Undefined, null, BsonMaxKey.Value); return; case BsonType.MinKey: _wrappedReader.ReadMinKey(); SetCurrentToken(Newtonsoft.Json.JsonToken.Undefined, null, BsonMinKey.Value); return; case BsonType.Null: _wrappedReader.ReadNull(); SetCurrentToken(Newtonsoft.Json.JsonToken.Null, null, BsonNull.Value); return; case BsonType.ObjectId: var bsonObjectId = new BsonObjectId(_wrappedReader.ReadObjectId()); SetCurrentToken(Newtonsoft.Json.JsonToken.Bytes, bsonObjectId.Value.ToByteArray(), bsonObjectId); return; case BsonType.RegularExpression: var bsonRegularExpression = _wrappedReader.ReadRegularExpression(); var pattern = bsonRegularExpression.Pattern; var options = bsonRegularExpression.Options; jsonDotNetValue = "/" + pattern.Replace("/", "\\/") + "/" + options; SetCurrentToken(Newtonsoft.Json.JsonToken.String, jsonDotNetValue, bsonRegularExpression); return; case BsonType.String: var stringValue = _wrappedReader.ReadString(); SetCurrentToken(Newtonsoft.Json.JsonToken.String, stringValue, (BsonString)stringValue); return; case BsonType.Symbol: var bsonSymbol = BsonSymbolTable.Lookup(_wrappedReader.ReadSymbol()); SetCurrentToken(Newtonsoft.Json.JsonToken.String, bsonSymbol.Name, bsonSymbol); return; case BsonType.Timestamp: var bsonTimestamp = new BsonTimestamp(_wrappedReader.ReadTimestamp()); SetCurrentToken(Newtonsoft.Json.JsonToken.Integer, bsonTimestamp.Value, bsonTimestamp); return; case BsonType.Undefined: _wrappedReader.ReadUndefined(); SetCurrentToken(Newtonsoft.Json.JsonToken.Undefined, null, BsonUndefined.Value); return; default: var message = string.Format("Unexpected BsonType: {0}.", _wrappedReader.GetCurrentBsonType()); throw new Newtonsoft.Json.JsonReaderException(message); } }
public void TestBsonJavaScriptWithScope() { var scope = new BsonDocument(); var value = new BsonJavaScriptWithScope("code", scope); Assert.AreSame(value, ((IConvertible)value).ToType(typeof(object), null)); Assert.Throws<InvalidCastException>(() => Convert.ToBoolean(value)); Assert.Throws<InvalidCastException>(() => Convert.ToByte(value)); Assert.Throws<InvalidCastException>(() => Convert.ToChar(value)); Assert.Throws<InvalidCastException>(() => Convert.ToDateTime(value)); Assert.Throws<InvalidCastException>(() => Convert.ToDecimal(value)); Assert.Throws<InvalidCastException>(() => Convert.ToDouble(value)); Assert.Throws<InvalidCastException>(() => Convert.ToInt16(value)); Assert.Throws<InvalidCastException>(() => Convert.ToInt32(value)); Assert.Throws<InvalidCastException>(() => Convert.ToInt64(value)); Assert.Throws<InvalidCastException>(() => Convert.ToSByte(value)); Assert.Throws<InvalidCastException>(() => Convert.ToSingle(value)); Assert.Throws<InvalidCastException>(() => Convert.ToString(value)); Assert.Throws<InvalidCastException>(() => Convert.ToUInt16(value)); Assert.Throws<InvalidCastException>(() => Convert.ToUInt32(value)); Assert.Throws<InvalidCastException>(() => Convert.ToUInt64(value)); }
private static BsonJavaScriptWithScope ConvertJavaScriptWithScope( BsonJavaScriptWithScope script ) { return(new BsonJavaScriptWithScope(script.Code, ConvertDocument(script.Scope))); }
private BsonJavaScriptWithScope RecursivelyMaterialize(BsonJavaScriptWithScope value) { return(new BsonJavaScriptWithScope(value.Code, RecursivelyMaterialize(value.Scope))); }
public void TestMapJavaScriptWithScope() { var value = new BsonJavaScriptWithScope("code", new BsonDocument()); var bsonValue = (BsonJavaScriptWithScope) BsonTypeMapper.MapToBsonValue(value); Assert.AreSame(value, bsonValue); var bsonJavaScriptWithScope = (BsonJavaScriptWithScope) BsonTypeMapper.MapToBsonValue(value, BsonType.JavaScriptWithScope); Assert.AreSame(value, bsonJavaScriptWithScope); }
public static BsonValue Create(this BsonType bsonType, object o) { BsonValue value = BsonNull.Value; try { switch (bsonType) { case BsonType.EndOfDocument: break; case BsonType.Double: value = BsonDouble.Create(o); break; case BsonType.String: value = BsonString.Create(o); break; case BsonType.Document: value = BsonDocument.Create(o); break; case BsonType.Array: value = BsonArray.Create(o); break; case BsonType.Binary: value = BsonBinaryData.Create(o); break; case BsonType.Undefined: break; case BsonType.ObjectId: value = BsonObjectId.Create(o); break; case BsonType.Boolean: value = BsonBoolean.Create(o); break; case BsonType.DateTime: value = BsonDateTime.Create(o); break; case BsonType.Null: value = BsonNull.Value; break; case BsonType.RegularExpression: value = BsonRegularExpression.Create(o); break; case BsonType.JavaScript: value = BsonJavaScript.Create(o); break; case BsonType.Symbol: value = BsonSymbol.Create(o); break; case BsonType.JavaScriptWithScope: value = BsonJavaScriptWithScope.Create(o); break; case BsonType.Int32: value = BsonInt32.Create(o); break; case BsonType.Timestamp: value = BsonTimestamp.Create(o); break; case BsonType.Int64: value = BsonInt64.Create(o); break; case BsonType.MaxKey: value = BsonValue.Create(o); break; case BsonType.MinKey: value = BsonValue.Create(o); break; } } catch { } return(value); }
public void DeserializeProperty( BsonReader bsonReader, object obj, BsonPropertyMap propertyMap ) { var bsonType = bsonReader.PeekBsonType(); BsonJavaScriptWithScope value; if (bsonType == BsonType.Null) { bsonReader.ReadNull(propertyMap.ElementName); value = null; } else { var code = bsonReader.ReadJavaScriptWithScope(propertyMap.ElementName); var scope = BsonDocument.ReadFrom(bsonReader); value = new BsonJavaScriptWithScope(code, scope); } propertyMap.Setter(obj, value); }
// private methods private void ReadValue() { object jsonDotNetValue; switch (_wrappedReader.GetCurrentBsonType()) { case BsonType.Array: _wrappedReader.ReadStartArray(); SetCurrentToken(Newtonsoft.Json.JsonToken.StartArray); return; case BsonType.Binary: var bsonBinaryData = _wrappedReader.ReadBinaryData(); switch (bsonBinaryData.SubType) { case BsonBinarySubType.UuidLegacy: var guidRepresentation = GuidRepresentation.Unspecified; var bsonReader = _wrappedReader as BsonReader; if (bsonReader != null) { guidRepresentation = bsonReader.Settings.GuidRepresentation; } jsonDotNetValue = GuidConverter.FromBytes(bsonBinaryData.Bytes, guidRepresentation); break; case BsonBinarySubType.UuidStandard: jsonDotNetValue = GuidConverter.FromBytes(bsonBinaryData.Bytes, GuidRepresentation.Standard); break; default: jsonDotNetValue = bsonBinaryData.Bytes; break; } SetCurrentToken(Newtonsoft.Json.JsonToken.Bytes, jsonDotNetValue, bsonBinaryData); return; case BsonType.Boolean: var booleanValue = _wrappedReader.ReadBoolean(); SetCurrentToken(Newtonsoft.Json.JsonToken.Boolean, booleanValue, (BsonBoolean)booleanValue); return; case BsonType.DateTime: var bsonDateTime = new BsonDateTime(_wrappedReader.ReadDateTime()); if (bsonDateTime.IsValidDateTime) { jsonDotNetValue = bsonDateTime.ToUniversalTime(); } else { jsonDotNetValue = bsonDateTime.MillisecondsSinceEpoch; } SetCurrentToken(Newtonsoft.Json.JsonToken.Date, jsonDotNetValue, bsonDateTime); return; case BsonType.Document: _wrappedReader.ReadStartDocument(); SetCurrentToken(Newtonsoft.Json.JsonToken.StartObject); return; case BsonType.Double: var bsonDouble = new BsonDouble(_wrappedReader.ReadDouble()); switch (FloatParseHandling) { case Newtonsoft.Json.FloatParseHandling.Decimal: jsonDotNetValue = Convert.ToDecimal(bsonDouble); break; case Newtonsoft.Json.FloatParseHandling.Double: jsonDotNetValue = bsonDouble.Value; break; default: throw new NotSupportedException(string.Format("Unexpected FloatParseHandling value: {0}.", FloatParseHandling)); } SetCurrentToken(Newtonsoft.Json.JsonToken.Float, jsonDotNetValue, bsonDouble); return; case BsonType.Int32: var bsonInt32 = (BsonInt32)_wrappedReader.ReadInt32(); SetCurrentToken(Newtonsoft.Json.JsonToken.Integer, (long)bsonInt32.Value, bsonInt32); return; case BsonType.Int64: var bsonInt64 = (BsonInt64)_wrappedReader.ReadInt64(); SetCurrentToken(Newtonsoft.Json.JsonToken.Integer, bsonInt64.Value, bsonInt64); return; case BsonType.JavaScript: { var code = _wrappedReader.ReadJavaScript(); var bsonJavaScript = new BsonJavaScript(code); SetCurrentToken(Newtonsoft.Json.JsonToken.String, code, bsonJavaScript); } return; case BsonType.JavaScriptWithScope: { var code = _wrappedReader.ReadJavaScriptWithScope(); var context = BsonDeserializationContext.CreateRoot(_wrappedReader); var scope = BsonDocumentSerializer.Instance.Deserialize<BsonDocument>(context); var bsonJavaScriptWithScope = new BsonJavaScriptWithScope(code, scope); SetCurrentToken(Newtonsoft.Json.JsonToken.String, code, bsonJavaScriptWithScope); } return; case BsonType.MaxKey: _wrappedReader.ReadMaxKey(); SetCurrentToken(Newtonsoft.Json.JsonToken.Undefined, null, BsonMaxKey.Value); return; case BsonType.MinKey: _wrappedReader.ReadMinKey(); SetCurrentToken(Newtonsoft.Json.JsonToken.Undefined, null, BsonMinKey.Value); return; case BsonType.Null: _wrappedReader.ReadNull(); SetCurrentToken(Newtonsoft.Json.JsonToken.Null, null, BsonNull.Value); return; case BsonType.ObjectId: var bsonObjectId = new BsonObjectId(_wrappedReader.ReadObjectId()); SetCurrentToken(Newtonsoft.Json.JsonToken.Bytes, bsonObjectId.Value.ToByteArray(), bsonObjectId); return; case BsonType.RegularExpression: var bsonRegularExpression = _wrappedReader.ReadRegularExpression(); var pattern = bsonRegularExpression.Pattern; var options = bsonRegularExpression.Options; jsonDotNetValue = "/" + pattern.Replace("/", "\\/") + "/" + options; SetCurrentToken(Newtonsoft.Json.JsonToken.String, jsonDotNetValue, bsonRegularExpression); return; case BsonType.String: var stringValue = _wrappedReader.ReadString(); SetCurrentToken(Newtonsoft.Json.JsonToken.String, stringValue, (BsonString)stringValue); return; case BsonType.Symbol: var bsonSymbol = BsonSymbolTable.Lookup(_wrappedReader.ReadSymbol()); SetCurrentToken(Newtonsoft.Json.JsonToken.String, bsonSymbol.Name, bsonSymbol); return; case BsonType.Timestamp: var bsonTimestamp = new BsonTimestamp(_wrappedReader.ReadTimestamp()); SetCurrentToken(Newtonsoft.Json.JsonToken.Integer, bsonTimestamp.Value, bsonTimestamp); return; case BsonType.Undefined: _wrappedReader.ReadUndefined(); SetCurrentToken(Newtonsoft.Json.JsonToken.Undefined, null, BsonUndefined.Value); return; default: var message = string.Format("Unexpected BsonType: {0}.", _wrappedReader.GetCurrentBsonType()); throw new Newtonsoft.Json.JsonReaderException(message); } }