public void TestDocumentNested() { var json = "{ \"a\" : { \"x\" : 1 }, \"y\" : 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(BsonType.Int32, _bsonReader.ReadBsonType()); Assert.Equal("x", _bsonReader.ReadName()); Assert.Equal(1, _bsonReader.ReadInt32()); Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType()); _bsonReader.ReadEndDocument(); 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()); }
/// <summary> /// Reads a single value. /// </summary> /// <param name="Reader">Binary reader.</param> /// <param name="FieldDataType">Field data type.</param> /// <returns>Single value.</returns> /// <exception cref="ArgumentException">If the <paramref name="FieldDataType"/> was invalid.</exception> public static float ReadSingle(IBsonReader Reader, BsonType FieldDataType) { switch (FieldDataType) { case BsonType.Boolean: return(Reader.ReadBoolean() ? (float)1 : (float)0); case BsonType.Decimal128: return((float)Reader.ReadDecimal128()); case BsonType.Double: return((float)Reader.ReadDouble()); case BsonType.Int32: return((float)Reader.ReadInt32()); case BsonType.Int64: return((float)Reader.ReadInt64()); case BsonType.String: return(float.Parse(Reader.ReadString())); case BsonType.MinKey: Reader.ReadMinKey(); return(float.MinValue); case BsonType.MaxKey: Reader.ReadMaxKey(); return(float.MaxValue); default: throw new ArgumentException("Expected a single value, but was a " + FieldDataType.ToString() + ".", nameof(FieldDataType)); } }
public override Amortization Deserialize(IBsonReader bsonReader) { string read = null; bsonReader.ReadStartDocument(); var amort = new Amortization { ID = bsonReader.ReadGuid("_id", ref read), User = bsonReader.ReadString("user", ref read), Name = bsonReader.ReadString("name", ref read), Value = bsonReader.ReadDouble("value", ref read), Date = bsonReader.ReadDateTime("date", ref read), TotalDays = bsonReader.ReadInt32("tday", ref read), }; amort.Interval = bsonReader.ReadString("interval", ref read) switch { "d" => AmortizeInterval.EveryDay, "w" => AmortizeInterval.SameDayOfWeek, "W" => AmortizeInterval.LastDayOfWeek, "m" => AmortizeInterval.SameDayOfMonth, "M" => AmortizeInterval.LastDayOfMonth, "y" => AmortizeInterval.SameDayOfYear, "Y" => AmortizeInterval.LastDayOfYear, _ => amort.Interval, }; amort.Template = bsonReader.ReadDocument("template", ref read, VoucherSerializer.Deserialize); amort.Schedule = bsonReader.ReadArray("schedule", ref read, ItemSerializer.Deserialize); amort.Remark = bsonReader.ReadString("remark", ref read); bsonReader.ReadEndDocument(); return(amort); }
static object ReadObject(IBsonReader bsonReader) //_120509_173140 keep consistent { switch (bsonReader.GetCurrentBsonType()) { case BsonType.Array: return(ReadArray(bsonReader)); // replacement case BsonType.Binary: var binary = BsonSerializer.Deserialize <BsonValue>(bsonReader); return(BsonTypeMapper.MapToDotNetValue(binary) ?? binary); // byte[] or Guid else self case BsonType.Boolean: return(bsonReader.ReadBoolean()); case BsonType.DateTime: return(BsonUtils.ToDateTimeFromMillisecondsSinceEpoch(bsonReader.ReadDateTime())); case BsonType.Document: return(ReadCustomObject(bsonReader)); // replacement case BsonType.Double: return(bsonReader.ReadDouble()); case BsonType.Int32: return(bsonReader.ReadInt32()); case BsonType.Int64: return(bsonReader.ReadInt64()); case BsonType.Null: bsonReader.ReadNull(); return(null); case BsonType.ObjectId: return(bsonReader.ReadObjectId()); case BsonType.String: return(bsonReader.ReadString()); default: return(BsonSerializer.Deserialize <BsonValue>(bsonReader)); } }
/// <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."); } }
/// <summary> /// Reads a string value. /// </summary> /// <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 string ReadString(IBsonReader Reader, BsonType FieldDataType) { switch (FieldDataType) { case BsonType.Boolean: return(Reader.ReadBoolean().ToString()); case BsonType.DateTime: return(ObjectSerializer.UnixEpoch.AddMilliseconds(Reader.ReadDateTime()).ToString()); case BsonType.Decimal128: return(Reader.ReadDecimal128().ToString()); case BsonType.Double: return(Reader.ReadDouble().ToString()); case BsonType.Int32: return(Reader.ReadInt32().ToString()); case BsonType.Int64: return(Reader.ReadInt64().ToString()); case BsonType.JavaScript: return(Reader.ReadJavaScript()); case BsonType.JavaScriptWithScope: return(Reader.ReadJavaScriptWithScope()); case BsonType.Null: Reader.ReadNull(); return(null); case BsonType.ObjectId: return(Reader.ReadObjectId().ToString()); case BsonType.String: return(Reader.ReadString()); case BsonType.Symbol: return(Reader.ReadSymbol()); default: throw new ArgumentException("Expected a char value, but was a " + FieldDataType.ToString() + ".", nameof(FieldDataType)); } }
public override Balance Deserialize(IBsonReader bsonReader) { string read = null; bsonReader.ReadStartDocument(); var balance = bsonReader .ReadDocument( "_id", ref read, bR => { // ReSharper disable AccessToModifiedClosure bR.ReadStartDocument(); var bal = new Balance { Date = bR.ReadDateTime("date", ref read), User = bR.ReadString("user", ref read), Currency = bR.ReadString("currency", ref read), Title = bR.ReadInt32("title", ref read), SubTitle = bR.ReadInt32("subtitle", ref read), Content = bR.ReadString("content", ref read), Remark = bR.ReadString("remark", ref read), }; bR.ReadEndDocument(); return(bal); // ReSharper restore AccessToModifiedClosure }); balance.Fund = bsonReader.ReadDouble("total", ref read) ?? bsonReader.ReadInt32("count", ref read) !.Value; bsonReader.ReadEndDocument(); return(balance); }
/// <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."); } }
/// <summary> /// Reads a char value. /// </summary> /// <param name="Reader">Binary reader.</param> /// <param name="FieldDataType">Field data type.</param> /// <returns>Char value.</returns> /// <exception cref="ArgumentException">If the <paramref name="FieldDataType"/> was invalid.</exception> public static char ReadChar(IBsonReader Reader, BsonType FieldDataType) { switch (FieldDataType) { 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.MinValue); case BsonType.MaxKey: Reader.ReadMaxKey(); return(char.MaxValue); case BsonType.String: string s = Reader.ReadString(); return(string.IsNullOrEmpty(s) ? (char)0 : s[0]); default: throw new ArgumentException("Expected a char value, but was a " + FieldDataType.ToString() + ".", nameof(FieldDataType)); } }
/// <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."); } }
/// <summary> /// Reads a 64-bit unsigned integer value. /// </summary> /// <param name="Reader">Binary reader.</param> /// <param name="FieldDataType">Field data type.</param> /// <returns>64-bit unsigned integer value.</returns> /// <exception cref="ArgumentException">If the <paramref name="FieldDataType"/> was invalid.</exception> public static ulong ReadUInt64(IBsonReader Reader, BsonType FieldDataType) { switch (FieldDataType) { case BsonType.Boolean: return(Reader.ReadBoolean() ? (ulong)1 : (ulong)0); case BsonType.Decimal128: return((ulong)Reader.ReadDecimal128()); case BsonType.Double: return((ulong)Reader.ReadDouble()); case BsonType.Int32: return((ulong)Reader.ReadInt32()); case BsonType.Int64: return((ulong)Reader.ReadInt64()); case BsonType.String: return(ulong.Parse(Reader.ReadString())); case BsonType.MinKey: Reader.ReadMinKey(); return(ulong.MinValue); case BsonType.MaxKey: Reader.ReadMaxKey(); return(ulong.MaxValue); default: throw new ArgumentException("Expected a 64-bit unsigned integer value, but was a " + FieldDataType.ToString() + ".", nameof(FieldDataType)); } }
/// <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 override object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) { IBsonReader reader = context.Reader; BsonType currentBsonType = reader.GetCurrentBsonType(); switch (currentBsonType) { case BsonType.Array: if (context.DynamicArraySerializer != null) { return(context.DynamicArraySerializer.Deserialize(context)); } break; case BsonType.Binary: { BsonBinaryData bsonBinaryData = reader.ReadBinaryData(); BsonBinarySubType subType = bsonBinaryData.SubType; if (subType == BsonBinarySubType.UuidStandard || subType == BsonBinarySubType.UuidLegacy) { return(bsonBinaryData.ToGuid()); } break; } case BsonType.Boolean: return(reader.ReadBoolean()); case BsonType.DateTime: return(new BsonDateTime(reader.ReadDateTime()).ToUniversalTime()); case BsonType.Decimal128: return(reader.ReadDecimal128()); case BsonType.Document: return(DeserializeDiscriminatedValue(context, args)); case BsonType.Double: return(reader.ReadDouble()); case BsonType.Int32: return(reader.ReadInt32()); case BsonType.Int64: return(reader.ReadInt64()); case BsonType.Null: reader.ReadNull(); return(null); case BsonType.ObjectId: return(reader.ReadObjectId()); case BsonType.String: return(reader.ReadString()); } Engine.Reflection.Compute.RecordError($"ObjectSerializer does not support BSON type '{currentBsonType}'."); return(null); }
public void TestArrayTwoElements() { var json = "[1, 2]"; using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.Array, _bsonReader.ReadBsonType()); _bsonReader.ReadStartArray(); Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType()); Assert.Equal(1, _bsonReader.ReadInt32()); Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType()); Assert.Equal(2, _bsonReader.ReadInt32()); Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType()); _bsonReader.ReadEndArray(); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } Assert.Equal(json, BsonSerializer.Deserialize <BsonArray>(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 override VoucherDetail Deserialize(IBsonReader bsonReader) { string read = null; bsonReader.ReadStartDocument(); var detail = new VoucherDetail { Currency = bsonReader.ReadString("currency", ref read), Title = bsonReader.ReadInt32("title", ref read), SubTitle = bsonReader.ReadInt32("subtitle", ref read), Content = bsonReader.ReadString("content", ref read), Fund = bsonReader.ReadDouble("fund", ref read), Remark = bsonReader.ReadString("remark", ref read) }; bsonReader.ReadEndDocument(); return(detail); }
private JToken GenerateJToken(IBsonReader reader, JToken parent) { switch (reader.CurrentBsonType) { case BsonType.Symbol: case BsonType.JavaScriptWithScope: case BsonType.JavaScript: case BsonType.ObjectId: case BsonType.RegularExpression: case BsonType.DateTime: case BsonType.Decimal128: case BsonType.MinKey: case BsonType.MaxKey: case BsonType.String: return(reader.ReadString()); case BsonType.Binary: return(reader.ReadBytes()); case BsonType.Undefined: reader.ReadUndefined(); return(JValue.CreateUndefined()); case BsonType.Boolean: return(reader.ReadBoolean()); case BsonType.Null: reader.ReadNull(); return(JValue.CreateNull()); case BsonType.Int32: return(reader.ReadInt32()); case BsonType.Int64: case BsonType.Timestamp: return(reader.ReadInt64()); case BsonType.Double: return(reader.ReadDouble()); case BsonType.Document: return(GenerateJObject(reader, parent)); case BsonType.Array: return(GenerateJArray(reader, parent)); case BsonType.EndOfDocument: break; default: break; } return(null); }
public void TestNestedDocument() { var json = "{ \"a\" : { \"b\" : 1, \"c\" : 2 } }"; using (_bsonReader = new JsonReader(json)) { Assert.AreEqual(BsonType.Document, _bsonReader.ReadBsonType()); _bsonReader.ReadStartDocument(); Assert.AreEqual(BsonType.Document, _bsonReader.ReadBsonType()); Assert.AreEqual("a", _bsonReader.ReadName()); _bsonReader.ReadStartDocument(); Assert.AreEqual("b", _bsonReader.ReadName()); Assert.AreEqual(1, _bsonReader.ReadInt32()); Assert.AreEqual("c", _bsonReader.ReadName()); Assert.AreEqual(2, _bsonReader.ReadInt32()); _bsonReader.ReadEndDocument(); _bsonReader.ReadEndDocument(); Assert.AreEqual(BsonReaderState.Done, _bsonReader.State); } Assert.AreEqual(json, BsonSerializer.Deserialize <BsonDocument>(json).ToJson()); }
public override Amortization Deserialize(IBsonReader bsonReader) { string read = null; bsonReader.ReadStartDocument(); var amort = new Amortization { ID = bsonReader.ReadGuid("_id", ref read), User = bsonReader.ReadString("user", ref read), Name = bsonReader.ReadString("name", ref read), Value = bsonReader.ReadDouble("value", ref read), Date = bsonReader.ReadDateTime("date", ref read), TotalDays = bsonReader.ReadInt32("tday", ref read) }; switch (bsonReader.ReadString("interval", ref read)) { case "d": amort.Interval = AmortizeInterval.EveryDay; break; case "w": amort.Interval = AmortizeInterval.SameDayOfWeek; break; case "W": amort.Interval = AmortizeInterval.LastDayOfWeek; break; case "m": amort.Interval = AmortizeInterval.SameDayOfMonth; break; case "M": amort.Interval = AmortizeInterval.LastDayOfMonth; break; case "y": amort.Interval = AmortizeInterval.SameDayOfYear; break; case "Y": amort.Interval = AmortizeInterval.LastDayOfYear; break; } amort.Template = bsonReader.ReadDocument("template", ref read, VoucherSerializer.Deserialize); amort.Schedule = bsonReader.ReadArray("schedule", ref read, ItemSerializer.Deserialize); amort.Remark = bsonReader.ReadString("remark", ref read); bsonReader.ReadEndDocument(); return(amort); }
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 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 override Asset Deserialize(IBsonReader bsonReader) { string read = null; bsonReader.ReadStartDocument(); var asset = new Asset { ID = bsonReader.ReadGuid("_id", ref read), User = bsonReader.ReadString("user", ref read), Name = bsonReader.ReadString("name", ref read), Date = bsonReader.ReadDateTime("date", ref read), Currency = bsonReader.ReadString("currency", ref read), Value = bsonReader.ReadDouble("value", ref read), Salvge = bsonReader.ReadDouble("salvge", ref read), Life = bsonReader.ReadInt32("life", ref read), Title = bsonReader.ReadInt32("title", ref read), DepreciationTitle = bsonReader.ReadInt32("deptitle", ref read), DevaluationTitle = bsonReader.ReadInt32("devtitle", ref read), DepreciationExpenseTitle = bsonReader.ReadInt32("exptitle", ref read), DevaluationExpenseTitle = bsonReader.ReadInt32("exvtitle", ref read) }; switch (bsonReader.ReadString("method", ref read)) { case "sl": asset.Method = DepreciationMethod.StraightLine; break; case "sy": asset.Method = DepreciationMethod.SumOfTheYear; break; case "dd": asset.Method = DepreciationMethod.DoubleDeclineMethod; break; default: asset.Method = DepreciationMethod.None; break; } if (asset.DepreciationExpenseTitle > 100) { asset.DepreciationExpenseSubTitle = asset.DepreciationExpenseTitle % 100; asset.DepreciationExpenseTitle /= 100; } if (asset.DevaluationExpenseTitle > 100) { asset.DevaluationExpenseSubTitle = asset.DevaluationExpenseTitle % 100; asset.DevaluationExpenseTitle /= 100; } asset.Schedule = bsonReader.ReadArray("schedule", ref read, ItemSerializer.Deserialize); asset.Remark = bsonReader.ReadString("remark", ref read); bsonReader.ReadEndDocument(); return(asset); }
public void TestDocumentOneElement() { var json = "{ \"x\" : 1 }"; using (_bsonReader = new JsonReader(json)) { Assert.AreEqual(BsonType.Document, _bsonReader.ReadBsonType()); _bsonReader.ReadStartDocument(); Assert.AreEqual(BsonType.Int32, _bsonReader.ReadBsonType()); Assert.AreEqual("x", _bsonReader.ReadName()); Assert.AreEqual(1, _bsonReader.ReadInt32()); Assert.AreEqual(BsonType.EndOfDocument, _bsonReader.ReadBsonType()); _bsonReader.ReadEndDocument(); Assert.AreEqual(BsonReaderState.Done, _bsonReader.State); } Assert.AreEqual(json, BsonSerializer.Deserialize <BsonDocument>(json).ToJson()); }
/*******************************************/ public override IntPtr Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) { IBsonReader bsonReader = context.Reader; if (bsonReader.CurrentBsonType == BsonType.Int32) { return(new IntPtr(bsonReader.ReadInt32())); } else if (bsonReader.CurrentBsonType == BsonType.Int64) { return(new IntPtr(bsonReader.ReadInt64())); } else { return(new IntPtr()); } }
object GetDeserializedValue(Type valueType, ref IBsonReader bsonReader) { var bsonType = bsonReader.CurrentBsonType; if (bsonType == BsonType.Null) { bsonReader.ReadNull(); return(null); } if (valueType == typeof(Guid)) { var binaryData = bsonReader.ReadBinaryData(); return(binaryData.ToGuid()); } else if (valueType == typeof(double)) { return(bsonReader.ReadDouble()); } else if (valueType == typeof(float)) { return((float)bsonReader.ReadDouble()); } else if (valueType == typeof(int)) { return(bsonReader.ReadInt32()); } else if (valueType == typeof(long)) { return(bsonReader.ReadInt64()); } else if (valueType == typeof(bool)) { return(bsonReader.ReadBoolean()); } else if (valueType == typeof(string)) { return(bsonReader.ReadString()); } else if (valueType == typeof(decimal)) { return(decimal.Parse(bsonReader.ReadString(), CultureInfo.InvariantCulture)); } throw new FailedConceptSerialization($"Could not deserialize the concept value to '{valueType.FullName}'"); }
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()); }
object GetDeserializedValue(Type valueType, ref IBsonReader bsonReader) { var bsonType = bsonReader.CurrentBsonType; if (bsonType == BsonType.Null) { bsonReader.ReadNull(); return(null); } if (valueType == typeof(Guid)) { var binaryData = bsonReader.ReadBinaryData(); return(binaryData.ToGuid()); } else if (valueType == typeof(double)) { return(bsonReader.ReadDouble()); } else if (valueType == typeof(float)) { return((float)bsonReader.ReadDouble()); } else if (valueType == typeof(Int32)) { return(bsonReader.ReadInt32()); } else if (valueType == typeof(Int64)) { return(bsonReader.ReadInt64()); } else if (valueType == typeof(bool)) { return(bsonReader.ReadBoolean()); } else if (valueType == typeof(string)) { return(bsonReader.ReadString()); } else if (valueType == typeof(decimal)) { return(decimal.Parse(bsonReader.ReadString())); } throw new Exception(); }
public static object ReadObject(IBsonReader reader) { var bsonType = reader.CurrentBsonType; switch (bsonType) { case BsonType.String: return(reader.ReadString()); case BsonType.Double: return(reader.ReadDouble()); case BsonType.Int32: return(reader.ReadInt32()); default: var message = string.Format("Cannot provide value for type : {0}", bsonType); throw new Exception(message); } }
static object ReadObject(IBsonReader bsonReader) //_120509_173140 sync, test { switch (bsonReader.GetCurrentBsonType()) { case BsonType.Array: return(ReadArray(bsonReader)); // replacement case BsonType.Boolean: return(bsonReader.ReadBoolean()); case BsonType.DateTime: return(BsonUtils.ToDateTimeFromMillisecondsSinceEpoch(bsonReader.ReadDateTime())); case BsonType.Decimal128: return(Decimal128.ToDecimal(bsonReader.ReadDecimal128())); case BsonType.Document: return(ReadCustomObject(bsonReader)); // replacement case BsonType.Double: return(bsonReader.ReadDouble()); case BsonType.Int32: return(bsonReader.ReadInt32()); case BsonType.Int64: return(bsonReader.ReadInt64()); case BsonType.Null: bsonReader.ReadNull(); return(null); case BsonType.ObjectId: return(bsonReader.ReadObjectId()); case BsonType.String: return(bsonReader.ReadString()); case BsonType.Binary: var data = bsonReader.ReadBinaryData(); switch (data.SubType) { case BsonBinarySubType.UuidLegacy: case BsonBinarySubType.UuidStandard: return(data.ToGuid()); default: return(data); } default: return(BsonSerializer.Deserialize <BsonValue>(bsonReader)); } }
/// <summary> /// Reads a boolean value. /// </summary> /// <param name="Reader">Binary reader.</param> /// <param name="FieldDataType">Field data type.</param> /// <returns>Boolean value.</returns> /// <exception cref="ArgumentException">If the <paramref name="FieldDataType"/> was invalid.</exception> public static bool ReadBoolean(IBsonReader Reader, BsonType FieldDataType) { switch (FieldDataType) { 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); default: throw new ArgumentException("Expected a boolean value, but was a " + FieldDataType.ToString() + ".", nameof(FieldDataType)); } }
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 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 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 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 TestArrayTwoElements() { var json = "[1, 2]"; using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.Array, _bsonReader.ReadBsonType()); _bsonReader.ReadStartArray(); Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType()); Assert.Equal(1, _bsonReader.ReadInt32()); Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType()); Assert.Equal(2, _bsonReader.ReadInt32()); Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType()); _bsonReader.ReadEndArray(); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } Assert.Equal(json, BsonSerializer.Deserialize<BsonArray>(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 TestBookmark() { var json = "{ \"x\" : 1, \"y\" : 2 }"; using (_bsonReader = new JsonReader(json)) { // do everything twice returning to bookmark in between var bookmark = _bsonReader.GetBookmark(); Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType()); _bsonReader.ReturnToBookmark(bookmark); Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType()); bookmark = _bsonReader.GetBookmark(); _bsonReader.ReadStartDocument(); _bsonReader.ReturnToBookmark(bookmark); _bsonReader.ReadStartDocument(); bookmark = _bsonReader.GetBookmark(); Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType()); _bsonReader.ReturnToBookmark(bookmark); Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType()); bookmark = _bsonReader.GetBookmark(); Assert.Equal("x", _bsonReader.ReadName()); _bsonReader.ReturnToBookmark(bookmark); Assert.Equal("x", _bsonReader.ReadName()); bookmark = _bsonReader.GetBookmark(); Assert.Equal(1, _bsonReader.ReadInt32()); _bsonReader.ReturnToBookmark(bookmark); Assert.Equal(1, _bsonReader.ReadInt32()); bookmark = _bsonReader.GetBookmark(); Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType()); _bsonReader.ReturnToBookmark(bookmark); Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType()); bookmark = _bsonReader.GetBookmark(); Assert.Equal("y", _bsonReader.ReadName()); _bsonReader.ReturnToBookmark(bookmark); Assert.Equal("y", _bsonReader.ReadName()); bookmark = _bsonReader.GetBookmark(); Assert.Equal(2, _bsonReader.ReadInt32()); _bsonReader.ReturnToBookmark(bookmark); Assert.Equal(2, _bsonReader.ReadInt32()); bookmark = _bsonReader.GetBookmark(); Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType()); _bsonReader.ReturnToBookmark(bookmark); Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType()); bookmark = _bsonReader.GetBookmark(); _bsonReader.ReadEndDocument(); _bsonReader.ReturnToBookmark(bookmark); _bsonReader.ReadEndDocument(); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } Assert.Equal(json, BsonSerializer.Deserialize<BsonDocument>(json).ToJson()); }
public void TestNestedArray() { var json = "{ \"a\" : [1, 2] }"; using (_bsonReader = new JsonReader(json)) { Assert.AreEqual(BsonType.Document, _bsonReader.ReadBsonType()); _bsonReader.ReadStartDocument(); Assert.AreEqual(BsonType.Array, _bsonReader.ReadBsonType()); Assert.AreEqual("a", _bsonReader.ReadName()); _bsonReader.ReadStartArray(); Assert.AreEqual(1, _bsonReader.ReadInt32()); Assert.AreEqual(2, _bsonReader.ReadInt32()); _bsonReader.ReadEndArray(); _bsonReader.ReadEndDocument(); Assert.AreEqual(BsonReaderState.Initial, _bsonReader.State); } Assert.AreEqual(json, BsonSerializer.Deserialize<BsonDocument>(json).ToJson()); }