-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]
|
||||
|
||||
Reference in New Issue
Block a user