public void WriteBytes() { byte[] data = Encoding.UTF8.GetBytes("Hello world!"); MemoryStream ms = new MemoryStream(); BsonDataWriter writer = new BsonDataWriter(ms); writer.WriteStartArray(); writer.WriteValue("a"); writer.WriteValue("b"); writer.WriteValue(data); writer.WriteEndArray(); writer.Flush(); ms.Seek(0, SeekOrigin.Begin); string expected = "2B-00-00-00-02-30-00-02-00-00-00-61-00-02-31-00-02-00-00-00-62-00-05-32-00-0C-00-00-00-00-48-65-6C-6C-6F-20-77-6F-72-6C-64-21-00"; string bson = BytesToHex(ms.ToArray()); Assert.AreEqual(expected, bson); BsonDataReader reader = new BsonDataReader(new MemoryStream(ms.ToArray())); reader.ReadRootValueAsArray = true; reader.Read(); reader.Read(); reader.Read(); reader.Read(); Assert.AreEqual(JsonToken.Bytes, reader.TokenType); CollectionAssert.AreEquivalent(data, (byte[])reader.Value); }
public void Serialize(BsonDataWriter wrt) { wrt.WriteStartArray(); wrt.WriteValue(Key); wrt.WriteValue(Value); wrt.WriteEndArray(); }
public void Serialize(BsonDataWriter wrt) { wrt.WriteStartArray(); wrt.WriteValue(Path); wrt.WriteValue(PacketId); wrt.WriteEndArray(); }
public void Serialize(BsonDataWriter wrt) { wrt.WriteStartArray(); wrt.WriteValue(Res); wrt.WriteValue(Msg); wrt.WriteEndArray(); }
public string Serialize <TKey>(Dictionary <TKey, object[]> list) { MemoryStream ms = new MemoryStream(); using (BsonDataWriter writer = new BsonDataWriter(ms)) { writer.WriteStartArray(); foreach (KeyValuePair <TKey, object[]> val in list) { writer.WriteStartObject(); for (int i = 0; i < propertyKeys.Length; i++) { writer.WritePropertyName(propertyKeys[i]); writer.WriteValue(val.Value[i].Serialize()); } writer.WriteEndObject(); } writer.WriteEndArray(); } return(Convert.ToBase64String(ms.ToArray())); }
public void DateTimeZoneHandling() { MemoryStream ms = new MemoryStream(); JsonWriter writer = new BsonDataWriter(ms) { DateTimeZoneHandling = Json.DateTimeZoneHandling.Utc }; writer.WriteStartArray(); writer.WriteValue(new DateTime(2000, 1, 1, 1, 1, 1, DateTimeKind.Unspecified)); writer.WriteEndArray(); Assert.AreEqual("10-00-00-00-09-30-00-C8-88-07-6B-DC-00-00-00-00", (BitConverter.ToString(ms.ToArray()))); }
public void WriteArrayBsonFromSite() { MemoryStream ms = new MemoryStream(); BsonDataWriter writer = new BsonDataWriter(ms); writer.WriteStartArray(); writer.WriteValue("a"); writer.WriteValue("b"); writer.WriteValue("c"); writer.WriteEndArray(); writer.Flush(); ms.Seek(0, SeekOrigin.Begin); string expected = "20-00-00-00-02-30-00-02-00-00-00-61-00-02-31-00-02-00-00-00-62-00-02-32-00-02-00-00-00-63-00-00"; string bson = BytesToHex(ms.ToArray()); Assert.AreEqual(expected, bson); }
public void WriteDateTimes() { MemoryStream ms = new MemoryStream(); BsonDataWriter writer = new BsonDataWriter(ms); writer.DateTimeKindHandling = DateTimeKind.Unspecified; writer.WriteStartArray(); writer.WriteValue(new DateTime(2000, 10, 12, 20, 55, 0, DateTimeKind.Utc)); writer.WriteValue(new DateTime(2000, 10, 12, 20, 55, 0, DateTimeKind.Local)); writer.WriteValue(new DateTime(2000, 10, 12, 20, 55, 0, DateTimeKind.Unspecified)); writer.WriteEndArray(); ms.Seek(0, SeekOrigin.Begin); BsonDataReader reader = new BsonDataReader(ms); reader.ReadRootValueAsArray = true; reader.DateTimeKindHandling = DateTimeKind.Utc; Assert.IsTrue(reader.Read()); Assert.AreEqual(JsonToken.StartArray, reader.TokenType); Assert.IsTrue(reader.Read()); Assert.AreEqual(JsonToken.Date, reader.TokenType); Assert.AreEqual(new DateTime(2000, 10, 12, 20, 55, 0, DateTimeKind.Utc), reader.Value); Assert.IsTrue(reader.Read()); Assert.AreEqual(JsonToken.Date, reader.TokenType); Assert.AreEqual(new DateTime(2000, 10, 12, 20, 55, 0, DateTimeKind.Utc), reader.Value); Assert.IsTrue(reader.Read()); Assert.AreEqual(JsonToken.Date, reader.TokenType); Assert.AreEqual(new DateTime(2000, 10, 12, 20, 55, 0, DateTimeKind.Utc), reader.Value); Assert.IsTrue(reader.Read()); Assert.AreEqual(JsonToken.EndArray, reader.TokenType); Assert.IsFalse(reader.Read()); }
public void WriteReadEmptyAndNullStrings() { MemoryStream ms = new MemoryStream(); BsonDataWriter writer = new BsonDataWriter(ms); writer.WriteStartArray(); writer.WriteValue("Content!"); writer.WriteValue(""); writer.WriteValue((string)null); writer.WriteEndArray(); ms.Seek(0, SeekOrigin.Begin); BsonDataReader reader = new BsonDataReader(ms); reader.ReadRootValueAsArray = true; Assert.IsTrue(reader.Read()); Assert.AreEqual(JsonToken.StartArray, reader.TokenType); Assert.IsTrue(reader.Read()); Assert.AreEqual(JsonToken.String, reader.TokenType); Assert.AreEqual("Content!", reader.Value); Assert.IsTrue(reader.Read()); Assert.AreEqual(JsonToken.String, reader.TokenType); Assert.AreEqual("", reader.Value); Assert.IsTrue(reader.Read()); Assert.AreEqual(JsonToken.Null, reader.TokenType); Assert.AreEqual(null, reader.Value); Assert.IsTrue(reader.Read()); Assert.AreEqual(JsonToken.EndArray, reader.TokenType); Assert.IsFalse(reader.Read()); }
public void WriteNestedArray() { MemoryStream ms = new MemoryStream(); BsonDataWriter writer = new BsonDataWriter(ms); writer.WriteStartObject(); writer.WritePropertyName("_id"); writer.WriteValue(HexToBytes("4A-78-93-79-17-22-00-00-00-00-61-CF")); writer.WritePropertyName("a"); writer.WriteStartArray(); for (int i = 1; i <= 8; i++) { double value = (i != 5) ? Convert.ToDouble(i) : 5.78960446186581E+77d; writer.WriteValue(value); } writer.WriteEndArray(); writer.WritePropertyName("b"); writer.WriteValue("test"); writer.WriteEndObject(); writer.Flush(); ms.Seek(0, SeekOrigin.Begin); string expected = "87-00-00-00-05-5F-69-64-00-0C-00-00-00-00-4A-78-93-79-17-22-00-00-00-00-61-CF-04-61-00-5D-00-00-00-01-30-00-00-00-00-00-00-00-F0-3F-01-31-00-00-00-00-00-00-00-00-40-01-32-00-00-00-00-00-00-00-08-40-01-33-00-00-00-00-00-00-00-10-40-01-34-00-00-00-00-00-00-00-14-50-01-35-00-00-00-00-00-00-00-18-40-01-36-00-00-00-00-00-00-00-1C-40-01-37-00-00-00-00-00-00-00-20-40-00-02-62-00-05-00-00-00-74-65-73-74-00-00"; string bson = BytesToHex(ms.ToArray()); Assert.AreEqual(expected, bson); }