diff --git a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs index 7655dd52..25915659 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs @@ -8153,6 +8153,53 @@ Path '', line 1, position 1."); ExceptionAssert.Throws(() => { s.Deserialize>(new JsonTextReader(new StringReader(json))); }, "Additional text encountered after finished reading JSON content: {. Path '', line 1, position 7."); } + [Test] + public void CheckAdditionalContentJustComment() + { + string json = "{one:1} // This is just a comment"; + + JsonSerializerSettings settings = new JsonSerializerSettings {CheckAdditionalContent = true}; + JsonSerializer s = JsonSerializer.Create(settings); + IDictionary o = s.Deserialize>(new JsonTextReader(new StringReader(json))); + + Assert.IsNotNull(o); + Assert.AreEqual(1, o["one"]); + } + + [Test] + public void CheckAdditionalContentJustMultipleComments() + { + string json = @"{one:1} // This is just a comment +/* This is just a comment +over multiple +lines.*/ + +// This is just another comment."; + + JsonSerializerSettings settings = new JsonSerializerSettings {CheckAdditionalContent = true}; + JsonSerializer s = JsonSerializer.Create(settings); + IDictionary o = s.Deserialize>(new JsonTextReader(new StringReader(json))); + + Assert.IsNotNull(o); + Assert.AreEqual(1, o["one"]); + } + + [Test] + public void CheckAdditionalContentCommentsThenAnotherObject() + { + string json = @"{one:1} // This is just a comment +/* This is just a comment +over multiple +lines.*/ + +// This is just another comment. But here comes an empty object. +{}"; + + JsonSerializerSettings settings = new JsonSerializerSettings { CheckAdditionalContent = true }; + JsonSerializer s = JsonSerializer.Create(settings); + ExceptionAssert.Throws(() => { s.Deserialize>(new JsonTextReader(new StringReader(json))); }, "Additional text encountered after finished reading JSON content: {. Path '', line 7, position 0."); + } + #if !(PORTABLE || DNXCORE50 || PORTABLE40) [Test] public void DeserializeException() diff --git a/Src/Newtonsoft.Json/Serialization/DefaultSerializationBinder.cs b/Src/Newtonsoft.Json/Serialization/DefaultSerializationBinder.cs index 23437891..1b84e74c 100644 --- a/Src/Newtonsoft.Json/Serialization/DefaultSerializationBinder.cs +++ b/Src/Newtonsoft.Json/Serialization/DefaultSerializationBinder.cs @@ -35,7 +35,11 @@ namespace Newtonsoft.Json.Serialization /// /// The default serialization binder used when resolving and loading classes from type names. /// - public class DefaultSerializationBinder : SerializationBinder, ISerializationBinder + public class DefaultSerializationBinder : +#pragma warning disable CS0618 // Type or member is obsolete + SerializationBinder, +#pragma warning restore CS0618 // Type or member is obsolete + ISerializationBinder { internal static readonly DefaultSerializationBinder Instance = new DefaultSerializationBinder(); diff --git a/Src/Newtonsoft.Json/Serialization/JsonArrayContract.cs b/Src/Newtonsoft.Json/Serialization/JsonArrayContract.cs index 3a301647..96afac5b 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonArrayContract.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonArrayContract.cs @@ -191,6 +191,11 @@ namespace Newtonsoft.Json.Serialization _genericCollectionDefinitionType = typeof(List<>).MakeGenericType(CollectionItemType); _parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(CreatedType, CollectionItemType); + +#if HAVE_FSHARP_TYPES + StoreFSharpListCreatorIfNecessary(underlyingType); +#endif + IsReadOnlyOrFixedSize = true; canDeserialize = HasParameterizedCreatorInternal; } @@ -207,11 +212,7 @@ namespace Newtonsoft.Json.Serialization _parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(underlyingType, CollectionItemType); #if HAVE_FSHARP_TYPES - if (!HasParameterizedCreatorInternal && underlyingType.Name == FSharpUtils.FSharpListTypeName) - { - FSharpUtils.EnsureInitialized(underlyingType.Assembly()); - _parameterizedCreator = FSharpUtils.CreateSeq(CollectionItemType); - } + StoreFSharpListCreatorIfNecessary(underlyingType); #endif if (underlyingType.IsGenericType() && underlyingType.GetGenericTypeDefinition() == typeof(IEnumerable<>)) @@ -306,5 +307,16 @@ namespace Newtonsoft.Json.Serialization return (IList)_genericTemporaryCollectionCreator(); } + +#if HAVE_FSHARP_TYPES + private void StoreFSharpListCreatorIfNecessary(Type underlyingType) + { + if (!HasParameterizedCreatorInternal && underlyingType.Name == FSharpUtils.FSharpListTypeName) + { + FSharpUtils.EnsureInitialized(underlyingType.Assembly()); + _parameterizedCreator = FSharpUtils.CreateSeq(CollectionItemType); + } + } +#endif } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs index ce4b123b..5ff29e78 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs @@ -169,9 +169,12 @@ namespace Newtonsoft.Json.Serialization if (checkAdditionalContent) { - if (reader.Read() && reader.TokenType != JsonToken.Comment) + while (reader.Read()) { - throw new JsonSerializationException("Additional text found in JSON string after finishing deserializing object."); + if (reader.TokenType != JsonToken.Comment) + { + throw new JsonSerializationException("Additional text found in JSON string after finishing deserializing object."); + } } } diff --git a/Src/Newtonsoft.Json/Serialization/SerializationBinderAdapter.cs b/Src/Newtonsoft.Json/Serialization/SerializationBinderAdapter.cs index 89d86f8b..033692c4 100644 --- a/Src/Newtonsoft.Json/Serialization/SerializationBinderAdapter.cs +++ b/Src/Newtonsoft.Json/Serialization/SerializationBinderAdapter.cs @@ -30,12 +30,16 @@ namespace Newtonsoft.Json.Serialization { internal class SerializationBinderAdapter : ISerializationBinder { +#pragma warning disable 618 public readonly SerializationBinder SerializationBinder; +#pragma warning restore 618 +#pragma warning disable 618 public SerializationBinderAdapter(SerializationBinder serializationBinder) { SerializationBinder = serializationBinder; } +#pragma warning restore 618 public Type BindToType(string assemblyName, string typeName) { diff --git a/Src/Newtonsoft.Json/SerializationBinder.cs b/Src/Newtonsoft.Json/SerializationBinder.cs index da34c77c..b3e480f0 100644 --- a/Src/Newtonsoft.Json/SerializationBinder.cs +++ b/Src/Newtonsoft.Json/SerializationBinder.cs @@ -8,6 +8,7 @@ namespace Newtonsoft.Json /// /// Allows users to control class loading and mandate what class to load. /// + [Obsolete("SerializationBinder is obsolete. Use ISerializationBinder instead.")] public abstract class SerializationBinder { ///