-Added support for enum values to LINQ to JSON
-Added TypeNameAssemblyFormat property to JsonSerializer. Property controls whether .NET type names are written with a full or partial assembly name -Added TypeNameHandling to JsonProperty attribute. Allows a type names to be included on an individual property basis -Fixed serializing HashSet<T> -Additional permission demands added to DynamicCodeGeneration flag
This commit is contained in:
@@ -243,6 +243,8 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
|
||||
byte[] data = new byte[0];
|
||||
Assert.AreEqual(data, (byte[])(new JValue(data)));
|
||||
|
||||
Assert.AreEqual(5, (int)(new JValue(StringComparison.OrdinalIgnoreCase)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -66,6 +66,10 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
|
||||
Assert.AreEqual(data, v.Value);
|
||||
Assert.AreEqual(JTokenType.Bytes, v.Type);
|
||||
|
||||
v.Value = StringComparison.OrdinalIgnoreCase;
|
||||
Assert.AreEqual(StringComparison.OrdinalIgnoreCase, v.Value);
|
||||
Assert.AreEqual(JTokenType.Integer, v.Type);
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<OutputPath>bin\Debug\Compact\</OutputPath>
|
||||
<DefineConstants>TRACE;DEBUG;PocketPC</DefineConstants>
|
||||
<NoStdLib>true</NoStdLib>
|
||||
<NoConfig>true</NoConfig>
|
||||
@@ -35,7 +35,7 @@
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<OutputPath>bin\Release\Compact\</OutputPath>
|
||||
<DefineConstants>TRACE;$(PlatformFamilyName)</DefineConstants>
|
||||
<NoStdLib>true</NoStdLib>
|
||||
<NoConfig>true</NoConfig>
|
||||
|
||||
@@ -33,7 +33,7 @@
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<OutputPath>bin\Debug\DotNet20\</OutputPath>
|
||||
<DefineConstants>TRACE;DEBUG;NET20</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
@@ -41,7 +41,7 @@
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<OutputPath>bin\Release\DotNet20\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>Bin\Debug</OutputPath>
|
||||
<OutputPath>Bin\Debug\Silverlight\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE;SILVERLIGHT</DefineConstants>
|
||||
<NoStdLib>true</NoStdLib>
|
||||
<NoConfig>true</NoConfig>
|
||||
@@ -32,7 +32,7 @@
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>Bin\Release</OutputPath>
|
||||
<OutputPath>Bin\Release\Silverlight\</OutputPath>
|
||||
<DefineConstants>TRACE;SILVERLIGHT</DefineConstants>
|
||||
<NoStdLib>true</NoStdLib>
|
||||
<NoConfig>true</NoConfig>
|
||||
|
||||
@@ -31,7 +31,7 @@
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<OutputPath>bin\Debug\DotNet\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
@@ -39,7 +39,7 @@
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<OutputPath>bin\Release\DotNet\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
|
||||
@@ -33,8 +33,8 @@ namespace Newtonsoft.Json.Tests
|
||||
|
||||
public class PerformanceTests : TestFixtureBase
|
||||
{
|
||||
private const int Iterations = 100;
|
||||
//private const int Iterations = 5000;
|
||||
//private const int Iterations = 100;
|
||||
private const int Iterations = 5000;
|
||||
|
||||
#region Data
|
||||
private const string BsonHex =
|
||||
@@ -472,6 +472,39 @@ namespace Newtonsoft.Json.Tests
|
||||
return (T)formatter.Deserialize(new MemoryStream(bytes));
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
[Test]
|
||||
public void SerializeLargeObject()
|
||||
{
|
||||
LargeRecursiveTestClass rootValue = null;
|
||||
LargeRecursiveTestClass parentValue = null;
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
LargeRecursiveTestClass currentValue = new LargeRecursiveTestClass()
|
||||
{
|
||||
Integer = int.MaxValue,
|
||||
Text = "The quick red fox jumped over the lazy dog."
|
||||
};
|
||||
|
||||
if (rootValue == null)
|
||||
rootValue = currentValue;
|
||||
if (parentValue != null)
|
||||
parentValue.Child = currentValue;
|
||||
|
||||
parentValue = currentValue;
|
||||
}
|
||||
|
||||
BenchmarkSerializeMethod(SerializeMethod.JsonNetBinary, rootValue);
|
||||
}
|
||||
}
|
||||
|
||||
public class LargeRecursiveTestClass
|
||||
{
|
||||
public LargeRecursiveTestClass Child { get; set; }
|
||||
public string Text { get; set; }
|
||||
public int Integer { get; set; }
|
||||
}
|
||||
|
||||
#region Classes
|
||||
|
||||
@@ -3372,5 +3372,31 @@ keyword such as type of business.""
|
||||
Assert.AreEqual(4.3, d[1]);
|
||||
Assert.AreEqual(null, d[2]);
|
||||
}
|
||||
|
||||
#if !SILVERLIGHT && !NET20 && !PocketPC
|
||||
[Test]
|
||||
public void SerializeHashSet()
|
||||
{
|
||||
string jsonText = JsonConvert.SerializeObject(new HashSet<string>()
|
||||
{
|
||||
"One",
|
||||
"2",
|
||||
"III"
|
||||
}, Formatting.Indented);
|
||||
|
||||
Assert.AreEqual(@"[
|
||||
""One"",
|
||||
""2"",
|
||||
""III""
|
||||
]", jsonText);
|
||||
|
||||
HashSet<string> d = JsonConvert.DeserializeObject<HashSet<string>>(jsonText);
|
||||
|
||||
Assert.AreEqual(3, d.Count);
|
||||
Assert.IsTrue(d.Contains("One"));
|
||||
Assert.IsTrue(d.Contains("2"));
|
||||
Assert.IsTrue(d.Contains("III"));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -26,10 +26,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.Serialization.Formatters;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Tests.TestObjects;
|
||||
using NUnit.Framework;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
using System.Net;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Serialization
|
||||
{
|
||||
@@ -38,7 +41,7 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
[Test]
|
||||
public void WriteTypeNameForObjects()
|
||||
{
|
||||
string employeeRef = typeof(EmployeeReference).AssemblyQualifiedName;
|
||||
string employeeRef = ReflectionUtils.GetTypeName(typeof(EmployeeReference), FormatterAssemblyStyle.Simple);
|
||||
|
||||
EmployeeReference employee = new EmployeeReference();
|
||||
|
||||
@@ -58,7 +61,7 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
[Test]
|
||||
public void DeserializeTypeName()
|
||||
{
|
||||
string employeeRef = typeof(EmployeeReference).AssemblyQualifiedName;
|
||||
string employeeRef = ReflectionUtils.GetTypeName(typeof(EmployeeReference), FormatterAssemblyStyle.Simple);
|
||||
|
||||
string json = @"{
|
||||
""$id"": ""1"",
|
||||
@@ -76,6 +79,26 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
Assert.AreEqual("Name!", ((EmployeeReference)employee).Name);
|
||||
}
|
||||
|
||||
#if !SILVERLIGHT && !PocketPC
|
||||
[Test]
|
||||
public void DeserializeTypeNameFromGacAssembly()
|
||||
{
|
||||
string cookieRef = ReflectionUtils.GetTypeName(typeof(Cookie), FormatterAssemblyStyle.Simple);
|
||||
|
||||
string json = @"{
|
||||
""$id"": ""1"",
|
||||
""$type"": """ + cookieRef + @"""
|
||||
}";
|
||||
|
||||
object cookie = JsonConvert.DeserializeObject(json, null, new JsonSerializerSettings
|
||||
{
|
||||
TypeNameHandling = TypeNameHandling.Objects
|
||||
});
|
||||
|
||||
Assert.IsInstanceOfType(typeof(Cookie), cookie);
|
||||
}
|
||||
#endif
|
||||
|
||||
[Test]
|
||||
public void SerializeGenericObjectListWithTypeName()
|
||||
{
|
||||
@@ -101,7 +124,8 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
|
||||
string json = JsonConvert.SerializeObject(values, Formatting.Indented, new JsonSerializerSettings
|
||||
{
|
||||
TypeNameHandling = TypeNameHandling.Objects
|
||||
TypeNameHandling = TypeNameHandling.Objects,
|
||||
TypeNameAssemblyFormat = FormatterAssemblyStyle.Full
|
||||
});
|
||||
|
||||
Assert.AreEqual(@"[
|
||||
@@ -157,7 +181,8 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
|
||||
List<object> values = (List<object>)JsonConvert.DeserializeObject(json, typeof(List<object>), new JsonSerializerSettings
|
||||
{
|
||||
TypeNameHandling = TypeNameHandling.Objects
|
||||
TypeNameHandling = TypeNameHandling.Objects,
|
||||
TypeNameAssemblyFormat = FormatterAssemblyStyle.Full
|
||||
});
|
||||
|
||||
Assert.AreEqual(4, values.Count);
|
||||
@@ -191,7 +216,8 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
|
||||
JsonConvert.DeserializeObject(json, typeof(Person), new JsonSerializerSettings
|
||||
{
|
||||
TypeNameHandling = TypeNameHandling.Objects
|
||||
TypeNameHandling = TypeNameHandling.Objects,
|
||||
TypeNameAssemblyFormat = FormatterAssemblyStyle.Full
|
||||
});
|
||||
}
|
||||
|
||||
@@ -275,7 +301,8 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
|
||||
ICorrelatedMessage message = JsonConvert.DeserializeObject<ICorrelatedMessage>(json, new JsonSerializerSettings
|
||||
{
|
||||
TypeNameHandling = TypeNameHandling.Objects
|
||||
TypeNameHandling = TypeNameHandling.Objects,
|
||||
TypeNameAssemblyFormat = FormatterAssemblyStyle.Full
|
||||
});
|
||||
|
||||
Assert.IsInstanceOfType(typeof(SendHttpRequest), message);
|
||||
@@ -302,7 +329,8 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
new JsonSerializerSettings
|
||||
{
|
||||
NullValueHandling = NullValueHandling.Ignore,
|
||||
TypeNameHandling = TypeNameHandling.All
|
||||
TypeNameHandling = TypeNameHandling.All,
|
||||
TypeNameAssemblyFormat = FormatterAssemblyStyle.Full
|
||||
});
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
@@ -317,5 +345,86 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
}
|
||||
}", json);
|
||||
}
|
||||
|
||||
public class TypeNameProperty
|
||||
{
|
||||
public string Name { get; set; }
|
||||
[JsonProperty(TypeNameHandling = TypeNameHandling.All)]
|
||||
public object Value { get; set; }
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteObjectTypeNameForProperty()
|
||||
{
|
||||
string typeNamePropertyRef = ReflectionUtils.GetTypeName(typeof(TypeNameProperty), FormatterAssemblyStyle.Simple);
|
||||
|
||||
TypeNameProperty typeNameProperty = new TypeNameProperty
|
||||
{
|
||||
Name = "Name!",
|
||||
Value = new TypeNameProperty
|
||||
{
|
||||
Name = "Nested!"
|
||||
}
|
||||
};
|
||||
|
||||
string json = JsonConvert.SerializeObject(typeNameProperty, Formatting.Indented);
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""Name"": ""Name!"",
|
||||
""Value"": {
|
||||
""$type"": """ + typeNamePropertyRef + @""",
|
||||
""Name"": ""Nested!"",
|
||||
""Value"": null
|
||||
}
|
||||
}", json);
|
||||
|
||||
TypeNameProperty deserialized = JsonConvert.DeserializeObject<TypeNameProperty>(json);
|
||||
Assert.AreEqual("Name!", deserialized.Name);
|
||||
Assert.IsInstanceOfType(typeof(TypeNameProperty), deserialized.Value);
|
||||
|
||||
TypeNameProperty nested = (TypeNameProperty)deserialized.Value;
|
||||
Assert.AreEqual("Nested!", nested.Name);
|
||||
Assert.AreEqual(null, nested.Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteListTypeNameForProperty()
|
||||
{
|
||||
string listRef = ReflectionUtils.GetTypeName(typeof(List<int>), FormatterAssemblyStyle.Simple);
|
||||
|
||||
TypeNameProperty typeNameProperty = new TypeNameProperty
|
||||
{
|
||||
Name = "Name!",
|
||||
Value = new List<int> { 1, 2, 3, 4, 5 }
|
||||
};
|
||||
|
||||
string json = JsonConvert.SerializeObject(typeNameProperty, Formatting.Indented);
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""Name"": ""Name!"",
|
||||
""Value"": {
|
||||
""$type"": """ + listRef + @""",
|
||||
""$values"": [
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
4,
|
||||
5
|
||||
]
|
||||
}
|
||||
}", json);
|
||||
|
||||
TypeNameProperty deserialized = JsonConvert.DeserializeObject<TypeNameProperty>(json);
|
||||
Assert.AreEqual("Name!", deserialized.Name);
|
||||
Assert.IsInstanceOfType(typeof(List<int>), deserialized.Value);
|
||||
|
||||
List<int> nested = (List<int>)deserialized.Value;
|
||||
Assert.AreEqual(5, nested.Count);
|
||||
Assert.AreEqual(1, nested[0]);
|
||||
Assert.AreEqual(2, nested[1]);
|
||||
Assert.AreEqual(3, nested[2]);
|
||||
Assert.AreEqual(4, nested[3]);
|
||||
Assert.AreEqual(5, nested[4]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -48,6 +48,7 @@ namespace Newtonsoft.Json.Converters
|
||||
/// </summary>
|
||||
/// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
|
||||
/// <param name="objectType">Type of the object.</param>
|
||||
/// <param name="existingValue">The existing property value of the JSON that is being converted.</param>
|
||||
/// <param name="serializer">The calling serializer.</param>
|
||||
/// <returns>The object value.</returns>
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
#if SILVERLIGHT || PocketPC
|
||||
namespace System.Runtime.Serialization.Formatters
|
||||
{
|
||||
/// <summary>
|
||||
/// Indicates the method that will be used during deserialization for locating and loading assemblies.
|
||||
/// </summary>
|
||||
public enum FormatterAssemblyStyle
|
||||
{
|
||||
/// <summary>
|
||||
/// In simple mode, the assembly used during deserialization need not match exactly the assembly used during serialization. Specifically, the version numbers need not match as the LoadWithPartialName method is used to load the assembly.
|
||||
/// </summary>
|
||||
Simple,
|
||||
/// <summary>
|
||||
/// In full mode, the assembly used during deserialization must match exactly the assembly used during serialization. The Load method of the Assembly class is used to load the assembly.
|
||||
/// </summary>
|
||||
Full
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -14,6 +14,7 @@ namespace Newtonsoft.Json
|
||||
internal DefaultValueHandling? _defaultValueHandling;
|
||||
internal ReferenceLoopHandling? _referenceLoopHandling;
|
||||
internal ObjectCreationHandling? _objectCreationHandling;
|
||||
internal TypeNameHandling? _typeNameHandling;
|
||||
internal bool? _isReference;
|
||||
|
||||
/// <summary>
|
||||
@@ -56,6 +57,12 @@ namespace Newtonsoft.Json
|
||||
set { _objectCreationHandling = value; }
|
||||
}
|
||||
|
||||
public TypeNameHandling TypeNameHandling
|
||||
{
|
||||
get { return _typeNameHandling ?? default(TypeNameHandling); }
|
||||
set { _typeNameHandling = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets whether this property's value is serialized as a reference.
|
||||
/// </summary>
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Runtime.Serialization.Formatters;
|
||||
using Newtonsoft.Json.Converters;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
@@ -42,6 +43,7 @@ namespace Newtonsoft.Json
|
||||
{
|
||||
#region Properties
|
||||
private TypeNameHandling _typeNameHandling;
|
||||
private FormatterAssemblyStyle _typeNameAssemblyFormat;
|
||||
private PreserveReferencesHandling _preserveReferencesHandling;
|
||||
private ReferenceLoopHandling _referenceLoopHandling;
|
||||
private MissingMemberHandling _missingMemberHandling;
|
||||
@@ -114,6 +116,22 @@ namespace Newtonsoft.Json
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets how a type name assembly is written and resolved by the serializer.
|
||||
/// </summary>
|
||||
/// <value>The type name assembly format.</value>
|
||||
public virtual FormatterAssemblyStyle TypeNameAssemblyFormat
|
||||
{
|
||||
get { return _typeNameAssemblyFormat; }
|
||||
set
|
||||
{
|
||||
if (value < FormatterAssemblyStyle.Simple || value > FormatterAssemblyStyle.Full)
|
||||
throw new ArgumentOutOfRangeException("value");
|
||||
|
||||
_typeNameAssemblyFormat = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets how object references are preserved by the serializer.
|
||||
/// </summary>
|
||||
@@ -296,6 +314,7 @@ namespace Newtonsoft.Json
|
||||
jsonSerializer.Converters.AddRange(settings.Converters);
|
||||
|
||||
jsonSerializer.TypeNameHandling = settings.TypeNameHandling;
|
||||
jsonSerializer.TypeNameAssemblyFormat = settings.TypeNameAssemblyFormat;
|
||||
jsonSerializer.PreserveReferencesHandling = settings.PreserveReferencesHandling;
|
||||
jsonSerializer.ReferenceLoopHandling = settings.ReferenceLoopHandling;
|
||||
jsonSerializer.MissingMemberHandling = settings.MissingMemberHandling;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.Serialization.Formatters;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
@@ -21,6 +22,7 @@ namespace Newtonsoft.Json
|
||||
internal const PreserveReferencesHandling DefaultPreserveReferencesHandling = PreserveReferencesHandling.None;
|
||||
internal const ConstructorHandling DefaultConstructorHandling = ConstructorHandling.Default;
|
||||
internal const TypeNameHandling DefaultTypeNameHandling = TypeNameHandling.None;
|
||||
internal const FormatterAssemblyStyle DefaultTypeNameAssemblyFormat = FormatterAssemblyStyle.Simple;
|
||||
internal static readonly StreamingContext DefaultContext = new StreamingContext();
|
||||
|
||||
/// <summary>
|
||||
@@ -28,41 +30,55 @@ namespace Newtonsoft.Json
|
||||
/// </summary>
|
||||
/// <value>Reference loop handling.</value>
|
||||
public ReferenceLoopHandling ReferenceLoopHandling { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets how missing members (e.g. JSON contains a property that isn't a member on the object) are handled during deserialization.
|
||||
/// </summary>
|
||||
/// <value>Missing member handling.</value>
|
||||
public MissingMemberHandling MissingMemberHandling { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets how objects are created during deserialization.
|
||||
/// </summary>
|
||||
/// <value>The object creation handling.</value>
|
||||
public ObjectCreationHandling ObjectCreationHandling { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets how null values are handled during serialization and deserialization.
|
||||
/// </summary>
|
||||
/// <value>Null value handling.</value>
|
||||
public NullValueHandling NullValueHandling { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets how null default are handled during serialization and deserialization.
|
||||
/// </summary>
|
||||
/// <value>The default value handling.</value>
|
||||
public DefaultValueHandling DefaultValueHandling { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a collection <see cref="JsonConverter"/> that will be used during serialization.
|
||||
/// </summary>
|
||||
/// <value>The converters.</value>
|
||||
public IList<JsonConverter> Converters { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets how object references are preserved by the serializer.
|
||||
/// </summary>
|
||||
/// <value>The preserve references handling.</value>
|
||||
public PreserveReferencesHandling PreserveReferencesHandling { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets how type name writing and reading is handled by the serializer.
|
||||
/// </summary>
|
||||
/// <value>The type name handling.</value>
|
||||
public TypeNameHandling TypeNameHandling { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets how a type name assembly is written and resolved by the serializer.
|
||||
/// </summary>
|
||||
/// <value>The type name assembly format.</value>
|
||||
public FormatterAssemblyStyle TypeNameAssemblyFormat { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets how constructors are used during deserialization.
|
||||
/// </summary>
|
||||
@@ -75,11 +91,13 @@ namespace Newtonsoft.Json
|
||||
/// </summary>
|
||||
/// <value>The contract resolver.</value>
|
||||
public IContractResolver ContractResolver { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the <see cref="IReferenceResolver"/> used by the serializer when resolving references.
|
||||
/// </summary>
|
||||
/// <value>The reference resolver.</value>
|
||||
public IReferenceResolver ReferenceResolver { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the <see cref="SerializationBinder"/> used by the serializer when resolving type names.
|
||||
/// </summary>
|
||||
@@ -110,6 +128,7 @@ namespace Newtonsoft.Json
|
||||
DefaultValueHandling = DefaultDefaultValueHandling;
|
||||
PreserveReferencesHandling = DefaultPreserveReferencesHandling;
|
||||
TypeNameHandling = DefaultTypeNameHandling;
|
||||
TypeNameAssemblyFormat = DefaultTypeNameAssemblyFormat;
|
||||
Context = DefaultContext;
|
||||
Converters = new List<JsonConverter>();
|
||||
}
|
||||
|
||||
@@ -209,6 +209,8 @@ namespace Newtonsoft.Json.Linq
|
||||
else if (value is long || value is int || value is short || value is sbyte
|
||||
|| value is ulong || value is uint || value is ushort || value is byte)
|
||||
return JTokenType.Integer;
|
||||
else if (value is Enum)
|
||||
return JTokenType.Integer;
|
||||
else if (value is double || value is float || value is decimal)
|
||||
return JTokenType.Float;
|
||||
else if (value is DateTime)
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<OutputPath>bin\Debug\Compact\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE;$(PlatformFamilyName)</DefineConstants>
|
||||
<NoStdLib>true</NoStdLib>
|
||||
<NoConfig>true</NoConfig>
|
||||
@@ -37,7 +37,7 @@
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<OutputPath>bin\Release\Compact\</OutputPath>
|
||||
<DefineConstants>TRACE;$(PlatformFamilyName)</DefineConstants>
|
||||
<NoStdLib>true</NoStdLib>
|
||||
<NoConfig>true</NoConfig>
|
||||
@@ -45,7 +45,7 @@
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
|
||||
<DocumentationFile>bin\Release\Newtonsoft.Json.Compact.xml</DocumentationFile>
|
||||
<DocumentationFile>bin\Release\Compact\Newtonsoft.Json.Compact.xml</DocumentationFile>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="mscorlib" />
|
||||
@@ -75,6 +75,7 @@
|
||||
<Compile Include="Converters\KeyValuePairConverter.cs" />
|
||||
<Compile Include="Converters\RegexConverter.cs" />
|
||||
<Compile Include="Converters\StringEnumConverter.cs" />
|
||||
<Compile Include="FormatterAssemblyStyle.cs" />
|
||||
<Compile Include="IJsonLineInfo.cs" />
|
||||
<Compile Include="JsonArrayAttribute.cs" />
|
||||
<Compile Include="JsonContainerAttribute.cs" />
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -24,24 +24,24 @@
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>Bin\Debug</OutputPath>
|
||||
<OutputPath>Bin\Debug\Silverlight\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE;SILVERLIGHT</DefineConstants>
|
||||
<NoStdLib>true</NoStdLib>
|
||||
<NoConfig>true</NoConfig>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<DocumentationFile>Bin\Debug\Newtonsoft.Json.Silverlight.xml</DocumentationFile>
|
||||
<DocumentationFile>Bin\Debug\Silverlight\Newtonsoft.Json.Silverlight.xml</DocumentationFile>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>Bin\Release</OutputPath>
|
||||
<OutputPath>Bin\Release\Silverlight\</OutputPath>
|
||||
<DefineConstants>TRACE;SILVERLIGHT</DefineConstants>
|
||||
<NoStdLib>true</NoStdLib>
|
||||
<NoConfig>true</NoConfig>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<DocumentationFile>Bin\Release\Newtonsoft.Json.Silverlight.xml</DocumentationFile>
|
||||
<DocumentationFile>Bin\Release\Silverlight\Newtonsoft.Json.Silverlight.xml</DocumentationFile>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System.Runtime.Serialization" />
|
||||
@@ -77,6 +77,7 @@
|
||||
<Compile Include="Converters\StringEnumConverter.cs" />
|
||||
<Compile Include="Converters\XmlNodeConverter.cs" />
|
||||
<Compile Include="Converters\HtmlColorConverter.cs" />
|
||||
<Compile Include="FormatterAssemblyStyle.cs" />
|
||||
<Compile Include="DefaultValueHandling.cs" />
|
||||
<Compile Include="IJsonLineInfo.cs" />
|
||||
<Compile Include="JsonArrayAttribute.cs" />
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -460,22 +460,20 @@ namespace Newtonsoft.Json.Serialization
|
||||
if (objectType.IsSubclassOf(typeof(JToken)))
|
||||
return CreateLinqContract(objectType);
|
||||
|
||||
bool canConvertToString = CanConvertToString(objectType);
|
||||
|
||||
#if !SILVERLIGHT && !PocketPC
|
||||
if (typeof(ISerializable).IsAssignableFrom(objectType) && !canConvertToString)
|
||||
return CreateISerializableContract(objectType);
|
||||
#endif
|
||||
|
||||
if (CollectionUtils.IsDictionaryType(objectType))
|
||||
return CreateDictionaryContract(objectType);
|
||||
|
||||
if (typeof(IEnumerable).IsAssignableFrom(objectType))
|
||||
return CreateArrayContract(objectType);
|
||||
|
||||
if (canConvertToString)
|
||||
if (CanConvertToString(objectType))
|
||||
return CreateStringContract(objectType);
|
||||
|
||||
#if !SILVERLIGHT && !PocketPC
|
||||
if (typeof(ISerializable).IsAssignableFrom(objectType))
|
||||
return CreateISerializableContract(objectType);
|
||||
#endif
|
||||
|
||||
return CreateObjectContract(objectType);
|
||||
}
|
||||
|
||||
@@ -668,6 +666,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
property.DefaultValueHandling = (propertyAttribute != null) ? propertyAttribute._defaultValueHandling : null;
|
||||
property.ReferenceLoopHandling = (propertyAttribute != null) ? propertyAttribute._referenceLoopHandling : null;
|
||||
property.ObjectCreationHandling = (propertyAttribute != null) ? propertyAttribute._objectCreationHandling : null;
|
||||
property.TypeNameHandling = (propertyAttribute != null) ? propertyAttribute._typeNameHandling : null;
|
||||
property.IsReference = (propertyAttribute != null) ? propertyAttribute._isReference : null;
|
||||
|
||||
property.ShouldSerialize = CreateShouldSerializeTest(member);
|
||||
|
||||
@@ -50,13 +50,24 @@ namespace Newtonsoft.Json.Serialization
|
||||
{
|
||||
if (assemblyName != null)
|
||||
{
|
||||
Assembly assembly = Assembly.Load(assemblyName);
|
||||
Assembly assembly;
|
||||
|
||||
#if !SILVERLIGHT && !PocketPC
|
||||
// look, I don't like using obsolete methods as much as you do but this is the only way
|
||||
// Assembly.Load won't check the GAC for a partial name
|
||||
#pragma warning disable 618,612
|
||||
assembly = Assembly.LoadWithPartialName(assemblyName);
|
||||
#pragma warning restore 618,612
|
||||
#else
|
||||
assembly = Assembly.Load(assemblyName);
|
||||
#endif
|
||||
|
||||
if (assembly == null)
|
||||
throw new JsonSerializationException("Could not load assembly '{0}'.".FormatWith(CultureInfo.InvariantCulture, assemblyName));
|
||||
|
||||
Type type = assembly.GetType(typeName);
|
||||
if (type == null)
|
||||
throw new JsonSerializationException("Could not find type '{0}'.".FormatWith(CultureInfo.InvariantCulture, typeName));
|
||||
throw new JsonSerializationException("Could not find type '{0}' in assembly '{1}'.".FormatWith(CultureInfo.InvariantCulture, typeName, assembly.FullName));
|
||||
|
||||
return type;
|
||||
}
|
||||
|
||||
@@ -127,6 +127,8 @@ namespace Newtonsoft.Json.Serialization
|
||||
|
||||
public Predicate<object> ShouldSerialize { get; set; }
|
||||
|
||||
public TypeNameHandling? TypeNameHandling { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns a <see cref="String"/> that represents this instance.
|
||||
/// </summary>
|
||||
|
||||
@@ -192,7 +192,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
return converter.ReadJson(reader, objectType, currentValue, GetInternalSerializer());
|
||||
}
|
||||
|
||||
return CreateValueInternal(reader, objectType, contract, currentValue);
|
||||
return CreateValueInternal(reader, objectType, contract, property, currentValue);
|
||||
}
|
||||
|
||||
private object CreateValueNonProperty(JsonReader reader, Type objectType, JsonContract contract)
|
||||
@@ -202,10 +202,10 @@ namespace Newtonsoft.Json.Serialization
|
||||
if (converter != null && converter.CanRead)
|
||||
return converter.ReadJson(reader, objectType, null, GetInternalSerializer());
|
||||
|
||||
return CreateValueInternal(reader, objectType, contract, null);
|
||||
return CreateValueInternal(reader, objectType, contract, null, null);
|
||||
}
|
||||
|
||||
private object CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, object existingValue)
|
||||
private object CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, object existingValue)
|
||||
{
|
||||
if (contract is JsonLinqContract)
|
||||
return CreateJToken(reader, contract);
|
||||
@@ -217,9 +217,9 @@ namespace Newtonsoft.Json.Serialization
|
||||
// populate a typed object or generic dictionary/array
|
||||
// depending upon whether an objectType was supplied
|
||||
case JsonToken.StartObject:
|
||||
return CreateObject(reader, objectType, contract, existingValue);
|
||||
return CreateObject(reader, objectType, contract, member, existingValue);
|
||||
case JsonToken.StartArray:
|
||||
return CreateList(reader, objectType, contract, existingValue, null);
|
||||
return CreateList(reader, objectType, contract, member, existingValue, null);
|
||||
case JsonToken.Integer:
|
||||
case JsonToken.Float:
|
||||
case JsonToken.Boolean:
|
||||
@@ -282,7 +282,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
return converter;
|
||||
}
|
||||
|
||||
private object CreateObject(JsonReader reader, Type objectType, JsonContract contract, object existingValue)
|
||||
private object CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, object existingValue)
|
||||
{
|
||||
CheckedRead(reader);
|
||||
|
||||
@@ -317,7 +317,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
|
||||
CheckedRead(reader);
|
||||
|
||||
if (Serializer.TypeNameHandling != TypeNameHandling.None)
|
||||
if ((((member != null) ? member.TypeNameHandling : null) ?? Serializer.TypeNameHandling) != TypeNameHandling.None)
|
||||
{
|
||||
string typeName;
|
||||
string assemblyName;
|
||||
@@ -355,7 +355,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
else if (string.Equals(propertyName, JsonTypeReflector.ArrayValuesPropertyName, StringComparison.Ordinal))
|
||||
{
|
||||
CheckedRead(reader);
|
||||
object list = CreateList(reader, objectType, contract, existingValue, id);
|
||||
object list = CreateList(reader, objectType, contract, member, existingValue, id);
|
||||
CheckedRead(reader);
|
||||
return list;
|
||||
}
|
||||
@@ -420,7 +420,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
throw new JsonSerializationException("Unexpected end when deserializing object.");
|
||||
}
|
||||
|
||||
private object CreateList(JsonReader reader, Type objectType, JsonContract contract, object existingValue, string reference)
|
||||
private object CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, object existingValue, string reference)
|
||||
{
|
||||
object value;
|
||||
if (HasDefinedType(objectType))
|
||||
|
||||
@@ -30,6 +30,7 @@ using System.ComponentModel;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.Serialization.Formatters;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
using System.Runtime.Serialization;
|
||||
@@ -80,9 +81,9 @@ namespace Newtonsoft.Json.Serialization
|
||||
return Serializer.ContractResolver.ResolveContract(value.GetType());
|
||||
}
|
||||
|
||||
private void SerializeValue(JsonWriter writer, object value, JsonConverter memberConverter, JsonContract contract)
|
||||
private void SerializeValue(JsonWriter writer, object value, JsonProperty member, JsonContract contract)
|
||||
{
|
||||
JsonConverter converter = memberConverter;
|
||||
JsonConverter converter = (member != null) ? member.Converter : null;
|
||||
|
||||
if (value == null)
|
||||
{
|
||||
@@ -108,22 +109,22 @@ namespace Newtonsoft.Json.Serialization
|
||||
}
|
||||
else if (contract is JsonObjectContract)
|
||||
{
|
||||
SerializeObject(writer, value, (JsonObjectContract) contract);
|
||||
SerializeObject(writer, value, (JsonObjectContract) contract, member);
|
||||
}
|
||||
else if (contract is JsonDictionaryContract)
|
||||
{
|
||||
JsonDictionaryContract dictionaryContract = (JsonDictionaryContract) contract;
|
||||
SerializeDictionary(writer, dictionaryContract.CreateWrapper(value), dictionaryContract);
|
||||
SerializeDictionary(writer, dictionaryContract.CreateWrapper(value), dictionaryContract, member);
|
||||
}
|
||||
else if (contract is JsonArrayContract)
|
||||
{
|
||||
if (value is IList)
|
||||
{
|
||||
SerializeList(writer, (IList) value, (JsonArrayContract) contract);
|
||||
SerializeList(writer, (IList) value, (JsonArrayContract) contract, member);
|
||||
}
|
||||
else if (value is IEnumerable)
|
||||
{
|
||||
SerializeEnumerable(writer, (IEnumerable) value, (JsonArrayContract) contract);
|
||||
SerializeList(writer, ((IEnumerable)value).Cast<object>().ToList(), (JsonArrayContract)contract, member);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -162,11 +163,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
|
||||
if (isReference == null)
|
||||
{
|
||||
if (value is IList)
|
||||
isReference = HasFlag(Serializer.PreserveReferencesHandling, PreserveReferencesHandling.Arrays);
|
||||
else if (value is IDictionary)
|
||||
isReference = HasFlag(Serializer.PreserveReferencesHandling, PreserveReferencesHandling.Objects);
|
||||
else if (value is IEnumerable)
|
||||
if (contract is JsonArrayContract)
|
||||
isReference = HasFlag(Serializer.PreserveReferencesHandling, PreserveReferencesHandling.Arrays);
|
||||
else
|
||||
isReference = HasFlag(Serializer.PreserveReferencesHandling, PreserveReferencesHandling.Objects);
|
||||
@@ -181,7 +178,6 @@ namespace Newtonsoft.Json.Serialization
|
||||
private void WriteMemberInfoProperty(JsonWriter writer, object memberValue, JsonProperty property, JsonContract contract)
|
||||
{
|
||||
string propertyName = property.PropertyName;
|
||||
JsonConverter memberConverter = property.MemberConverter;
|
||||
object defaultValue = property.DefaultValue;
|
||||
|
||||
if (property.NullValueHandling.GetValueOrDefault(Serializer.NullValueHandling) == NullValueHandling.Ignore &&
|
||||
@@ -203,7 +199,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
return;
|
||||
|
||||
writer.WritePropertyName(propertyName);
|
||||
SerializeValue(writer, memberValue, memberConverter, contract);
|
||||
SerializeValue(writer, memberValue, property, contract);
|
||||
}
|
||||
|
||||
private bool CheckForCircularReference(object value, ReferenceLoopHandling? referenceLoopHandling, JsonContract contract)
|
||||
@@ -290,7 +286,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
contract.InvokeOnSerialized(value, Serializer.Context);
|
||||
}
|
||||
|
||||
private void SerializeObject(JsonWriter writer, object value, JsonObjectContract contract)
|
||||
private void SerializeObject(JsonWriter writer, object value, JsonObjectContract contract, JsonProperty member)
|
||||
{
|
||||
contract.InvokeOnSerializing(value, Serializer.Context);
|
||||
|
||||
@@ -303,7 +299,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
writer.WritePropertyName(JsonTypeReflector.IdPropertyName);
|
||||
writer.WriteValue(Serializer.ReferenceResolver.GetReference(value));
|
||||
}
|
||||
if (HasFlag(Serializer.TypeNameHandling, TypeNameHandling.Objects))
|
||||
if (HasFlag(((member != null) ? member.TypeNameHandling : null) ?? Serializer.TypeNameHandling, TypeNameHandling.Objects))
|
||||
{
|
||||
WriteTypeProperty(writer, contract.UnderlyingType);
|
||||
}
|
||||
@@ -340,7 +336,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
private void WriteTypeProperty(JsonWriter writer, Type type)
|
||||
{
|
||||
writer.WritePropertyName(JsonTypeReflector.TypePropertyName);
|
||||
writer.WriteValue(type.AssemblyQualifiedName);
|
||||
writer.WriteValue(ReflectionUtils.GetTypeName(type, Serializer.TypeNameAssemblyFormat));
|
||||
}
|
||||
|
||||
private bool HasFlag(PreserveReferencesHandling value, PreserveReferencesHandling flag)
|
||||
@@ -372,19 +368,14 @@ namespace Newtonsoft.Json.Serialization
|
||||
}
|
||||
}
|
||||
|
||||
private void SerializeEnumerable(JsonWriter writer, IEnumerable values, JsonArrayContract contract)
|
||||
{
|
||||
SerializeList(writer, values.Cast<object>().ToList(), contract);
|
||||
}
|
||||
|
||||
private void SerializeList(JsonWriter writer, IList values, JsonArrayContract contract)
|
||||
private void SerializeList(JsonWriter writer, IList values, JsonArrayContract contract, JsonProperty member)
|
||||
{
|
||||
contract.InvokeOnSerializing(values, Serializer.Context);
|
||||
|
||||
SerializeStack.Add(values);
|
||||
|
||||
bool isReference = contract.IsReference ?? HasFlag(Serializer.PreserveReferencesHandling, PreserveReferencesHandling.Arrays);
|
||||
bool includeTypeDetails = HasFlag(Serializer.TypeNameHandling, TypeNameHandling.Arrays);
|
||||
bool includeTypeDetails = HasFlag(((member != null) ? member.TypeNameHandling : null) ?? Serializer.TypeNameHandling, TypeNameHandling.Arrays);
|
||||
|
||||
if (isReference || includeTypeDetails)
|
||||
{
|
||||
@@ -470,7 +461,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
}
|
||||
#endif
|
||||
|
||||
private void SerializeDictionary(JsonWriter writer, IWrappedDictionary values, JsonDictionaryContract contract)
|
||||
private void SerializeDictionary(JsonWriter writer, IWrappedDictionary values, JsonDictionaryContract contract, JsonProperty member)
|
||||
{
|
||||
contract.InvokeOnSerializing(values.UnderlyingDictionary, Serializer.Context);
|
||||
|
||||
@@ -483,7 +474,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
writer.WritePropertyName(JsonTypeReflector.IdPropertyName);
|
||||
writer.WriteValue(Serializer.ReferenceResolver.GetReference(values.UnderlyingDictionary));
|
||||
}
|
||||
if (HasFlag(Serializer.TypeNameHandling, TypeNameHandling.Objects))
|
||||
if (HasFlag(((member != null) ? member.TypeNameHandling : null) ?? Serializer.TypeNameHandling, TypeNameHandling.Objects))
|
||||
{
|
||||
WriteTypeProperty(writer, values.UnderlyingDictionary.GetType());
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Runtime.Serialization.Formatters;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
@@ -100,6 +101,12 @@ namespace Newtonsoft.Json.Serialization
|
||||
set { _serializer.TypeNameHandling = value; }
|
||||
}
|
||||
|
||||
public override FormatterAssemblyStyle TypeNameAssemblyFormat
|
||||
{
|
||||
get { return _serializer.TypeNameAssemblyFormat; }
|
||||
set { _serializer.TypeNameAssemblyFormat = value; }
|
||||
}
|
||||
|
||||
public override ConstructorHandling ConstructorHandling
|
||||
{
|
||||
get { return _serializer.ConstructorHandling; }
|
||||
|
||||
@@ -267,6 +267,8 @@ namespace Newtonsoft.Json.Serialization
|
||||
try
|
||||
{
|
||||
new ReflectionPermission(ReflectionPermissionFlag.MemberAccess).Demand();
|
||||
new ReflectionPermission(ReflectionPermissionFlag.RestrictedMemberAccess).Demand();
|
||||
new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();
|
||||
_dynamicCodeGeneration = true;
|
||||
}
|
||||
catch (Exception)
|
||||
|
||||
@@ -473,7 +473,7 @@ namespace Newtonsoft.Json.Utilities
|
||||
}
|
||||
}
|
||||
|
||||
public static IList CreateAndPopulateList(Type listType, Action<IList, bool> populateList)
|
||||
public static object CreateAndPopulateList(Type listType, Action<IList, bool> populateList)
|
||||
{
|
||||
ValidationUtils.ArgumentNotNull(listType, "listType");
|
||||
ValidationUtils.ArgumentNotNull(populateList, "populateList");
|
||||
@@ -526,9 +526,12 @@ namespace Newtonsoft.Json.Utilities
|
||||
else
|
||||
list = null;
|
||||
}
|
||||
else if (ReflectionUtils.ImplementsGenericDefinition(listType, typeof(IList<>)))
|
||||
else if (ReflectionUtils.ImplementsGenericDefinition(listType, typeof(ICollection<>)))
|
||||
{
|
||||
list = CreateGenericList(ReflectionUtils.GetCollectionItemType(listType));
|
||||
if (ReflectionUtils.IsInstantiatableType(listType))
|
||||
list = CreateCollectionWrapper(Activator.CreateInstance(listType));
|
||||
else
|
||||
list = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -548,6 +551,10 @@ namespace Newtonsoft.Json.Utilities
|
||||
else if (ReflectionUtils.InheritsGenericDefinition(listType, typeof(ReadOnlyCollection<>)))
|
||||
list = (IList)ReflectionUtils.CreateInstance(listType, list);
|
||||
}
|
||||
else if (list is IWrappedCollection)
|
||||
{
|
||||
return ((IWrappedCollection) list).UnderlyingCollection;
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ using System.Reflection;
|
||||
using System.Collections;
|
||||
using System.Linq;
|
||||
using System.Globalization;
|
||||
using System.Runtime.Serialization.Formatters;
|
||||
|
||||
namespace Newtonsoft.Json.Utilities
|
||||
{
|
||||
@@ -39,6 +40,19 @@ namespace Newtonsoft.Json.Utilities
|
||||
return (v != null) ? v.GetType() : null;
|
||||
}
|
||||
|
||||
public static string GetTypeName(Type t, FormatterAssemblyStyle assemblyFormat)
|
||||
{
|
||||
switch (assemblyFormat)
|
||||
{
|
||||
case FormatterAssemblyStyle.Simple:
|
||||
return t.FullName + ", " + t.Assembly.GetName().Name;
|
||||
case FormatterAssemblyStyle.Full:
|
||||
return t.AssemblyQualifiedName;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
|
||||
public static bool IsInstantiatableType(Type t)
|
||||
{
|
||||
ValidationUtils.ArgumentNotNull(t, "t");
|
||||
|
||||
Reference in New Issue
Block a user