-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:
James Newton-King
2015-06-10 17:10:16 +12:00
parent 652d18746a
commit 9e29489e5a
5 changed files with 41 additions and 6 deletions
@@ -92,6 +92,21 @@ namespace Newtonsoft.Json.Tests
Assert.AreEqual(JsonToken.EndObject, jsonTextReader.TokenType); 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] [Test]
public void ReadSingleQuoteInsideDoubleQuoteString() public void ReadSingleQuoteInsideDoubleQuoteString()
@@ -54,6 +54,22 @@ namespace Newtonsoft.Json.Tests.Linq
[TestFixture] [TestFixture]
public class LinqToJsonTest : TestFixtureBase 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] [Test]
public void IncompleteContainers() public void IncompleteContainers()
{ {
+2 -1
View File
@@ -500,7 +500,8 @@ namespace Newtonsoft.Json
return data; 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) if (t == JsonToken.String)
{ {
string s = (string)Value; string s = (string)Value;
+5
View File
@@ -117,11 +117,16 @@ namespace Newtonsoft.Json
if (_readType == ReadType.ReadAsBytes) if (_readType == ReadType.ReadAsBytes)
{ {
Guid g;
byte[] data; byte[] data;
if (_stringReference.Length == 0) if (_stringReference.Length == 0)
{ {
data = new byte[0]; data = new byte[0];
} }
else if (_stringReference.Length == 36 && ConvertUtils.TryConvertGuid(_stringReference.ToString(), out g))
{
data = g.ToByteArray();
}
else else
{ {
data = Convert.FromBase64CharArray(_stringReference.Chars, _stringReference.StartIndex, _stringReference.Length); 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) public static bool TryConvertGuid(string s, out Guid g)
{ {
// GUID has to have format 00000000-0000-0000-0000-000000000000
#if NET20 || NET35 #if NET20 || NET35
if (s == null) if (s == null)
throw new ArgumentNullException("s"); throw new ArgumentNullException("s");
Regex format = new Regex( Regex format = new Regex("^[A-Fa-f0-9]{8}-([A-Fa-f0-9]{4}-){3}[A-Fa-f0-9]{12}$");
"^[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}(}})$");
Match match = format.Match(s); Match match = format.Match(s);
if (match.Success) if (match.Success)
{ {
@@ -794,7 +792,7 @@ namespace Newtonsoft.Json.Utilities
g = Guid.Empty; g = Guid.Empty;
return false; return false;
#else #else
return Guid.TryParse(s, out g); return Guid.TryParseExact(s, "D", out g);
#endif #endif
} }
} }