Skip to content

Commit

Permalink
Better formatting for Oid.
Browse files Browse the repository at this point in the history
Add Serializable to Oid.
  • Loading branch information
lanwin committed May 17, 2010
1 parent fa6f84c commit 9a90085
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 11 deletions.
2 changes: 1 addition & 1 deletion source/MongoDB.Tests/UnitTests/Bson/TestBsonReader.cs
Expand Up @@ -170,7 +170,7 @@ public class TestBsonReader : BsonTestBase
Assert.IsTrue(doc.Contains("_id"));
Assert.IsTrue(doc.Contains("a"));
Assert.IsTrue(doc.Contains("b"));
Assert.AreEqual("\"4a753ad8fac16ea58b290351\"", (doc["_id"]).ToString());
Assert.AreEqual("4a753ad8fac16ea58b290351", (doc["_id"]).ToString());
Assert.AreEqual(1, Convert.ToInt32(doc["a"]));
Assert.AreEqual("test", doc["b"]);
}
Expand Down
33 changes: 30 additions & 3 deletions source/MongoDB.Tests/UnitTests/TestOid.cs
@@ -1,4 +1,7 @@
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization;
using NUnit.Framework;

namespace MongoDB.UnitTests
Expand Down Expand Up @@ -124,9 +127,18 @@ public class TestOid
string hex = "4a7067c30a57000000008ecb";
Oid oid = new Oid(hex);

Assert.AreEqual("\"" + hex + "\"", oid.ToString());
Assert.AreEqual(hex, oid.ToString());
}


[Test]
public void TestFormatJ()
{
var hex = "4a7067c30a57000000008ecb";
var oid = new Oid(hex);

Assert.AreEqual("\"" + hex + "\"", oid.ToString("J"));
}

[Test]
public void TestEquals(){
string hex = "4a7067c30a57000000008ecb";
Expand Down Expand Up @@ -170,6 +182,21 @@ public class TestOid

Assert.AreEqual(firstOid.ToString(), secondOid.ToString());
}


[Test]
public void TestOidCanBeSerialized(){
var serializer = new BinaryFormatter();

var oidSource = Oid.NewOid();
Oid oidDesc;
using(var mem = new MemoryStream())
{
serializer.Serialize(mem, oidSource);
mem.Position = 0;
oidDesc = (Oid)serializer.Deserialize(mem);
}

Assert.AreEqual(oidSource,oidDesc);
}
}
}
59 changes: 55 additions & 4 deletions source/MongoDB/Oid.cs
Expand Up @@ -7,17 +7,28 @@ namespace MongoDB
/// <summary>
/// Oid is an immutable object that represents a Mongo ObjectId.
/// </summary>
public class Oid : IEquatable<Oid>, IComparable<Oid>
[Serializable]
public class Oid : IEquatable<Oid>, IComparable<Oid>, IFormattable
{
private static readonly OidGenerator OidGenerator = new OidGenerator();
private readonly byte[] bytes;
protected byte[] bytes;

/// <summary>
/// Initializes a new instance of the <see cref="Oid"/> class.
/// </summary>
protected Oid()
{
}

/// <summary>
/// Initializes a new instance of the <see cref = "Oid" /> class.
/// </summary>
/// <param name = "value">The value.</param>
public Oid(string value)
{
if(value == null)
throw new ArgumentNullException("value");

value = value.Replace("\"", "");
ValidateHex(value);
bytes = DecodeHex(value);
Expand All @@ -29,6 +40,9 @@ public Oid(string value)
/// <param name = "value">The value.</param>
public Oid(byte[] value)
{
if(value == null)
throw new ArgumentNullException("value");

bytes = new byte[12];
Array.Copy(value, bytes, 12);
}
Expand Down Expand Up @@ -122,7 +136,44 @@ public override int GetHashCode()
/// </returns>
public override string ToString()
{
return String.Format("\"{0}\"", BitConverter.ToString(bytes).Replace("-", "").ToLower());
return BitConverter.ToString(bytes).Replace("-", "").ToLower();
}

/// <summary>
/// Returns a <see cref="System.String"/> that represents this instance.
/// </summary>
/// <param name="format">The format.</param>
/// <returns>
/// A <see cref="System.String"/> that represents this instance.
/// </returns>
/// <remarks>
/// J = Returns Javascript string
/// </remarks>
public string ToString(string format)
{
if(string.IsNullOrEmpty(format))
return ToString();

if(format == "J")
return String.Format("\"{0}\"", ToString());

throw new ArgumentException("Invalid format string","format");
}

/// <summary>
/// Returns a <see cref="System.String"/> that represents this instance.
/// </summary>
/// <param name="format">The format.</param>
/// <param name="formatProvider">The format provider.</param>
/// <returns>
/// A <see cref="System.String"/> that represents this instance.
/// </returns>
/// <remarks>
/// J = Returns Javascript string
/// </remarks>
public string ToString(string format, IFormatProvider formatProvider)
{
return ToString(format);
}

/// <summary>
Expand Down Expand Up @@ -153,7 +204,7 @@ public static Oid NewOid()
/// <param name = "b">The b.</param>
/// <returns>The result of the operator.</returns>
public static bool operator ==(Oid a, Oid b){
if (System.Object.ReferenceEquals(a, b)){
if (ReferenceEquals(a, b)){
return true;
}
if((Object)a == null || (Object)b == null){
Expand Down
8 changes: 5 additions & 3 deletions source/MongoDB/Util/JsonFormatter.cs
Expand Up @@ -42,10 +42,12 @@ public class JsonFormatter
json.Append("null");
return;
}
if (value is bool) {
if (value is bool)
{
json.Append(((bool)value) ? "true" : "false");
} else if(value is Document ||
value is Oid ||
}else if(value is Oid) {
json.Append(((Oid)value).ToString("J"));
}else if(value is Document ||
value is Binary ||
value is DBRef ||
value is MongoMinKey ||
Expand Down

0 comments on commit 9a90085

Please sign in to comment.