-Added support for converting byte array JValues to Guid

-Added support for deserializing byte arrays to Guid
-Changed Guids to be serialized as binary UUID in BSON
-Changed JsonNet35BinaryCompatibility on BsonReader to be obsolete
This commit is contained in:
James Newton-King
2013-04-07 15:07:35 +12:00
parent 1abcfb1ace
commit 341656e7eb
11 changed files with 130 additions and 18 deletions
+8 -2
View File
@@ -69,7 +69,7 @@ namespace Newtonsoft.Json.Linq
private static readonly JTokenType[] NumberTypes = new[] { JTokenType.Integer, JTokenType.Float, JTokenType.String, JTokenType.Comment, JTokenType.Raw, JTokenType.Boolean };
private static readonly JTokenType[] BigIntegerTypes = new[] { JTokenType.Integer, JTokenType.Float, JTokenType.String, JTokenType.Comment, JTokenType.Raw, JTokenType.Boolean, JTokenType.Bytes };
private static readonly JTokenType[] StringTypes = new[] { JTokenType.Date, JTokenType.Integer, JTokenType.Float, JTokenType.String, JTokenType.Comment, JTokenType.Raw, JTokenType.Boolean, JTokenType.Bytes, JTokenType.Guid, JTokenType.TimeSpan, JTokenType.Uri };
private static readonly JTokenType[] GuidTypes = new[] { JTokenType.String, JTokenType.Comment, JTokenType.Raw, JTokenType.Guid };
private static readonly JTokenType[] GuidTypes = new[] { JTokenType.String, JTokenType.Comment, JTokenType.Raw, JTokenType.Guid, JTokenType.Bytes };
private static readonly JTokenType[] TimeSpanTypes = new[] { JTokenType.String, JTokenType.Comment, JTokenType.Raw, JTokenType.TimeSpan };
private static readonly JTokenType[] UriTypes = new[] { JTokenType.String, JTokenType.Comment, JTokenType.Raw, JTokenType.Uri };
private static readonly JTokenType[] CharTypes = new[] { JTokenType.Integer, JTokenType.Float, JTokenType.String, JTokenType.Comment, JTokenType.Raw };
@@ -1093,7 +1093,10 @@ namespace Newtonsoft.Json.Linq
if (v == null || !ValidateToken(v, GuidTypes, false))
throw new ArgumentException("Can not convert {0} to Guid.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
return (v.Value is Guid) ? (Guid)v.Value : new Guid(Convert.ToString(v.Value, CultureInfo.InvariantCulture));
if (v.Value is byte[])
return new Guid((byte[]) v.Value);
return (v.Value is Guid) ? (Guid) v.Value : new Guid(Convert.ToString(v.Value, CultureInfo.InvariantCulture));
}
/// <summary>
@@ -1113,6 +1116,9 @@ namespace Newtonsoft.Json.Linq
if (v.Value == null)
return null;
if (v.Value is byte[])
return new Guid((byte[])v.Value);
return (v.Value is Guid) ? (Guid)v.Value : new Guid(Convert.ToString(v.Value, CultureInfo.InvariantCulture));
}