-Added support for reading GUID strings as bytes in JsonTextReader
-Changed reading GUIDs as bytes to only support 00000000-0000-0000-0000-000000000000 format
This commit is contained in:
@@ -92,6 +92,21 @@ namespace Newtonsoft.Json.Tests
|
||||
Assert.AreEqual(JsonToken.EndObject, jsonTextReader.TokenType);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReadAsBytes_Base64AndGuid()
|
||||
{
|
||||
JsonTextReader jsonTextReader = new JsonTextReader(new StringReader("'AAAAAAAAAAAAAAAAAAAAAAAAAAABAAAA'"));
|
||||
byte[] data = jsonTextReader.ReadAsBytes();
|
||||
byte[] expected = Convert.FromBase64String("AAAAAAAAAAAAAAAAAAAAAAAAAAABAAAA");
|
||||
|
||||
CollectionAssert.AreEqual(expected, data);
|
||||
|
||||
jsonTextReader = new JsonTextReader(new StringReader("'AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAABAAAA'"));
|
||||
data = jsonTextReader.ReadAsBytes();
|
||||
expected = Guid.Parse("AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAABAAAA").ToByteArray();
|
||||
|
||||
CollectionAssert.AreEqual(expected, data);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReadSingleQuoteInsideDoubleQuoteString()
|
||||
|
||||
@@ -54,6 +54,22 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
[TestFixture]
|
||||
public class LinqToJsonTest : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void ToObject_Base64AndGuid()
|
||||
{
|
||||
JObject o = JObject.Parse("{'responseArray':'AAAAAAAAAAAAAAAAAAAAAAAAAAABAAAA'}");
|
||||
byte[] data = o["responseArray"].ToObject<byte[]>();
|
||||
byte[] expected = Convert.FromBase64String("AAAAAAAAAAAAAAAAAAAAAAAAAAABAAAA");
|
||||
|
||||
CollectionAssert.AreEqual(expected, data);
|
||||
|
||||
o = JObject.Parse("{'responseArray':'AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAABAAAA'}");
|
||||
data = o["responseArray"].ToObject<byte[]>();
|
||||
expected = Guid.Parse("AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAABAAAA").ToByteArray();
|
||||
|
||||
CollectionAssert.AreEqual(expected, data);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IncompleteContainers()
|
||||
{
|
||||
|
||||
@@ -500,7 +500,8 @@ namespace Newtonsoft.Json
|
||||
return data;
|
||||
}
|
||||
|
||||
// attempt to convert possible base 64 string to bytes
|
||||
// attempt to convert possible base 64 or GUID string to bytes
|
||||
// GUID has to have format 00000000-0000-0000-0000-000000000000
|
||||
if (t == JsonToken.String)
|
||||
{
|
||||
string s = (string)Value;
|
||||
|
||||
@@ -117,11 +117,16 @@ namespace Newtonsoft.Json
|
||||
|
||||
if (_readType == ReadType.ReadAsBytes)
|
||||
{
|
||||
Guid g;
|
||||
byte[] data;
|
||||
if (_stringReference.Length == 0)
|
||||
{
|
||||
data = new byte[0];
|
||||
}
|
||||
else if (_stringReference.Length == 36 && ConvertUtils.TryConvertGuid(_stringReference.ToString(), out g))
|
||||
{
|
||||
data = g.ToByteArray();
|
||||
}
|
||||
else
|
||||
{
|
||||
data = Convert.FromBase64CharArray(_stringReference.Chars, _stringReference.StartIndex, _stringReference.Length);
|
||||
|
||||
@@ -776,14 +776,12 @@ namespace Newtonsoft.Json.Utilities
|
||||
|
||||
public static bool TryConvertGuid(string s, out Guid g)
|
||||
{
|
||||
// GUID has to have format 00000000-0000-0000-0000-000000000000
|
||||
#if NET20 || NET35
|
||||
if (s == null)
|
||||
throw new ArgumentNullException("s");
|
||||
|
||||
Regex format = new Regex(
|
||||
"^[A-Fa-f0-9]{32}$|" +
|
||||
"^({|\\()?[A-Fa-f0-9]{8}-([A-Fa-f0-9]{4}-){3}[A-Fa-f0-9]{12}(}|\\))?$|" +
|
||||
"^({)?[0xA-Fa-f0-9]{3,10}(, {0,1}[0xA-Fa-f0-9]{3,6}){2}, {0,1}({)([0xA-Fa-f0-9]{3,4}, {0,1}){7}[0xA-Fa-f0-9]{3,4}(}})$");
|
||||
Regex format = new Regex("^[A-Fa-f0-9]{8}-([A-Fa-f0-9]{4}-){3}[A-Fa-f0-9]{12}$");
|
||||
Match match = format.Match(s);
|
||||
if (match.Success)
|
||||
{
|
||||
@@ -794,7 +792,7 @@ namespace Newtonsoft.Json.Utilities
|
||||
g = Guid.Empty;
|
||||
return false;
|
||||
#else
|
||||
return Guid.TryParse(s, out g);
|
||||
return Guid.TryParseExact(s, "D", out g);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user