-Fixed unhelpful error message when deserializing badly formatted regex string
This commit is contained in:
@@ -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