Skip to content

Commit

Permalink
-Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK committed Oct 22, 2017
1 parent 9932155 commit 473a772
Show file tree
Hide file tree
Showing 32 changed files with 228 additions and 198 deletions.
2 changes: 1 addition & 1 deletion Src/Newtonsoft.Json/Linq/JObject.Async.cs
Expand Up @@ -44,7 +44,7 @@ public partial class JObject
/// <returns>A <see cref="Task"/> that represents the asynchronous write operation.</returns>
public override Task WriteToAsync(JsonWriter writer, CancellationToken cancellationToken, params JsonConverter[] converters)
{
var t = writer.WriteStartObjectAsync(cancellationToken);
Task t = writer.WriteStartObjectAsync(cancellationToken);
if (!t.IsCompletedSucessfully())
{
return AwaitProperties(t, 0, writer, cancellationToken, converters);
Expand Down
27 changes: 26 additions & 1 deletion Src/Newtonsoft.Json/Serialization/CamelCaseNamingStrategy.cs
@@ -1,4 +1,29 @@
using Newtonsoft.Json.Utilities;
#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

using Newtonsoft.Json.Utilities;

namespace Newtonsoft.Json.Serialization
{
Expand Down
34 changes: 14 additions & 20 deletions Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs
Expand Up @@ -275,8 +275,7 @@ protected virtual List<MemberInfo> GetSerializableMembers(Type objectType)
// serialize all fields
foreach (MemberInfo member in allMembers)
{
FieldInfo field = member as FieldInfo;
if (field != null && !field.IsStatic)
if (member is FieldInfo field && !field.IsStatic)
{
serializableMembers.Add(member);
}
Expand Down Expand Up @@ -426,8 +425,7 @@ private MemberInfo GetExtensionDataMemberForType(Type type)
Type t = ReflectionUtils.GetMemberUnderlyingType(m);
Type dictionaryType;
if (ReflectionUtils.ImplementsGenericDefinition(t, typeof(IDictionary<,>), out dictionaryType))
if (ReflectionUtils.ImplementsGenericDefinition(t, typeof(IDictionary<,>), out Type dictionaryType))
{
Type keyType = dictionaryType.GetGenericArguments()[0];
Type valueType = dictionaryType.GetGenericArguments()[1];
Expand All @@ -454,8 +452,7 @@ private static void SetExtensionDataDelegates(JsonObjectContract contract, Membe

Type t = ReflectionUtils.GetMemberUnderlyingType(member);

Type dictionaryType;
ReflectionUtils.ImplementsGenericDefinition(t, typeof(IDictionary<,>), out dictionaryType);
ReflectionUtils.ImplementsGenericDefinition(t, typeof(IDictionary<,>), out Type dictionaryType);

Type keyType = dictionaryType.GetGenericArguments()[0];
Type valueType = dictionaryType.GetGenericArguments()[1];
Expand Down Expand Up @@ -590,7 +587,7 @@ private ConstructorInfo GetImmutableConstructor(Type objectType, JsonPropertyCol
{
foreach (ParameterInfo parameterInfo in parameters)
{
var memberProperty = MatchProperty(memberProperties, parameterInfo.Name, parameterInfo.ParameterType);
JsonProperty memberProperty = MatchProperty(memberProperties, parameterInfo.Name, parameterInfo.ParameterType);
if (memberProperty == null || memberProperty.Writable)
{
return null;
Expand Down Expand Up @@ -691,8 +688,7 @@ protected virtual JsonProperty CreatePropertyFromConstructorParameter(JsonProper
property.PropertyType = parameterInfo.ParameterType;
property.AttributeProvider = new ReflectionAttributeProvider(parameterInfo);

bool allowNonPublicAccess;
SetPropertySettingsFromAttributes(property, parameterInfo, parameterInfo.Name, parameterInfo.Member.DeclaringType, MemberSerialization.OptOut, out allowNonPublicAccess);
SetPropertySettingsFromAttributes(property, parameterInfo, parameterInfo.Name, parameterInfo.Member.DeclaringType, MemberSerialization.OptOut, out _);

property.Readable = false;
property.Writable = true;
Expand Down Expand Up @@ -776,13 +772,13 @@ private void InitializeContract(JsonContract contract)

private void ResolveCallbackMethods(JsonContract contract, Type t)
{
List<SerializationCallback> onSerializing;
List<SerializationCallback> onSerialized;
List<SerializationCallback> onDeserializing;
List<SerializationCallback> onDeserialized;
List<SerializationErrorCallback> onError;

GetCallbackMethodsForType(t, out onSerializing, out onSerialized, out onDeserializing, out onDeserialized, out onError);
GetCallbackMethodsForType(
t,
out List<SerializationCallback> onSerializing,
out List<SerializationCallback> onSerialized,
out List<SerializationCallback> onDeserializing,
out List<SerializationCallback> onDeserialized,
out List<SerializationErrorCallback> onError);

if (onSerializing != null)
{
Expand Down Expand Up @@ -1222,8 +1218,7 @@ internal static bool IsIConvertible(Type t)
internal static bool CanConvertToString(Type type)
{
#if HAVE_TYPE_DESCRIPTOR
TypeConverter converter;
if (JsonTypeReflector.CanTypeDescriptorConvertString(type, out converter))
if (JsonTypeReflector.CanTypeDescriptorConvertString(type, out _))
{
return true;
}
Expand Down Expand Up @@ -1379,8 +1374,7 @@ protected virtual JsonProperty CreateProperty(MemberInfo member, MemberSerializa
property.ValueProvider = CreateMemberValueProvider(member);
property.AttributeProvider = new ReflectionAttributeProvider(member);

bool allowNonPublicAccess;
SetPropertySettingsFromAttributes(property, member, member.Name, member.DeclaringType, memberSerialization, out allowNonPublicAccess);
SetPropertySettingsFromAttributes(property, member, member.Name, member.DeclaringType, memberSerialization, out bool allowNonPublicAccess);

if (memberSerialization != MemberSerialization.Fields)
{
Expand Down
15 changes: 5 additions & 10 deletions Src/Newtonsoft.Json/Serialization/DefaultReferenceResolver.cs
Expand Up @@ -35,11 +35,9 @@ internal class DefaultReferenceResolver : IReferenceResolver

private BidirectionalDictionary<string, object> GetMappings(object context)
{
JsonSerializerInternalBase internalSerializer = context as JsonSerializerInternalBase;
if (internalSerializer == null)
if (!(context is JsonSerializerInternalBase internalSerializer))
{
JsonSerializerProxy proxy = context as JsonSerializerProxy;
if (proxy != null)
if (context is JsonSerializerProxy proxy)
{
internalSerializer = proxy.GetInternalSerializer();
}
Expand All @@ -54,17 +52,15 @@ internal class DefaultReferenceResolver : IReferenceResolver

public object ResolveReference(object context, string reference)
{
object value;
GetMappings(context).TryGetByFirst(reference, out value);
GetMappings(context).TryGetByFirst(reference, out object value);
return value;
}

public string GetReference(object context, object value)
{
BidirectionalDictionary<string, object> mappings = GetMappings(context);

string reference;
if (!mappings.TryGetBySecond(value, out reference))
if (!mappings.TryGetBySecond(value, out string reference))
{
_referenceCount++;
reference = _referenceCount.ToString(CultureInfo.InvariantCulture);
Expand All @@ -81,8 +77,7 @@ public void AddReference(object context, string reference, object value)

public bool IsReferenced(object context, object value)
{
string reference;
return GetMappings(context).TryGetBySecond(value, out reference);
return GetMappings(context).TryGetBySecond(value, out _);
}
}
}
8 changes: 5 additions & 3 deletions Src/Newtonsoft.Json/Serialization/JsonArrayContract.cs
Expand Up @@ -251,9 +251,11 @@ public JsonArrayContract(Type underlyingType)
}
#endif

Type immutableCreatedType;
ObjectConstructor<object> immutableParameterizedCreator;
if (ImmutableCollectionsUtils.TryBuildImmutableForArrayContract(underlyingType, CollectionItemType, out immutableCreatedType, out immutableParameterizedCreator))
if (ImmutableCollectionsUtils.TryBuildImmutableForArrayContract(
underlyingType,
CollectionItemType,
out Type immutableCreatedType,
out ObjectConstructor<object> immutableParameterizedCreator))
{
CreatedType = immutableCreatedType;
_parameterizedCreator = immutableParameterizedCreator;
Expand Down
13 changes: 7 additions & 6 deletions Src/Newtonsoft.Json/Serialization/JsonDictionaryContract.cs
Expand Up @@ -180,20 +180,21 @@ public JsonDictionaryContract(Type underlyingType)
#if (NET20 || NET35)
if (DictionaryValueType != null && ReflectionUtils.IsNullableType(DictionaryValueType))
{
Type tempDictioanryType;

// bug in .NET 2.0 & 3.5 that Dictionary<TKey, Nullable<TValue>> throws an error when adding null via IDictionary[key] = object
// wrapper will handle calling Add(T) instead
if (ReflectionUtils.InheritsGenericDefinition(CreatedType, typeof(Dictionary<,>), out tempDictioanryType))
if (ReflectionUtils.InheritsGenericDefinition(CreatedType, typeof(Dictionary<,>), out _))
{
ShouldCreateWrapper = true;
}
}
#endif

Type immutableCreatedType;
ObjectConstructor<object> immutableParameterizedCreator;
if (ImmutableCollectionsUtils.TryBuildImmutableForDictionaryContract(underlyingType, DictionaryKeyType, DictionaryValueType, out immutableCreatedType, out immutableParameterizedCreator))
if (ImmutableCollectionsUtils.TryBuildImmutableForDictionaryContract(
underlyingType,
DictionaryKeyType,
DictionaryValueType,
out Type immutableCreatedType,
out ObjectConstructor<object> immutableParameterizedCreator))
{
CreatedType = immutableCreatedType;
_parameterizedCreator = immutableParameterizedCreator;
Expand Down
3 changes: 0 additions & 3 deletions Src/Newtonsoft.Json/Serialization/JsonDynamicContract.cs
Expand Up @@ -25,12 +25,9 @@

#if HAVE_DYNAMIC
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Reflection;
using System.Runtime.CompilerServices;
using Newtonsoft.Json.Utilities;
using System.Collections;

namespace Newtonsoft.Json.Serialization
{
Expand Down
3 changes: 1 addition & 2 deletions Src/Newtonsoft.Json/Serialization/JsonFormatterConverter.cs
Expand Up @@ -60,8 +60,7 @@ public object Convert(object value, Type type)
{
ValidationUtils.ArgumentNotNull(value, nameof(value));

JToken token = value as JToken;
if (token == null)
if (!(value is JToken token))
{
throw new ArgumentException("Value is not a JToken.", nameof(value));
}
Expand Down
3 changes: 1 addition & 2 deletions Src/Newtonsoft.Json/Serialization/JsonPropertyCollection.cs
Expand Up @@ -156,8 +156,7 @@ public JsonProperty GetProperty(string propertyName, StringComparison comparison
// KeyedCollection has an ordinal comparer
if (comparisonType == StringComparison.Ordinal)
{
JsonProperty property;
if (TryGetValue(propertyName, out property))
if (TryGetValue(propertyName, out JsonProperty property))
{
return property;
}
Expand Down
35 changes: 10 additions & 25 deletions Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs
Expand Up @@ -380,8 +380,7 @@ private string GetReference(JsonWriter writer, object value)
internal static bool TryConvertToString(object value, Type type, out string s)
{
#if HAVE_TYPE_DESCRIPTOR
TypeConverter converter;
if (JsonTypeReflector.CanTypeDescriptorConvertString(type, out converter))
if (JsonTypeReflector.CanTypeDescriptorConvertString(type, out TypeConverter converter))
{
s = converter.ConvertToInvariantString(value);
return true;
Expand Down Expand Up @@ -411,8 +410,7 @@ private void SerializeString(JsonWriter writer, object value, JsonStringContract
{
OnSerializing(writer, contract, value);

string s;
TryConvertToString(value, contract.UnderlyingType, out s);
TryConvertToString(value, contract.UnderlyingType, out string s);
writer.WriteValue(s);

OnSerialized(writer, contract, value);
Expand Down Expand Up @@ -453,10 +451,7 @@ private void SerializeObject(JsonWriter writer, object value, JsonObjectContract
JsonProperty property = contract.Properties[index];
try
{
object memberValue;
JsonContract memberContract;

if (!CalculatePropertyValues(writer, value, contract, member, property, out memberContract, out memberValue))
if (!CalculatePropertyValues(writer, value, contract, member, property, out JsonContract memberContract, out object memberValue))
{
continue;
}
Expand Down Expand Up @@ -585,8 +580,7 @@ private void WriteObjectStart(JsonWriter writer, object value, JsonContract cont

private bool HasCreatorParameter(JsonContainerContract contract, JsonProperty property)
{
JsonObjectContract objectContract = contract as JsonObjectContract;
if (objectContract == null)
if (!(contract is JsonObjectContract objectContract))
{
return false;
}
Expand Down Expand Up @@ -668,8 +662,7 @@ private void SerializeConvertable(JsonWriter writer, JsonConverter converter, ob

private void SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
{
IWrappedCollection wrappedCollection = values as IWrappedCollection;
object underlyingList = wrappedCollection != null ? wrappedCollection.UnderlyingCollection : values;
object underlyingList = values is IWrappedCollection wrappedCollection ? wrappedCollection.UnderlyingCollection : values;

OnSerializing(writer, contract, underlyingList);

Expand Down Expand Up @@ -904,10 +897,7 @@ private void SerializeDynamic(JsonWriter writer, IDynamicMetaObjectProvider valu
{
try
{
object memberValue;
JsonContract memberContract;

if (!CalculatePropertyValues(writer, value, contract, member, property, out memberContract, out memberValue))
if (!CalculatePropertyValues(writer, value, contract, member, property, out JsonContract memberContract, out object memberValue))
{
continue;
}
Expand All @@ -931,8 +921,7 @@ private void SerializeDynamic(JsonWriter writer, IDynamicMetaObjectProvider valu

foreach (string memberName in value.GetDynamicMemberNames())
{
object memberValue;
if (contract.TryGetMember(value, memberName, out memberValue))
if (contract.TryGetMember(value, memberName, out object memberValue))
{
try
{
Expand Down Expand Up @@ -1036,8 +1025,7 @@ private bool ShouldWriteType(TypeNameHandling typeNameHandlingFlag, JsonContract

private void SerializeDictionary(JsonWriter writer, IDictionary values, JsonDictionaryContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
{
IWrappedDictionary wrappedDictionary = values as IWrappedDictionary;
object underlyingDictionary = wrappedDictionary != null ? wrappedDictionary.UnderlyingDictionary : values;
object underlyingDictionary = values is IWrappedDictionary wrappedDictionary ? wrappedDictionary.UnderlyingDictionary : values;

OnSerializing(writer, contract, underlyingDictionary);
_serializeStack.Add(underlyingDictionary);
Expand All @@ -1064,8 +1052,7 @@ private void SerializeDictionary(JsonWriter writer, IDictionary values, JsonDict
{
DictionaryEntry entry = e.Entry;

bool escape;
string propertyName = GetPropertyName(writer, entry.Key, contract.KeyContract, out escape);
string propertyName = GetPropertyName(writer, entry.Key, contract.KeyContract, out bool escape);

propertyName = (contract.DictionaryKeyResolver != null)
? contract.DictionaryKeyResolver(propertyName)
Expand Down Expand Up @@ -1120,8 +1107,6 @@ private void SerializeDictionary(JsonWriter writer, IDictionary values, JsonDict

private string GetPropertyName(JsonWriter writer, object name, JsonContract contract, out bool escape)
{
string propertyName;

if (contract.ContractType == JsonContractType.Primitive)
{
JsonPrimitiveContract primitiveContract = (JsonPrimitiveContract)contract;
Expand Down Expand Up @@ -1170,7 +1155,7 @@ private string GetPropertyName(JsonWriter writer, object name, JsonContract cont
}
}
}
else if (TryConvertToString(name, name.GetType(), out propertyName))
else if (TryConvertToString(name, name.GetType(), out string propertyName))
{
escape = true;
return propertyName;
Expand Down
27 changes: 26 additions & 1 deletion Src/Newtonsoft.Json/Serialization/NamingStrategy.cs
@@ -1,4 +1,29 @@
namespace Newtonsoft.Json.Serialization
#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.Serialization
{
/// <summary>
/// A base class for resolving how property names and dictionary keys are serialized.
Expand Down

0 comments on commit 473a772

Please sign in to comment.