-Added TypeNameAssemblyFormatHandling

-Added TypeNameAssemblyFormatHandling properties to JsonSerializer and JsonSerializerSettings
-Changed TypeNameAssemblyFormat properties on JsonSerializer and JsonSerializerSettings to obsolete
This commit is contained in:
James Newton-King
2016-09-17 15:57:03 +12:00
parent a0403492ad
commit ca27a0fc3a
17 changed files with 184 additions and 33 deletions
+24 -5
View File
@@ -45,7 +45,7 @@ namespace Newtonsoft.Json
public class JsonSerializer
{
internal TypeNameHandling _typeNameHandling;
internal FormatterAssemblyStyle _typeNameAssemblyFormat;
internal TypeNameAssemblyFormatHandling _typeNameAssemblyFormatHandling;
internal PreserveReferencesHandling _preserveReferencesHandling;
internal ReferenceLoopHandling _referenceLoopHandling;
internal MissingMemberHandling _missingMemberHandling;
@@ -161,9 +161,10 @@ namespace Newtonsoft.Json
/// Gets or sets how a type name assembly is written and resolved by the serializer.
/// </summary>
/// <value>The type name assembly format.</value>
[Obsolete("TypeNameAssemblyFormat is obsolete. Use TypeNameAssemblyFormatHandling instead.")]
public virtual FormatterAssemblyStyle TypeNameAssemblyFormat
{
get { return _typeNameAssemblyFormat; }
get { return (FormatterAssemblyStyle)_typeNameAssemblyFormatHandling; }
set
{
if (value < FormatterAssemblyStyle.Simple || value > FormatterAssemblyStyle.Full)
@@ -171,7 +172,25 @@ namespace Newtonsoft.Json
throw new ArgumentOutOfRangeException(nameof(value));
}
_typeNameAssemblyFormat = value;
_typeNameAssemblyFormatHandling = (TypeNameAssemblyFormatHandling)value;
}
}
/// <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 TypeNameAssemblyFormatHandling TypeNameAssemblyFormatHandling
{
get { return _typeNameAssemblyFormatHandling; }
set
{
if (value < TypeNameAssemblyFormatHandling.Simple || value > TypeNameAssemblyFormatHandling.Full)
{
throw new ArgumentOutOfRangeException(nameof(value));
}
_typeNameAssemblyFormatHandling = value;
}
}
@@ -594,9 +613,9 @@ namespace Newtonsoft.Json
{
serializer.MetadataPropertyHandling = settings.MetadataPropertyHandling;
}
if (settings._typeNameAssemblyFormat != null)
if (settings._typeNameAssemblyFormatHandling != null)
{
serializer.TypeNameAssemblyFormat = settings.TypeNameAssemblyFormat;
serializer.TypeNameAssemblyFormatHandling = settings.TypeNameAssemblyFormatHandling;
}
if (settings._preserveReferencesHandling != null)
{
+15 -4
View File
@@ -57,7 +57,7 @@ namespace Newtonsoft.Json
internal const FloatParseHandling DefaultFloatParseHandling = FloatParseHandling.Double;
internal const FloatFormatHandling DefaultFloatFormatHandling = FloatFormatHandling.String;
internal const StringEscapeHandling DefaultStringEscapeHandling = StringEscapeHandling.Default;
internal const FormatterAssemblyStyle DefaultFormatterAssemblyStyle = FormatterAssemblyStyle.Simple;
internal const TypeNameAssemblyFormatHandling DefaultTypeNameAssemblyFormatHandling = Json.TypeNameAssemblyFormatHandling.Simple;
internal static readonly CultureInfo DefaultCulture;
internal const bool DefaultCheckAdditionalContent = false;
internal const string DefaultDateFormatString = @"yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFFK";
@@ -75,7 +75,7 @@ namespace Newtonsoft.Json
internal bool _maxDepthSet;
internal string _dateFormatString;
internal bool _dateFormatStringSet;
internal FormatterAssemblyStyle? _typeNameAssemblyFormat;
internal TypeNameAssemblyFormatHandling? _typeNameAssemblyFormatHandling;
internal DefaultValueHandling? _defaultValueHandling;
internal PreserveReferencesHandling? _preserveReferencesHandling;
internal NullValueHandling? _nullValueHandling;
@@ -182,10 +182,21 @@ namespace Newtonsoft.Json
/// Gets or sets how a type name assembly is written and resolved by the serializer.
/// </summary>
/// <value>The type name assembly format.</value>
[Obsolete("TypeNameAssemblyFormat is obsolete. Use TypeNameAssemblyFormatHandling instead.")]
public FormatterAssemblyStyle TypeNameAssemblyFormat
{
get { return _typeNameAssemblyFormat ?? DefaultFormatterAssemblyStyle; }
set { _typeNameAssemblyFormat = value; }
get { return (FormatterAssemblyStyle)TypeNameAssemblyFormatHandling; }
set { TypeNameAssemblyFormatHandling = (TypeNameAssemblyFormatHandling)value; }
}
/// <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 TypeNameAssemblyFormatHandling TypeNameAssemblyFormatHandling
{
get { return _typeNameAssemblyFormatHandling ?? DefaultTypeNameAssemblyFormatHandling; }
set { _typeNameAssemblyFormatHandling = value; }
}
/// <summary>
@@ -144,6 +144,8 @@
<Compile Include="Serialization\TraceJsonReader.cs" />
<Compile Include="Serialization\TraceJsonWriter.cs" />
<Compile Include="StringEscapeHandling.cs" />
<Compile Include="TraceLevel.cs" />
<Compile Include="TypeNameAssemblyFormatHandling.cs" />
<Compile Include="Utilities\DateTimeParser.cs" />
<Compile Include="Utilities\DynamicProxy.cs" />
<Compile Include="Utilities\DynamicProxyMetaObject.cs" />
@@ -158,6 +158,7 @@
<Compile Include="Serialization\TraceJsonWriter.cs" />
<Compile Include="StringEscapeHandling.cs" />
<Compile Include="TraceLevel.cs" />
<Compile Include="TypeNameAssemblyFormatHandling.cs" />
<Compile Include="Utilities\DateTimeParser.cs" />
<Compile Include="Utilities\DynamicProxy.cs" />
<Compile Include="Linq\JRaw.cs" />
@@ -113,6 +113,7 @@
<Compile Include="Converters\VersionConverter.cs" />
<Compile Include="FloatFormatHandling.cs" />
<Compile Include="FloatParseHandling.cs" />
<Compile Include="FormatterAssemblyStyle.cs" />
<Compile Include="IArrayPool.cs" />
<Compile Include="JsonDictionaryAttribute.cs" />
<Compile Include="JsonException.cs" />
@@ -142,6 +143,7 @@
<Compile Include="Linq\MergeArrayHandling.cs" />
<Compile Include="Linq\MergeNullValueHandling.cs" />
<Compile Include="MetadataPropertyHandling.cs" />
<Compile Include="SerializationBinder.cs" />
<Compile Include="Serialization\CamelCaseNamingStrategy.cs" />
<Compile Include="Serialization\DefaultNamingStrategy.cs" />
<Compile Include="Serialization\DiagnosticsTraceWriter.cs" />
@@ -156,6 +158,8 @@
<Compile Include="Serialization\TraceJsonReader.cs" />
<Compile Include="Serialization\TraceJsonWriter.cs" />
<Compile Include="StringEscapeHandling.cs" />
<Compile Include="TraceLevel.cs" />
<Compile Include="TypeNameAssemblyFormatHandling.cs" />
<Compile Include="Utilities\DateTimeParser.cs" />
<Compile Include="Utilities\DynamicProxy.cs" />
<Compile Include="Linq\JRaw.cs" />
@@ -210,6 +210,7 @@
<Compile Include="Serialization\TraceJsonWriter.cs" />
<Compile Include="StringEscapeHandling.cs" />
<Compile Include="TraceLevel.cs" />
<Compile Include="TypeNameAssemblyFormatHandling.cs" />
<Compile Include="TypeNameHandling.cs" />
<Compile Include="Utilities\Base64Encoder.cs" />
<Compile Include="Utilities\BidirectionalDictionary.cs" />
@@ -215,6 +215,7 @@
<Compile Include="Serialization\TraceJsonWriter.cs" />
<Compile Include="StringEscapeHandling.cs" />
<Compile Include="TraceLevel.cs" />
<Compile Include="TypeNameAssemblyFormatHandling.cs" />
<Compile Include="TypeNameHandling.cs" />
<Compile Include="Utilities\Base64Encoder.cs" />
<Compile Include="Utilities\BidirectionalDictionary.cs" />
@@ -212,6 +212,7 @@
<Compile Include="Serialization\ReflectionValueProvider.cs" />
<Compile Include="Serialization\TraceJsonReader.cs" />
<Compile Include="Serialization\TraceJsonWriter.cs" />
<Compile Include="TypeNameAssemblyFormatHandling.cs" />
<Compile Include="StringEscapeHandling.cs" />
<Compile Include="TraceLevel.cs" />
<Compile Include="TypeNameHandling.cs" />
@@ -603,7 +603,7 @@ namespace Newtonsoft.Json.Serialization
private void WriteTypeProperty(JsonWriter writer, Type type)
{
string typeName = ReflectionUtils.GetTypeName(type, Serializer._typeNameAssemblyFormat, Serializer._binder);
string typeName = ReflectionUtils.GetTypeName(type, Serializer._typeNameAssemblyFormatHandling, Serializer._binder);
if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Verbose)
{
@@ -121,12 +121,19 @@ namespace Newtonsoft.Json.Serialization
set { _serializer.MetadataPropertyHandling = value; }
}
[Obsolete("TypeNameAssemblyFormat is obsolete. Use TypeNameAssemblyFormatHandling instead.")]
public override FormatterAssemblyStyle TypeNameAssemblyFormat
{
get { return _serializer.TypeNameAssemblyFormat; }
set { _serializer.TypeNameAssemblyFormat = value; }
}
public override TypeNameAssemblyFormatHandling TypeNameAssemblyFormatHandling
{
get { return _serializer.TypeNameAssemblyFormatHandling; }
set { _serializer.TypeNameAssemblyFormatHandling = value; }
}
public override ConstructorHandling ConstructorHandling
{
get { return _serializer.ConstructorHandling; }
@@ -0,0 +1,43 @@
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
namespace Newtonsoft.Json
{
/// <summary>
/// Indicates the method that will be used during deserialization for locating and loading assemblies.
/// </summary>
public enum TypeNameAssemblyFormatHandling
{
/// <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 = 0,
/// <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 = 1
}
}
@@ -151,7 +151,7 @@ namespace Newtonsoft.Json.Utilities
return (v != null) ? v.GetType() : null;
}
public static string GetTypeName(Type t, FormatterAssemblyStyle assemblyFormat, SerializationBinder binder)
public static string GetTypeName(Type t, TypeNameAssemblyFormatHandling assemblyFormat, SerializationBinder binder)
{
string fullyQualifiedTypeName;
#if !(NET20 || NET35)
@@ -171,9 +171,9 @@ namespace Newtonsoft.Json.Utilities
switch (assemblyFormat)
{
case FormatterAssemblyStyle.Simple:
case TypeNameAssemblyFormatHandling.Simple:
return RemoveAssemblyDetails(fullyQualifiedTypeName);
case FormatterAssemblyStyle.Full:
case TypeNameAssemblyFormatHandling.Full:
return fullyQualifiedTypeName;
default:
throw new ArgumentOutOfRangeException();