Skip to content

Commit

Permalink
-Changed StringEnumConverter.AllowIntegerValues to also reject writin…
Browse files Browse the repository at this point in the history
…g enum values with no name
  • Loading branch information
JamesNK committed Mar 19, 2017
1 parent 937b25b commit 7673a18
Show file tree
Hide file tree
Showing 16 changed files with 103 additions and 861 deletions.
28 changes: 0 additions & 28 deletions Src/Newtonsoft.Json.Portable40.sln

This file was deleted.

11 changes: 10 additions & 1 deletion Src/Newtonsoft.Json.Tests/Converters/StringEnumConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -612,12 +612,21 @@ public void AllowIntegerValueAndStringNumber()
{
JsonSerializationException ex = ExceptionAssert.Throws<JsonSerializationException>(() =>
{
JsonConvert.DeserializeObject<StoreColor>("\"1\"", new StringEnumConverter {AllowIntegerValues = false});
JsonConvert.DeserializeObject<StoreColor>("\"1\"", new StringEnumConverter { AllowIntegerValues = false });
});

Assert.AreEqual("Integer string '1' is not allowed.", ex.InnerException.Message);
}

[Test]
public void AllowIntegerValueAndNonNamedValue()
{
ExceptionAssert.Throws<JsonSerializationException>(() =>
{
JsonConvert.SerializeObject((StoreColor)999, new StringEnumConverter { AllowIntegerValues = false });
}, "Integer value 999 is not allowed. Path ''.");
}

public enum EnumWithDifferentCases
{
M,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,7 @@ public void OtherElementDataTypes()
[Test]
public void NoRootObject()
{
ExceptionAssert.Throws<JsonSerializationException>(() => { XmlDocument newDoc = (XmlDocument)JsonConvert.DeserializeXmlNode(@"[1]"); }, "XmlNodeConverter can only convert JSON that begins with an object.");
ExceptionAssert.Throws<JsonSerializationException>(() => { XmlDocument newDoc = (XmlDocument)JsonConvert.DeserializeXmlNode(@"[1]"); }, "XmlNodeConverter can only convert JSON that begins with an object. Path '', line 1, position 1.");
}

[Test]
Expand Down
431 changes: 0 additions & 431 deletions Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Portable40.csproj

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<DebugType Condition="'$(TargetFramework)' != '' AND '$(TargetFramework)' != 'netcoreapp1.0'">Full</DebugType>
</PropertyGroup>
<ItemGroup>
<None Remove="**\*.orig" />
<Compile Condition="'$(TargetFramework)' != '' AND '$(TargetFramework)' != 'net45'" Remove="Benchmarks\**" />
<None Include="large.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand Down
5 changes: 5 additions & 0 deletions Src/Newtonsoft.Json/Converters/StringEnumConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s

if (char.IsNumber(enumName[0]) || enumName[0] == '-')
{
if (!AllowIntegerValues)
{
throw JsonSerializationException.Create(null, writer.ContainerPath, "Integer value {0} is not allowed.".FormatWith(CultureInfo.InvariantCulture, enumName), null);
}

// enum value has no name so write number
writer.WriteValue(value);
}
Expand Down
10 changes: 5 additions & 5 deletions Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1617,7 +1617,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
case JsonToken.StartObject:
break;
default:
throw new JsonSerializationException("XmlNodeConverter can only convert JSON that begins with an object.");
throw JsonSerializationException.Create(reader, "XmlNodeConverter can only convert JSON that begins with an object.");
}

XmlNamespaceManager manager = new XmlNamespaceManager(new NameTable());
Expand All @@ -1629,7 +1629,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
{
if (objectType != typeof(XDocument) && objectType != typeof(XElement))
{
throw new JsonSerializationException("XmlNodeConverter only supports deserializing XDocument or XElement.");
throw JsonSerializationException.Create(reader, "XmlNodeConverter only supports deserializing XDocument or XElement.");
}

XDocument d = new XDocument();
Expand All @@ -1642,7 +1642,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
{
if (objectType != typeof(XmlDocument))
{
throw new JsonSerializationException("XmlNodeConverter only supports deserializing XmlDocuments");
throw JsonSerializationException.Create(reader, "XmlNodeConverter only supports deserializing XmlDocuments");
}

XmlDocument d = new XmlDocument();
Expand All @@ -1656,9 +1656,9 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
}
#endif

if (document == null || rootNode == null)
if (document == null || rootNode == null)
{
throw new JsonSerializationException("Unexpected type when converting XML: " + objectType);
throw JsonSerializationException.Create(reader, "Unexpected type when converting XML: " + objectType);
}

if (!string.IsNullOrEmpty(DeserializeRootElementName))
Expand Down
2 changes: 1 addition & 1 deletion Src/Newtonsoft.Json/Linq/JValue.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public override Task WriteToAsync(JsonWriter writer, CancellationToken cancellat
return writer.WriteValueAsync((Uri)_value, cancellationToken);
}

throw MiscellaneousUtils.CreateArgumentOutOfRangeException("TokenType", _valueType, "Unexpected token type.");
throw MiscellaneousUtils.CreateArgumentOutOfRangeException(nameof(Type), _valueType, "Unexpected token type.");
}
}
}
Expand Down

2 comments on commit 7673a18

@JamesNK
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Older nugget clients don't support it

@TylerBrinkley
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, thanks.

Please sign in to comment.