-More BSON UUID bytes as Guid support
This commit is contained in:
@@ -1630,6 +1630,53 @@ namespace Newtonsoft.Json.Tests.Bson
|
|||||||
|
|
||||||
Assert.IsTrue(reader.Read());
|
Assert.IsTrue(reader.Read());
|
||||||
Assert.IsFalse(reader.Read());
|
Assert.IsFalse(reader.Read());
|
||||||
|
|
||||||
|
JsonSerializer serializer = new JsonSerializer();
|
||||||
|
ObjectTestClass b = serializer.Deserialize<ObjectTestClass>(new BsonReader(new MemoryStream(bytes)));
|
||||||
|
Assert.AreEqual(typeof(Guid), b.TheGuid.GetType());
|
||||||
|
Assert.AreEqual(g, (Guid)b.TheGuid);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GuidsShouldBeProperlyDeserialised_AsBytes()
|
||||||
|
{
|
||||||
|
Guid g = new Guid("822C0CE6-CC42-4753-A3C3-26F0684A4B88");
|
||||||
|
|
||||||
|
MemoryStream ms = new MemoryStream();
|
||||||
|
BsonWriter writer = new BsonWriter(ms);
|
||||||
|
writer.WriteStartObject();
|
||||||
|
writer.WritePropertyName("TheGuid");
|
||||||
|
writer.WriteValue(g);
|
||||||
|
writer.WriteEndObject();
|
||||||
|
writer.Flush();
|
||||||
|
|
||||||
|
byte[] bytes = ms.ToArray();
|
||||||
|
|
||||||
|
BsonReader reader = new BsonReader(new MemoryStream(bytes));
|
||||||
|
Assert.IsTrue(reader.Read());
|
||||||
|
Assert.IsTrue(reader.Read());
|
||||||
|
|
||||||
|
CollectionAssert.AreEquivalent(g.ToByteArray(), reader.ReadAsBytes());
|
||||||
|
Assert.AreEqual(JsonToken.Bytes, reader.TokenType);
|
||||||
|
Assert.AreEqual(typeof(byte[]), reader.ValueType);
|
||||||
|
CollectionAssert.AreEquivalent(g.ToByteArray(), (byte[])reader.Value);
|
||||||
|
|
||||||
|
Assert.IsTrue(reader.Read());
|
||||||
|
Assert.IsFalse(reader.Read());
|
||||||
|
|
||||||
|
JsonSerializer serializer = new JsonSerializer();
|
||||||
|
BytesTestClass b = serializer.Deserialize<BytesTestClass>(new BsonReader(new MemoryStream(bytes)));
|
||||||
|
CollectionAssert.AreEquivalent(g.ToByteArray(), b.TheGuid);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class BytesTestClass
|
||||||
|
{
|
||||||
|
public byte[] TheGuid { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class ObjectTestClass
|
||||||
|
{
|
||||||
|
public object TheGuid { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -513,7 +513,16 @@ namespace Newtonsoft.Json
|
|||||||
return null;
|
return null;
|
||||||
|
|
||||||
if (t == JsonToken.Bytes)
|
if (t == JsonToken.Bytes)
|
||||||
|
{
|
||||||
|
if (ValueType == typeof(Guid))
|
||||||
|
{
|
||||||
|
byte[] data = ((Guid)Value).ToByteArray();
|
||||||
|
SetToken(JsonToken.Bytes, data, false);
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
return (byte[])Value;
|
return (byte[])Value;
|
||||||
|
}
|
||||||
|
|
||||||
if (t == JsonToken.StartArray)
|
if (t == JsonToken.StartArray)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -440,6 +440,12 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
return ConvertResult.Success;
|
return ConvertResult.Success;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (initialValue is Guid && targetType == typeof(byte[]))
|
||||||
|
{
|
||||||
|
value = ((Guid)initialValue).ToByteArray();
|
||||||
|
return ConvertResult.Success;
|
||||||
|
}
|
||||||
|
|
||||||
if (initialValue is string)
|
if (initialValue is string)
|
||||||
{
|
{
|
||||||
if (targetType == typeof(Guid))
|
if (targetType == typeof(Guid))
|
||||||
|
|||||||
Reference in New Issue
Block a user