Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Extend BsonWriter to write Uri as string.
  • Loading branch information
lanwin committed May 21, 2010
1 parent 9f226e1 commit 5f3f6cb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 25 deletions.
12 changes: 11 additions & 1 deletion source/MongoDB.Tests/UnitTests/Bson/TestBsonWriter.cs
Expand Up @@ -202,13 +202,23 @@ public void TestWriteBytesAsBinary()
}

[Test]
public void TestWriteTimeSpanAsTicks()
public void TestWriteTimeSpanAsLong()
{
var span = TimeSpan.FromSeconds(123456);

var bson = Serialize(new Document("span", span));

Assert.AreEqual("EwAAABJzcGFuAACggnEfAQAAAA==",bson);
}

[Test]
public void TestWriteUriAsString()
{
var uri = new Uri("http://www.microsoft.com");

var bson = Serialize(new Document("uri", uri));

Assert.AreEqual("KAAAAAJ1cmkAGgAAAGh0dHA6Ly93d3cubWljcm9zb2Z0LmNvbS8AAA==",bson);
}
}
}
34 changes: 10 additions & 24 deletions source/MongoDB/Bson/BsonWriter.cs
Expand Up @@ -83,10 +83,11 @@ public void WriteValue(BsonType type, Object obj)
_writer.Write(Convert.ToDouble(obj));
return;
case BsonType.String:
{
Write((String)obj);
if(obj is string)
Write((string)obj);
else
Write(obj.ToString());
return;
}
case BsonType.Obj:
if(obj is DBRef)
Write((DBRef)obj);
Expand All @@ -97,25 +98,17 @@ public void WriteValue(BsonType type, Object obj)
WriteArray((IEnumerable)obj);
return;
case BsonType.Regex:
{
Write((MongoRegex)obj);
return;
}
case BsonType.Code:
{
Write((Code)obj);
return;
}
case BsonType.Symbol:
{
WriteValue(BsonType.String, ((MongoSymbol)obj).Value);
return;
}
case BsonType.CodeWScope:
{
Write((CodeWScope)obj);
return;
}
case BsonType.Binary:
{
if(obj is Guid)
Expand Down Expand Up @@ -343,25 +336,19 @@ public int CalculateSize(Object obj)
case BsonType.Number:
return sizeof(Double);
case BsonType.String:
return CalculateSize((string)obj);
if(obj is string)
return CalculateSize((string)obj);
return CalculateSize(obj.ToString());
case BsonType.Obj:
{
if(obj.GetType() == typeof(DBRef))
return CalculateSize((DBRef)obj);
return CalculateSizeObject(obj);
}
return obj.GetType() == typeof(DBRef) ? CalculateSize((DBRef)obj) : CalculateSizeObject(obj);
case BsonType.Array:
return CalculateSize((IEnumerable)obj);
case BsonType.Regex:
{
return CalculateSize((MongoRegex)obj);
}
case BsonType.Code:
return CalculateSize((Code)obj);
case BsonType.CodeWScope:
{
return CalculateSize((CodeWScope)obj);
}
case BsonType.Binary:
{
if(obj is Guid)
Expand All @@ -372,9 +359,7 @@ public int CalculateSize(Object obj)
return CalculateSize((Binary)obj);
}
case BsonType.Symbol:
{
return CalculateSize(((MongoSymbol)obj).Value, true);
}
}

throw new NotImplementedException(String.Format("Calculating size of {0} is not implemented.", obj.GetType().Name));
Expand Down Expand Up @@ -565,13 +550,14 @@ 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))
return BsonType.Number;
if(type == typeof(String))
return BsonType.String;
if(type == typeof(Uri))
return BsonType.String;
if(type == typeof(int))
return BsonType.Integer;
if(type == typeof(long))
Expand Down

0 comments on commit 5f3f6cb

Please sign in to comment.