diff --git a/Src/Support/Google.Apis.Core/Json/NewtonsoftJsonSerializer.cs b/Src/Support/Google.Apis.Core/Json/NewtonsoftJsonSerializer.cs index bf9436891ff..b04dd3ec99c 100644 --- a/Src/Support/Google.Apis.Core/Json/NewtonsoftJsonSerializer.cs +++ b/Src/Support/Google.Apis.Core/Json/NewtonsoftJsonSerializer.cs @@ -14,13 +14,13 @@ You may obtain a copy of the License at limitations under the License. */ -using Google.Apis.Util; -using Newtonsoft.Json; +using Google.Apis.Util; +using Newtonsoft.Json; using Newtonsoft.Json.Serialization; -using System; -using System.IO; -using System.Reflection; -using System.Linq; +using System; +using System.IO; +using System.Linq; +using System.Reflection; using System.Threading; using System.Threading.Tasks; @@ -110,7 +110,6 @@ protected override JsonContract CreateContract(Type objectType) /// Class for serialization and deserialization of JSON documents using the Newtonsoft Library. public class NewtonsoftJsonSerializer : IJsonSerializer { - private readonly JsonSerializerSettings settings; private readonly JsonSerializer serializer; /// The default instance of the Newtonsoft JSON Serializer, with default settings. @@ -127,12 +126,8 @@ public NewtonsoftJsonSerializer() : this(CreateDefaultSettings()) /// Constructs a new instance with the given settings. /// /// The settings to apply when serializing and deserializing. Must not be null. - public NewtonsoftJsonSerializer(JsonSerializerSettings settings) - { - Utilities.ThrowIfNull(settings, nameof(settings)); - this.settings = settings; - serializer = JsonSerializer.Create(settings); - } + public NewtonsoftJsonSerializer(JsonSerializerSettings settings) => + serializer = JsonSerializer.Create(Utilities.ThrowIfNull(settings, nameof(settings))); /// /// Creates a new instance of with the same behavior @@ -146,7 +141,6 @@ public static JsonSerializerSettings CreateDefaultSettings() => NullValueHandling = NullValueHandling.Ignore, MetadataPropertyHandling = MetadataPropertyHandling.Ignore, ContractResolver = new NewtonsoftJsonContractResolver(), - DefaultValueHandling = DefaultValueHandling.Include, }; /// @@ -184,9 +178,9 @@ public T Deserialize(string input) { if (string.IsNullOrEmpty(input)) { - return default(T); + return default; } - return JsonConvert.DeserializeObject(input, settings); + return (T)Deserialize(input, typeof(T)); } /// @@ -196,7 +190,11 @@ public object Deserialize(string input, Type type) { return null; } - return JsonConvert.DeserializeObject(input, type, settings); + + using (JsonTextReader reader = new JsonTextReader(new StringReader(input))) + { + return serializer.Deserialize(reader, type); + } } /// @@ -229,4 +227,4 @@ public async Task DeserializeAsync(Stream input, CancellationToken cancell } } } -} +}