This commit is contained in:
James Newton-King
2017-01-17 11:44:24 +13:00
6 changed files with 79 additions and 8 deletions
@@ -8153,6 +8153,53 @@ Path '', line 1, position 1.");
ExceptionAssert.Throws<JsonReaderException>(() => { s.Deserialize<Dictionary<string, int>>(new JsonTextReader(new StringReader(json))); }, "Additional text encountered after finished reading JSON content: {. Path '', line 1, position 7."); ExceptionAssert.Throws<JsonReaderException>(() => { s.Deserialize<Dictionary<string, int>>(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<string, int> o = s.Deserialize<Dictionary<string, int>>(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<string, int> o = s.Deserialize<Dictionary<string, int>>(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<JsonReaderException>(() => { s.Deserialize<Dictionary<string, int>>(new JsonTextReader(new StringReader(json))); }, "Additional text encountered after finished reading JSON content: {. Path '', line 7, position 0.");
}
#if !(PORTABLE || DNXCORE50 || PORTABLE40) #if !(PORTABLE || DNXCORE50 || PORTABLE40)
[Test] [Test]
public void DeserializeException() public void DeserializeException()
@@ -35,7 +35,11 @@ namespace Newtonsoft.Json.Serialization
/// <summary> /// <summary>
/// The default serialization binder used when resolving and loading classes from type names. /// The default serialization binder used when resolving and loading classes from type names.
/// </summary> /// </summary>
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(); internal static readonly DefaultSerializationBinder Instance = new DefaultSerializationBinder();
@@ -191,6 +191,11 @@ namespace Newtonsoft.Json.Serialization
_genericCollectionDefinitionType = typeof(List<>).MakeGenericType(CollectionItemType); _genericCollectionDefinitionType = typeof(List<>).MakeGenericType(CollectionItemType);
_parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(CreatedType, CollectionItemType); _parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(CreatedType, CollectionItemType);
#if HAVE_FSHARP_TYPES
StoreFSharpListCreatorIfNecessary(underlyingType);
#endif
IsReadOnlyOrFixedSize = true; IsReadOnlyOrFixedSize = true;
canDeserialize = HasParameterizedCreatorInternal; canDeserialize = HasParameterizedCreatorInternal;
} }
@@ -207,11 +212,7 @@ namespace Newtonsoft.Json.Serialization
_parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(underlyingType, CollectionItemType); _parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(underlyingType, CollectionItemType);
#if HAVE_FSHARP_TYPES #if HAVE_FSHARP_TYPES
if (!HasParameterizedCreatorInternal && underlyingType.Name == FSharpUtils.FSharpListTypeName) StoreFSharpListCreatorIfNecessary(underlyingType);
{
FSharpUtils.EnsureInitialized(underlyingType.Assembly());
_parameterizedCreator = FSharpUtils.CreateSeq(CollectionItemType);
}
#endif #endif
if (underlyingType.IsGenericType() && underlyingType.GetGenericTypeDefinition() == typeof(IEnumerable<>)) if (underlyingType.IsGenericType() && underlyingType.GetGenericTypeDefinition() == typeof(IEnumerable<>))
@@ -306,5 +307,16 @@ namespace Newtonsoft.Json.Serialization
return (IList)_genericTemporaryCollectionCreator(); 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
} }
} }
@@ -169,9 +169,12 @@ namespace Newtonsoft.Json.Serialization
if (checkAdditionalContent) 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.");
}
} }
} }
@@ -30,12 +30,16 @@ namespace Newtonsoft.Json.Serialization
{ {
internal class SerializationBinderAdapter : ISerializationBinder internal class SerializationBinderAdapter : ISerializationBinder
{ {
#pragma warning disable 618
public readonly SerializationBinder SerializationBinder; public readonly SerializationBinder SerializationBinder;
#pragma warning restore 618
#pragma warning disable 618
public SerializationBinderAdapter(SerializationBinder serializationBinder) public SerializationBinderAdapter(SerializationBinder serializationBinder)
{ {
SerializationBinder = serializationBinder; SerializationBinder = serializationBinder;
} }
#pragma warning restore 618
public Type BindToType(string assemblyName, string typeName) public Type BindToType(string assemblyName, string typeName)
{ {
@@ -8,6 +8,7 @@ namespace Newtonsoft.Json
/// <summary> /// <summary>
/// Allows users to control class loading and mandate what class to load. /// Allows users to control class loading and mandate what class to load.
/// </summary> /// </summary>
[Obsolete("SerializationBinder is obsolete. Use ISerializationBinder instead.")]
public abstract class SerializationBinder public abstract class SerializationBinder
{ {
/// <summary> /// <summary>