Permalink
Browse files

Extend BsonReader and BsonWriter to read and write MongoTimestamp.

  • Loading branch information...
1 parent 3eb7873 commit f57a416b2074aeb3334b059f59b0cf5b5f69631a @lanwin lanwin committed Sep 7, 2010
@@ -283,5 +283,18 @@ public void TestCanReadNagativeDates()
Assert.AreEqual(new DateTime(1960,1,1).ToUniversalTime(),document["date"]);
}
+
+ [Test]
+ public void TestCanReadMongoTimestamp()
+ {
+ const string bson = "EQAAABF0cwD1////////fwA=";
+
+ var document = Deserialize(bson);
+
+ Assert.IsNotNull(document);
+ var ts = (MongoTimestamp)document["ts"];
+ Assert.IsNotNull(ts);
+ Assert.AreEqual(long.MaxValue-10,ts.Value);
+ }
}
}
@@ -254,5 +254,13 @@ public void TestCanWriteNagativeDates()
Assert.AreEqual("EwAAAAlkYXRlAIBFaoO2////AA==",bson);
}
+
+ [Test]
+ public void TestCanWriteMongoTimestap()
+ {
+ var bson = Serialize(new Document("ts", new MongoTimestamp(long.MaxValue-10)));
+
+ Assert.AreEqual("EQAAABF0cwD1////////fwA=", bson);
+ }
}
}
@@ -142,6 +142,9 @@ public BsonReader(Stream stream, BsonReaderSettings settings)
case BsonType.Long:
Position += 8;
return _reader.ReadInt64();
+ case BsonType.Timestamp:
+ Position += 8;
+ return new MongoTimestamp(_reader.ReadInt64());
case BsonType.Date:
return ReadDateTime();
case BsonType.Oid:
@@ -74,6 +74,9 @@ public void WriteValue(BsonType type, Object obj)
else
_writer.Write((long)obj);
return;
+ case BsonType.Timestamp:
+ _writer.Write(((MongoTimestamp)obj).Value);
+ return;
case BsonType.Date:
Write((DateTime)obj);
return;
@@ -334,6 +337,7 @@ public int CalculateSize(Object obj)
return 4;
case BsonType.Long:
case BsonType.Date:
+ case BsonType.Timestamp:
return 8;
case BsonType.Oid:
return 12;
@@ -556,6 +560,7 @@ protected BsonType TranslateToBsonType(object obj)
if(obj is Enum) //special case enums
type = Enum.GetUnderlyingType(type);
+
if(type == typeof(Double))
return BsonType.Number;
if(type == typeof(Single))
@@ -568,6 +573,8 @@ protected BsonType TranslateToBsonType(object obj)
return BsonType.Integer;
if(type == typeof(long))
return BsonType.Long;
+ if(type == typeof(MongoTimestamp))
+ return BsonType.Timestamp;
if(type == typeof(bool))
return BsonType.Boolean;
if(type == typeof(Oid))
@@ -600,6 +607,7 @@ protected BsonType TranslateToBsonType(object obj)
return BsonType.Symbol;
if(type == typeof(byte[]))
return BsonType.Binary;
+
if(_descriptor.IsArray(obj))
return BsonType.Array;
if(_descriptor.IsObject(obj))

0 comments on commit f57a416

Please sign in to comment.