-Fixed ReadAsBytes with type name handling
This commit is contained in:
@@ -2785,6 +2785,13 @@ new Date()"));
|
|||||||
Assert.AreEqual(12.34d, reader.ReadAsDouble());
|
Assert.AreEqual(12.34d, reader.ReadAsDouble());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void ReadAsDouble_Hex()
|
||||||
|
{
|
||||||
|
JsonTextReader reader = new JsonTextReader(new StringReader("0XCAFEBABE"));
|
||||||
|
Assert.AreEqual(3405691582d, reader.ReadAsDouble());
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void ReadAsDouble_AllowThousands()
|
public void ReadAsDouble_AllowThousands()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -6969,6 +6969,70 @@ Path '', line 1, position 1.");
|
|||||||
Assert.AreEqual(d1[0], d2[0]);
|
Assert.AreEqual(d1[0], d2[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal class HasByteArray
|
||||||
|
{
|
||||||
|
public byte[] EncryptedPassword { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeserializeByteArrayWithTypeName()
|
||||||
|
{
|
||||||
|
string json = @"{
|
||||||
|
""$type"": ""Newtonsoft.Json.Tests.Serialization.JsonSerializerTest+HasByteArray, Newtonsoft.Json.Tests"",
|
||||||
|
""EncryptedPassword"": {
|
||||||
|
""$type"": ""System.Byte[], mscorlib"",
|
||||||
|
""$value"": ""cGFzc3dvcmQ=""
|
||||||
|
}
|
||||||
|
}";
|
||||||
|
HasByteArray value = JsonConvert.DeserializeObject<HasByteArray>(json, new JsonSerializerSettings
|
||||||
|
{
|
||||||
|
TypeNameHandling = TypeNameHandling.Objects
|
||||||
|
});
|
||||||
|
|
||||||
|
CollectionAssert.AreEquivalent(Convert.FromBase64String("cGFzc3dvcmQ="), value.EncryptedPassword);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeserializeByteArrayWithTypeName_BadAdditionalContent()
|
||||||
|
{
|
||||||
|
string json = @"{
|
||||||
|
""$type"": ""Newtonsoft.Json.Tests.Serialization.JsonSerializerTest+HasByteArray, Newtonsoft.Json.Tests"",
|
||||||
|
""EncryptedPassword"": {
|
||||||
|
""$type"": ""System.Byte[], mscorlib"",
|
||||||
|
""$value"": ""cGFzc3dvcmQ="",
|
||||||
|
""$value"": ""cGFzc3dvcmQ=""
|
||||||
|
}
|
||||||
|
}";
|
||||||
|
|
||||||
|
ExceptionAssert.Throws<JsonReaderException>(() =>
|
||||||
|
{
|
||||||
|
JsonConvert.DeserializeObject<HasByteArray>(json, new JsonSerializerSettings
|
||||||
|
{
|
||||||
|
TypeNameHandling = TypeNameHandling.Objects
|
||||||
|
});
|
||||||
|
}, "Error reading bytes. Unexpected token: PropertyName. Path 'EncryptedPassword.$value', line 6, position 13.");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeserializeByteArrayWithTypeName_ExtraProperty()
|
||||||
|
{
|
||||||
|
string json = @"{
|
||||||
|
""$type"": ""Newtonsoft.Json.Tests.Serialization.JsonSerializerTest+HasByteArray, Newtonsoft.Json.Tests"",
|
||||||
|
""EncryptedPassword"": {
|
||||||
|
""$type"": ""System.Byte[], mscorlib"",
|
||||||
|
""$value"": ""cGFzc3dvcmQ=""
|
||||||
|
},
|
||||||
|
""Pie"": null
|
||||||
|
}";
|
||||||
|
HasByteArray value = JsonConvert.DeserializeObject<HasByteArray>(json, new JsonSerializerSettings
|
||||||
|
{
|
||||||
|
TypeNameHandling = TypeNameHandling.Objects
|
||||||
|
});
|
||||||
|
|
||||||
|
Assert.IsNotNull(value.EncryptedPassword);
|
||||||
|
CollectionAssert.AreEquivalent(Convert.FromBase64String("cGFzc3dvcmQ="), value.EncryptedPassword);
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void SerializeInheritanceHierarchyWithDuplicateProperty()
|
public void SerializeInheritanceHierarchyWithDuplicateProperty()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -523,7 +523,13 @@ namespace Newtonsoft.Json
|
|||||||
ReadIntoWrappedTypeObject();
|
ReadIntoWrappedTypeObject();
|
||||||
|
|
||||||
byte[] data = ReadAsBytes();
|
byte[] data = ReadAsBytes();
|
||||||
Read();
|
ReaderReadAndAssert();
|
||||||
|
|
||||||
|
if (TokenType != JsonToken.EndObject)
|
||||||
|
{
|
||||||
|
throw JsonReaderException.Create(this, "Error reading bytes. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType));
|
||||||
|
}
|
||||||
|
|
||||||
SetToken(JsonToken.Bytes, data, false);
|
SetToken(JsonToken.Bytes, data, false);
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -449,6 +449,7 @@ namespace Newtonsoft.Json
|
|||||||
public override byte[] ReadAsBytes()
|
public override byte[] ReadAsBytes()
|
||||||
{
|
{
|
||||||
EnsureBuffer();
|
EnsureBuffer();
|
||||||
|
bool isWrappedArray = false;
|
||||||
|
|
||||||
switch (_currentState)
|
switch (_currentState)
|
||||||
{
|
{
|
||||||
@@ -475,11 +476,22 @@ namespace Newtonsoft.Json
|
|||||||
case '"':
|
case '"':
|
||||||
case '\'':
|
case '\'':
|
||||||
ParseString(currentChar, ReadType.ReadAsBytes);
|
ParseString(currentChar, ReadType.ReadAsBytes);
|
||||||
return (byte[])Value;
|
byte[] data = (byte[])Value;
|
||||||
|
if (isWrappedArray)
|
||||||
|
{
|
||||||
|
ReaderReadAndAssert();
|
||||||
|
if (TokenType != JsonToken.EndObject)
|
||||||
|
{
|
||||||
|
throw JsonReaderException.Create(this, "Error reading bytes. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType));
|
||||||
|
}
|
||||||
|
SetToken(JsonToken.Bytes, data, false);
|
||||||
|
}
|
||||||
|
return data;
|
||||||
case '{':
|
case '{':
|
||||||
_charPos++;
|
_charPos++;
|
||||||
SetToken(JsonToken.StartObject);
|
SetToken(JsonToken.StartObject);
|
||||||
ReadIntoWrappedTypeObject();
|
ReadIntoWrappedTypeObject();
|
||||||
|
isWrappedArray = true;
|
||||||
break;
|
break;
|
||||||
case '[':
|
case '[':
|
||||||
_charPos++;
|
_charPos++;
|
||||||
|
|||||||
Reference in New Issue
Block a user