-Fixed error when deserializing large JSON integers to XML
-Fixed serializing extension data properties with no setter
This commit is contained in:
@@ -2338,6 +2338,22 @@ namespace Newtonsoft.Json.Tests.Converters
|
|||||||
|
|
||||||
Assert.AreEqual(@"<root><b xmlns=""http://www.example.com/ns"">Asd</b><c>AAA</c><test>adad</test></root>", xmlBack.OuterXml);
|
Assert.AreEqual(@"<root><b xmlns=""http://www.example.com/ns"">Asd</b><c>AAA</c><test>adad</test></root>", xmlBack.OuterXml);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if !(NET20 || NET35 || PORTABLE || PORTABLE40)
|
||||||
|
[Test]
|
||||||
|
public void DeserializeBigInteger()
|
||||||
|
{
|
||||||
|
var json = "{\"DocumentId\":13779965364495889899 }";
|
||||||
|
|
||||||
|
XmlDocument node = JsonConvert.DeserializeXmlNode(json);
|
||||||
|
|
||||||
|
Assert.AreEqual("<DocumentId>13779965364495889899</DocumentId>", node.OuterXml);
|
||||||
|
|
||||||
|
string json2 = JsonConvert.SerializeXmlNode(node);
|
||||||
|
|
||||||
|
Assert.AreEqual(@"{""DocumentId"":""13779965364495889899""}", json2);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -48,7 +48,7 @@ namespace Newtonsoft.Json.Tests.Serialization
|
|||||||
public class CustomDictionary : IDictionary<string, object>
|
public class CustomDictionary : IDictionary<string, object>
|
||||||
{
|
{
|
||||||
private readonly IDictionary<string, object> _inner = new Dictionary<string, object>();
|
private readonly IDictionary<string, object> _inner = new Dictionary<string, object>();
|
||||||
|
|
||||||
public void Add(string key, object value)
|
public void Add(string key, object value)
|
||||||
{
|
{
|
||||||
_inner.Add(key, value);
|
_inner.Add(key, value);
|
||||||
@@ -235,7 +235,7 @@ namespace Newtonsoft.Json.Tests.Serialization
|
|||||||
}
|
}
|
||||||
|
|
||||||
#pragma warning disable 649
|
#pragma warning disable 649
|
||||||
class ExtendedObject
|
private class ExtendedObject
|
||||||
{
|
{
|
||||||
[JsonExtensionData]
|
[JsonExtensionData]
|
||||||
internal IDictionary<string, JToken> _additionalData;
|
internal IDictionary<string, JToken> _additionalData;
|
||||||
@@ -744,5 +744,90 @@ namespace Newtonsoft.Json.Tests.Serialization
|
|||||||
Assert.AreEqual(1, c.CustomFields.Count);
|
Assert.AreEqual(1, c.CustomFields.Count);
|
||||||
Assert.AreEqual("Testing", (string)c.CustomFields["CustomField1"]);
|
Assert.AreEqual("Testing", (string)c.CustomFields["CustomField1"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class DocNoSetter
|
||||||
|
{
|
||||||
|
private readonly JObject _content;
|
||||||
|
|
||||||
|
public DocNoSetter()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public DocNoSetter(JObject content)
|
||||||
|
{
|
||||||
|
_content = content;
|
||||||
|
}
|
||||||
|
|
||||||
|
[JsonProperty("_name")]
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
[JsonExtensionData]
|
||||||
|
public JObject Content
|
||||||
|
{
|
||||||
|
get { return _content; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void SerializeExtensionData_NoSetter()
|
||||||
|
{
|
||||||
|
string json = JsonConvert.SerializeObject(new DocNoSetter(new JObject(new JProperty("Property1", 123)))
|
||||||
|
{
|
||||||
|
Name = "documentName"
|
||||||
|
});
|
||||||
|
Assert.AreEqual(@"{""_name"":""documentName"",""Property1"":123}", json);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void SerializeExtensionData_NoSetterAndNoValue()
|
||||||
|
{
|
||||||
|
string json = JsonConvert.SerializeObject(new DocNoSetter(null)
|
||||||
|
{
|
||||||
|
Name = "documentName"
|
||||||
|
});
|
||||||
|
Assert.AreEqual(@"{""_name"":""documentName""}", json);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeserializeExtensionData_NoSetterAndNoExtensionData()
|
||||||
|
{
|
||||||
|
DocNoSetter doc = JsonConvert.DeserializeObject<DocNoSetter>(@"{""_name"":""documentName""}");
|
||||||
|
|
||||||
|
Assert.AreEqual("documentName", doc.Name);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeserializeExtensionData_NoSetterAndWithExtensionData()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
JsonConvert.DeserializeObject<DocNoSetter>(@"{""_name"":""documentName"",""Property1"":123}");
|
||||||
|
}
|
||||||
|
catch (JsonSerializationException ex)
|
||||||
|
{
|
||||||
|
Assert.AreEqual("Error setting value in extension data for type 'Newtonsoft.Json.Tests.Serialization.ExtensionDataTests+DocNoSetter'. Path 'Property1', line 1, position 39.", ex.Message);
|
||||||
|
Assert.AreEqual("Cannot set value onto extension data member 'Content'. The extension data collection is null and it cannot be set.", ex.InnerException.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class DocNoGetter
|
||||||
|
{
|
||||||
|
[JsonExtensionData]
|
||||||
|
public JObject Content
|
||||||
|
{
|
||||||
|
set { }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void SerializeExtensionData_NoGetter()
|
||||||
|
{
|
||||||
|
ExceptionAssert.Throws<JsonException>(
|
||||||
|
() =>
|
||||||
|
{
|
||||||
|
JsonConvert.SerializeObject(new DocNoGetter());
|
||||||
|
},
|
||||||
|
"Invalid extension data attribute on 'Newtonsoft.Json.Tests.Serialization.ExtensionDataTests+DocNoGetter'. Member 'Content' must have a getter.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -23,6 +23,7 @@
|
|||||||
// OTHER DEALINGS IN THE SOFTWARE.
|
// OTHER DEALINGS IN THE SOFTWARE.
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
using System.Numerics;
|
||||||
#if !PORTABLE40
|
#if !PORTABLE40
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@@ -1417,6 +1418,11 @@ namespace Newtonsoft.Json.Converters
|
|||||||
}
|
}
|
||||||
else if (reader.TokenType == JsonToken.Integer)
|
else if (reader.TokenType == JsonToken.Integer)
|
||||||
{
|
{
|
||||||
|
#if !(NET20 || NET35 || PORTABLE || PORTABLE40)
|
||||||
|
if (reader.Value is BigInteger)
|
||||||
|
return ((BigInteger)reader.Value).ToString(CultureInfo.InvariantCulture);
|
||||||
|
#endif
|
||||||
|
|
||||||
return XmlConvert.ToString(Convert.ToInt64(reader.Value, CultureInfo.InvariantCulture));
|
return XmlConvert.ToString(Convert.ToInt64(reader.Value, CultureInfo.InvariantCulture));
|
||||||
}
|
}
|
||||||
else if (reader.TokenType == JsonToken.Float)
|
else if (reader.TokenType == JsonToken.Float)
|
||||||
|
|||||||
@@ -25,6 +25,7 @@
|
|||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
|
using Newtonsoft.Json.Schema;
|
||||||
#if !(NET35 || NET20 || PORTABLE || PORTABLE40)
|
#if !(NET35 || NET20 || PORTABLE || PORTABLE40)
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
#endif
|
#endif
|
||||||
@@ -438,6 +439,9 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
if (!m.IsDefined(typeof(JsonExtensionDataAttribute), false))
|
if (!m.IsDefined(typeof(JsonExtensionDataAttribute), false))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
if (!ReflectionUtils.CanReadMemberValue(m, true))
|
||||||
|
throw new JsonException("Invalid extension data attribute on '{0}'. Member '{1}' must have a getter.".FormatWith(CultureInfo.InvariantCulture, GetClrTypeFullName(m.DeclaringType), m.Name));
|
||||||
|
|
||||||
Type t = ReflectionUtils.GetMemberUnderlyingType(m);
|
Type t = ReflectionUtils.GetMemberUnderlyingType(m);
|
||||||
|
|
||||||
Type dictionaryType;
|
Type dictionaryType;
|
||||||
@@ -479,10 +483,12 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
else
|
else
|
||||||
createdType = t;
|
createdType = t;
|
||||||
|
|
||||||
MethodInfo addMethod = t.GetMethod("Add", new[] { keyType, valueType });
|
|
||||||
Func<object, object> getExtensionDataDictionary = JsonTypeReflector.ReflectionDelegateFactory.CreateGet<object>(member);
|
Func<object, object> getExtensionDataDictionary = JsonTypeReflector.ReflectionDelegateFactory.CreateGet<object>(member);
|
||||||
Action<object, object> setExtensionDataDictionary = JsonTypeReflector.ReflectionDelegateFactory.CreateSet<object>(member);
|
Action<object, object> setExtensionDataDictionary = (ReflectionUtils.CanSetMemberValue(member, true, false))
|
||||||
|
? JsonTypeReflector.ReflectionDelegateFactory.CreateSet<object>(member)
|
||||||
|
: null;
|
||||||
Func<object> createExtensionDataDictionary = JsonTypeReflector.ReflectionDelegateFactory.CreateDefaultConstructor<object>(createdType);
|
Func<object> createExtensionDataDictionary = JsonTypeReflector.ReflectionDelegateFactory.CreateDefaultConstructor<object>(createdType);
|
||||||
|
MethodInfo addMethod = t.GetMethod("Add", new[] { keyType, valueType });
|
||||||
MethodCall<object, object> setExtensionDataDictionaryValue = JsonTypeReflector.ReflectionDelegateFactory.CreateMethodCall<object>(addMethod);
|
MethodCall<object, object> setExtensionDataDictionaryValue = JsonTypeReflector.ReflectionDelegateFactory.CreateMethodCall<object>(addMethod);
|
||||||
|
|
||||||
ExtensionDataSetter extensionDataSetter = (o, key, value) =>
|
ExtensionDataSetter extensionDataSetter = (o, key, value) =>
|
||||||
@@ -490,6 +496,9 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
object dictionary = getExtensionDataDictionary(o);
|
object dictionary = getExtensionDataDictionary(o);
|
||||||
if (dictionary == null)
|
if (dictionary == null)
|
||||||
{
|
{
|
||||||
|
if (setExtensionDataDictionary == null)
|
||||||
|
throw new JsonSerializationException("Cannot set value onto extension data member '{0}'. The extension data collection is null and it cannot be set.".FormatWith(CultureInfo.InvariantCulture, member.Name));
|
||||||
|
|
||||||
dictionary = createExtensionDataDictionary();
|
dictionary = createExtensionDataDictionary();
|
||||||
setExtensionDataDictionary(o, dictionary);
|
setExtensionDataDictionary(o, dictionary);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user