-Fixed unhelpful error message when deserializing badly formatted regex string
This commit is contained in:
+3
-16
@@ -83,27 +83,14 @@ and write from your objects.</para>
|
||||
<linkTarget>_blank</linkTarget>
|
||||
</externalLink>,
|
||||
an async library for building real-time, multi-user interactive web applications; and <externalLink>
|
||||
<linkText>ASP.NET Web API</linkText>
|
||||
<linkUri>http://www.asp.net/web-api</linkUri>
|
||||
<linkText>ASP.NET Core</linkText>
|
||||
<linkUri>http://www.asp.net</linkUri>
|
||||
<linkTarget>_blank</linkTarget>
|
||||
</externalLink>,
|
||||
Microsoft's HTTP service framework.</para>
|
||||
Microsoft's web app and service framework.</para>
|
||||
</content>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<title>Donate</title>
|
||||
<content><para>Json.NET is a personal open source project. Started in 2006, I have put thousands of hours into adding, refining, and tuning Json.NET with the goal of making it not just the best JSON serializer for .NET, but the best serializer for any computer language. I need your
|
||||
help to achieve this.</para>
|
||||
|
||||
<markup>
|
||||
<p>
|
||||
<a href="https://pledgie.com/campaigns/18941" target="_blank"><img src="https://pledgie.com/campaigns/18941.png?skin_name=chrome" alt="Click here to lend your support to: Json.NET and make a donation at www.pledgie.com !" border="0" /></a>
|
||||
</p>
|
||||
</markup>
|
||||
</content>
|
||||
</section>
|
||||
|
||||
<relatedTopics>
|
||||
<link xlink:href="SerializingJSON" />
|
||||
<link xlink:href="LINQtoJSON" />
|
||||
|
||||
@@ -126,6 +126,67 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
Assert.AreEqual(RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture, r.Regex.Options);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeStringRegex()
|
||||
{
|
||||
string json = @"{
|
||||
""Regex"": ""\/abc\/""
|
||||
}";
|
||||
|
||||
RegexTestClass c = JsonConvert.DeserializeObject<RegexTestClass>(json, new JsonSerializerSettings
|
||||
{
|
||||
Converters = { new RegexConverter() }
|
||||
});
|
||||
|
||||
Assert.AreEqual("abc", c.Regex.ToString());
|
||||
Assert.AreEqual(RegexOptions.None, c.Regex.Options);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeStringRegex_NoStartSlash_Error()
|
||||
{
|
||||
string json = @"{
|
||||
""Regex"": ""abc\/""
|
||||
}";
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
() => JsonConvert.DeserializeObject<RegexTestClass>(json, new JsonSerializerSettings
|
||||
{
|
||||
Converters = { new RegexConverter() }
|
||||
}),
|
||||
"Regex pattern must be enclosed by slashes. Path 'Regex', line 2, position 18.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeStringRegex_NoEndSlash_Error()
|
||||
{
|
||||
string json = @"{
|
||||
""Regex"": ""\/abc""
|
||||
}";
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
() => JsonConvert.DeserializeObject<RegexTestClass>(json, new JsonSerializerSettings
|
||||
{
|
||||
Converters = {new RegexConverter()}
|
||||
}),
|
||||
"Regex pattern must be enclosed by slashes. Path 'Regex', line 2, position 18.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeStringRegex_NoStartAndEndSlashes_Error()
|
||||
{
|
||||
string json = @"{
|
||||
""Regex"": ""\/abc""
|
||||
}";
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
() => JsonConvert.DeserializeObject<RegexTestClass>(json, new JsonSerializerSettings
|
||||
{
|
||||
Converters = { new RegexConverter() }
|
||||
}),
|
||||
"Regex pattern must be enclosed by slashes. Path 'Regex', line 2, position 18.");
|
||||
}
|
||||
|
||||
#pragma warning disable 618
|
||||
[Test]
|
||||
public void SerializeToBson()
|
||||
|
||||
@@ -534,11 +534,11 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
string iPersonJson = JsonConvert.SerializeObject(employee, Formatting.Indented,
|
||||
new JsonSerializerSettings { ContractResolver = new IPersonContractResolver() });
|
||||
|
||||
StringAssert.AreEqual(@"{
|
||||
""FirstName"": ""Maurice"",
|
||||
""LastName"": ""Moss"",
|
||||
""BirthDate"": ""1977-12-30T01:01:01Z""
|
||||
}", iPersonJson);
|
||||
JObject o = JObject.Parse(iPersonJson);
|
||||
|
||||
Assert.AreEqual("Maurice", (string)o["FirstName"]);
|
||||
Assert.AreEqual("Moss", (string)o["LastName"]);
|
||||
Assert.AreEqual(new DateTime(1977, 12, 30, 1, 1, 1, DateTimeKind.Utc), (DateTime)o["BirthDate"]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -150,14 +150,23 @@ namespace Newtonsoft.Json.Converters
|
||||
private object ReadRegexString(JsonReader reader)
|
||||
{
|
||||
string regexText = (string)reader.Value;
|
||||
int patternOptionDelimiterIndex = regexText.LastIndexOf('/');
|
||||
|
||||
string patternText = regexText.Substring(1, patternOptionDelimiterIndex - 1);
|
||||
string optionsText = regexText.Substring(patternOptionDelimiterIndex + 1);
|
||||
if (regexText.Length > 0 && regexText[0] == '/')
|
||||
{
|
||||
int patternOptionDelimiterIndex = regexText.LastIndexOf('/');
|
||||
|
||||
RegexOptions options = MiscellaneousUtils.GetRegexOptions(optionsText);
|
||||
if (patternOptionDelimiterIndex > 0)
|
||||
{
|
||||
string patternText = regexText.Substring(1, patternOptionDelimiterIndex - 1);
|
||||
string optionsText = regexText.Substring(patternOptionDelimiterIndex + 1);
|
||||
|
||||
return new Regex(patternText, options);
|
||||
RegexOptions options = MiscellaneousUtils.GetRegexOptions(optionsText);
|
||||
|
||||
return new Regex(patternText, options);
|
||||
}
|
||||
}
|
||||
|
||||
throw JsonSerializationException.Create(reader, "Regex pattern must be enclosed by slashes.");
|
||||
}
|
||||
|
||||
private Regex ReadRegexObject(JsonReader reader, JsonSerializer serializer)
|
||||
|
||||
Reference in New Issue
Block a user