diff --git a/src/WireMock.Net/Matchers/JsonMatcher.cs b/src/WireMock.Net/Matchers/JsonMatcher.cs index 467bd062a..566e232ed 100644 --- a/src/WireMock.Net/Matchers/JsonMatcher.cs +++ b/src/WireMock.Net/Matchers/JsonMatcher.cs @@ -1,7 +1,8 @@ -using System.Linq; -using JetBrains.Annotations; +using JetBrains.Annotations; using Newtonsoft.Json; using Newtonsoft.Json.Linq; +using System.Linq; +using WireMock.Util; using WireMock.Validation; namespace WireMock.Matchers @@ -93,7 +94,7 @@ public double IsMatch(object input) break; case string stringValue: - jtokenValue = JToken.Parse(stringValue); + jtokenValue = JsonUtils.Parse(stringValue); break; default: diff --git a/src/WireMock.Net/Transformers/HandleBarsJsonPath.cs b/src/WireMock.Net/Transformers/HandleBarsJsonPath.cs index d7eacadca..7737b062c 100644 --- a/src/WireMock.Net/Transformers/HandleBarsJsonPath.cs +++ b/src/WireMock.Net/Transformers/HandleBarsJsonPath.cs @@ -1,8 +1,9 @@ -using System; -using System.Linq; -using HandlebarsDotNet; +using HandlebarsDotNet; using Newtonsoft.Json; using Newtonsoft.Json.Linq; +using System; +using System.Linq; +using WireMock.Util; using WireMock.Validation; namespace WireMock.Transformers @@ -56,7 +57,7 @@ private static (JObject valueToProcess, string jsonpath) ParseArguments(object[] switch (arguments[0]) { case string jsonAsString: - valueToProcess = JObject.Parse(jsonAsString); + valueToProcess = JsonUtils.Parse(jsonAsString); break; case JObject jsonAsJObject: @@ -67,7 +68,7 @@ private static (JObject valueToProcess, string jsonpath) ParseArguments(object[] throw new NotSupportedException($"The value '{arguments[0]}' with type '{arguments[0]?.GetType()}' cannot be used in Handlebars JsonPath."); } - return (valueToProcess, (string) arguments[1]); + return (valueToProcess, (string)arguments[1]); } } } \ No newline at end of file diff --git a/src/WireMock.Net/Util/JsonUtils.cs b/src/WireMock.Net/Util/JsonUtils.cs index 79ccba9e7..a1ed84692 100644 --- a/src/WireMock.Net/Util/JsonUtils.cs +++ b/src/WireMock.Net/Util/JsonUtils.cs @@ -1,4 +1,5 @@ -using Newtonsoft.Json.Linq; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Linq; @@ -8,6 +9,22 @@ namespace WireMock.Util { internal static class JsonUtils { + private static readonly JsonSerializerSettings JsonSerializerSettings = new JsonSerializerSettings + { + DateParseHandling = DateParseHandling.None + }; + + /// + /// Load a Newtonsoft.Json.Linq.JObject from a string that contains JSON. + /// Using : DateParseHandling = DateParseHandling.None + /// + /// A System.String that contains JSON. + /// A Newtonsoft.Json.Linq.JObject populated from the string that contains JSON. + public static JObject Parse(string json) + { + return JsonConvert.DeserializeObject(json, JsonSerializerSettings); + } + public static T ParseJTokenToObject(object value) { switch (value) diff --git a/test/WireMock.Net.Tests/Matchers/JsonMatcherTests.cs b/test/WireMock.Net.Tests/Matchers/JsonMatcherTests.cs index 87ff302a5..f25d4d6d2 100644 --- a/test/WireMock.Net.Tests/Matchers/JsonMatcherTests.cs +++ b/test/WireMock.Net.Tests/Matchers/JsonMatcherTests.cs @@ -192,5 +192,22 @@ public void JsonMatcher_IsMatch_JObjectAsString_RejectOnMatch() // Assert Assert.Equal(0.0, match); } + + [Fact] + public void JsonMatcher_IsMatch_JObjectWithDateTimeOffsetAsString() + { + // Assign + var matcher = new JsonMatcher("{ \"preferredAt\" : \"2019-11-21T10:32:53.2210009+00:00\" }"); + + // Act + var jobject = new JObject + { + { "preferredAt", new JValue("2019-11-21T10:32:53.2210009+00:00") } + }; + double match = matcher.IsMatch(jobject); + + // Assert + Assert.Equal(1.0, match); + } } } \ No newline at end of file