// public methods #pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(BitArray)); BsonType bsonType = bsonReader.GetCurrentBsonType(); BitArray bitArray; switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return(null); case BsonType.Binary: return(new BitArray(bsonReader.ReadBytes())); case BsonType.Document: bsonReader.ReadStartDocument(); var length = bsonReader.ReadInt32("Length"); var bytes = bsonReader.ReadBytes("Bytes"); bsonReader.ReadEndDocument(); bitArray = new BitArray(bytes); bitArray.Length = length; return(bitArray); case BsonType.String: var s = bsonReader.ReadString(); bitArray = new BitArray(s.Length); for (int i = 0; i < s.Length; i++) { var c = s[i]; switch (c) { case '0': break; case '1': bitArray[i] = true; break; default: throw new FileFormatException("String value is not a valid BitArray."); } } return(bitArray); default: var message = string.Format("Cannot deserialize Byte[] from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
// public methods #pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(BitArray)); BsonType bsonType = bsonReader.GetCurrentBsonType(); BitArray bitArray; switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return null; case BsonType.Binary: return new BitArray(bsonReader.ReadBytes()); case BsonType.Document: bsonReader.ReadStartDocument(); var length = bsonReader.ReadInt32("Length"); var bytes = bsonReader.ReadBytes("Bytes"); bsonReader.ReadEndDocument(); bitArray = new BitArray(bytes); bitArray.Length = length; return bitArray; case BsonType.String: var s = bsonReader.ReadString(); bitArray = new BitArray(s.Length); for (int i = 0; i < s.Length; i++) { var c = s[i]; switch (c) { case '0': break; case '1': bitArray[i] = true; break; default: throw new FileFormatException("String value is not a valid BitArray."); } } return bitArray; default: var message = string.Format("Cannot deserialize Byte[] from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
// public methods /// <summary> /// Deserializes an Bitmap from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the Bitmap.</param> /// <param name="actualType">The actual type of the Bitmap.</param> /// <param name="options">The serialization options.</param> /// <returns>A Bitmap.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { if (nominalType != typeof(Image) && nominalType != typeof(Bitmap)) { var message = string.Format("Nominal type must be Image or Bitmap, not {0}.", nominalType.FullName); throw new ArgumentException(message, "nominalType"); } if (actualType != typeof(Bitmap)) { var message = string.Format("Actual type must be Bitmap, not {0}.", actualType.FullName); throw new ArgumentException(message, "actualType"); } var bsonType = bsonReader.GetCurrentBsonType(); byte[] bytes; switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return(null); case BsonType.Binary: bytes = bsonReader.ReadBytes(); break; case BsonType.Document: bsonReader.ReadStartDocument(); bsonReader.ReadString("_t"); bytes = bsonReader.ReadBytes("bitmap"); bsonReader.ReadEndDocument(); break; default: var message = string.Format("BsonType must be Null, Binary or Document, not {0}.", bsonType); throw new FileFormatException(message); } var stream = new MemoryStream(bytes); return(new Bitmap(stream)); }
// public methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(sbyte)); var bsonType = bsonReader.GetCurrentBsonType(); var lostData = false; sbyte value; switch (bsonType) { case BsonType.Binary: var bytes = bsonReader.ReadBytes(); if (bytes.Length != 1) { throw new Exception("Binary data for SByte must be exactly one byte long."); } value = (sbyte)bytes[0]; break; case BsonType.Int32: var int32Value = bsonReader.ReadInt32(); value = (sbyte)int32Value; lostData = (int)value != int32Value; break; case BsonType.Int64: var int64Value = bsonReader.ReadInt64(); value = (sbyte)int64Value; lostData = (int)value != int64Value; break; case BsonType.String: var s = bsonReader.ReadString(); if (s.Length == 1) { s = "0" + s; } value = (sbyte)byte.Parse(s, NumberStyles.HexNumber); break; default: var message = string.Format("Cannot deserialize SByte from BsonType {0}.", bsonType); throw new Exception(message); } if (lostData) { var message = string.Format("Data loss occurred when trying to convert from {0} to SByte.", bsonType); throw new Exception(message); } return(value); }
// public methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(sbyte)); var bsonType = bsonReader.GetCurrentBsonType(); var lostData = false; sbyte value; switch (bsonType) { case BsonType.Binary: var bytes = bsonReader.ReadBytes(); if (bytes.Length != 1) { throw new FileFormatException("Binary data for SByte must be exactly one byte long."); } value = (sbyte)bytes[0]; break; case BsonType.Int32: var int32Value = bsonReader.ReadInt32(); value = (sbyte)int32Value; lostData = (int)value != int32Value; break; case BsonType.Int64: var int64Value = bsonReader.ReadInt64(); value = (sbyte)int64Value; lostData = (int)value != int64Value; break; case BsonType.String: var s = bsonReader.ReadString(); if (s.Length == 1) { s = "0" + s; } value = (sbyte)byte.Parse(s, NumberStyles.HexNumber); break; default: var message = string.Format("Cannot deserialize SByte from BsonType {0}.", bsonType); throw new FileFormatException(message); } if (lostData) { var message = string.Format("Data loss occurred when trying to convert from {0} to SByte.", bsonType); throw new FileFormatException(message); } return value; }
public void TestHexData() { var expectedBytes = new byte[] { 0x01, 0x23 }; var json = "HexData(0, \"123\")"; using (_bsonReader = BsonReader.Create(json)) { Assert.AreEqual(BsonType.Binary, _bsonReader.ReadBsonType()); var bytes = _bsonReader.ReadBytes(); Assert.IsTrue(expectedBytes.SequenceEqual(bytes)); Assert.AreEqual(BsonReaderState.Done, _bsonReader.State); } var expectedJson = "new BinData(0, \"ASM=\")"; Assert.AreEqual(expectedJson, BsonSerializer.Deserialize <byte[]>(new StringReader(json)).ToJson()); }
// public methods #pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(byte[])); BsonType bsonType = bsonReader.GetCurrentBsonType(); byte[] bytes; string message; switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return(null); case BsonType.Binary: bytes = bsonReader.ReadBytes(); return(bytes); case BsonType.String: var s = bsonReader.ReadString(); if ((s.Length % 2) != 0) { s = "0" + s; // prepend a zero to make length even } bytes = new byte[s.Length / 2]; for (int i = 0; i < s.Length; i += 2) { var hex = s.Substring(i, 2); var b = byte.Parse(hex, NumberStyles.HexNumber); bytes[i / 2] = b; } return(bytes); default: message = string.Format("Cannot deserialize Byte[] from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
// public methods #pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(byte[])); BsonType bsonType = bsonReader.GetCurrentBsonType(); byte[] bytes; string message; switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return null; case BsonType.Binary: bytes = bsonReader.ReadBytes(); return bytes; case BsonType.String: var s = bsonReader.ReadString(); if ((s.Length % 2) != 0) { s = "0" + s; // prepend a zero to make length even } bytes = new byte[s.Length / 2]; for (int i = 0; i < s.Length; i += 2) { var hex = s.Substring(i, 2); var b = byte.Parse(hex, NumberStyles.HexNumber); bytes[i / 2] = b; } return bytes; default: message = string.Format("Cannot deserialize Byte[] from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
private void ReadValue() { //bool value = true; _type = PBBsonReaderType.Value; switch (_bsonType) { case BsonType.Document: WriteLine(2, "ReadStartDocument"); WriteLine(1, "{"); //_indent += 2; Indent(2); _reader.ReadStartDocument(); _type = PBBsonReaderType.Document; //value = false; break; case BsonType.Array: WriteLine(2, "ReadStartArray"); WriteLine(1, "["); //_indent += 2; Indent(2); _reader.ReadStartArray(); _type = PBBsonReaderType.Array; //value = false; break; case BsonType.Binary: _value = BsonValue.Create(_reader.ReadBytes()); break; case BsonType.Boolean: _value = BsonValue.Create(_reader.ReadBoolean()); break; case BsonType.DateTime: _value = BsonValue.Create(_reader.ReadDateTime()); break; case BsonType.Double: _value = BsonValue.Create(_reader.ReadDouble()); break; case BsonType.Int32: _value = BsonValue.Create(_reader.ReadInt32()); break; case BsonType.Int64: _value = BsonValue.Create(_reader.ReadInt64()); break; case BsonType.JavaScript: _value = BsonValue.Create(_reader.ReadJavaScript()); break; case BsonType.JavaScriptWithScope: _value = BsonValue.Create(_reader.ReadJavaScriptWithScope()); break; case BsonType.MaxKey: _reader.ReadMaxKey(); _value = BsonMaxKey.Value; break; case BsonType.MinKey: _reader.ReadMinKey(); _value = BsonMinKey.Value; break; case BsonType.Null: _reader.ReadNull(); _value = BsonNull.Value; break; case BsonType.ObjectId: _value = BsonValue.Create(_reader.ReadObjectId()); break; case BsonType.RegularExpression: _value = BsonValue.Create(_reader.ReadRegularExpression()); break; case BsonType.String: _value = BsonValue.Create(_reader.ReadString()); break; case BsonType.Symbol: _value = BsonValue.Create(_reader.ReadSymbol()); break; case BsonType.Timestamp: _value = BsonValue.Create(_reader.ReadTimestamp()); break; case BsonType.Undefined: _reader.ReadUndefined(); _value = BsonUndefined.Value; break; default: throw new PBException("error unable to read value type {0}", _bsonType); } //return value; }
// public methods /// <summary> /// Deserializes an Bitmap from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the Bitmap.</param> /// <param name="actualType">The actual type of the Bitmap.</param> /// <param name="options">The serialization options.</param> /// <returns>A Bitmap.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { if (nominalType != typeof(Image) && nominalType != typeof(Bitmap)) { var message = string.Format("Nominal type must be Image or Bitmap, not {0}.", nominalType.FullName); throw new ArgumentException(message, "nominalType"); } if (actualType != typeof(Bitmap)) { var message = string.Format("Actual type must be Bitmap, not {0}.", actualType.FullName); throw new ArgumentException(message, "actualType"); } var bsonType = bsonReader.GetCurrentBsonType(); byte[] bytes; switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return null; case BsonType.Binary: bytes = bsonReader.ReadBytes(); break; case BsonType.Document: bsonReader.ReadStartDocument(); bsonReader.ReadString("_t"); bytes = bsonReader.ReadBytes("bitmap"); bsonReader.ReadEndDocument(); break; default: var message = string.Format("BsonType must be Null, Binary or Document, not {0}.", bsonType); throw new FileFormatException(message); } var stream = new MemoryStream(bytes); return new Bitmap(stream); }