| | | 1 | | using System; |
| | | 2 | | using System.Linq; |
| | | 3 | | using JetBrains.Annotations; |
| | | 4 | | using Newtonsoft.Json.Linq; |
| | | 5 | | using WireMock.Validation; |
| | | 6 | | |
| | | 7 | | namespace WireMock.Matchers |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// JSONPathMatcher |
| | | 11 | | /// </summary> |
| | | 12 | | /// <seealso cref="IMatcher" /> |
| | | 13 | | public class JsonPathMatcher : IStringMatcher, IObjectMatcher |
| | | 14 | | { |
| | | 15 | | // private readonly object _jsonPattern; |
| | | 16 | | private readonly string[] _patterns; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Initializes a new instance of the <see cref="JsonPathMatcher"/> class. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="patterns">The patterns.</param> |
| | 7 | 22 | | public JsonPathMatcher([NotNull] params string[] patterns) |
| | 7 | 23 | | { |
| | 7 | 24 | | Check.NotNull(patterns, nameof(patterns)); |
| | | 25 | | |
| | 7 | 26 | | _patterns = patterns; |
| | 7 | 27 | | } |
| | | 28 | | |
| | | 29 | | //public JsonPathMatcher([NotNull] object jsonPattern) |
| | | 30 | | //{ |
| | | 31 | | // Check.NotNull(jsonPattern, nameof(jsonPattern)); |
| | | 32 | | |
| | | 33 | | // _jsonPattern = jsonPattern; |
| | | 34 | | //} |
| | | 35 | | |
| | | 36 | | /// <inheritdoc cref="IStringMatcher.IsMatch"/> |
| | | 37 | | public double IsMatch(string input) |
| | 2 | 38 | | { |
| | 2 | 39 | | if (input == null) |
| | 0 | 40 | | { |
| | 0 | 41 | | return MatchScores.Mismatch; |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | try |
| | 2 | 45 | | { |
| | 2 | 46 | | JObject o = JObject.Parse(input); |
| | | 47 | | |
| | 6 | 48 | | return MatchScores.ToScore(_patterns.Select(p => o.SelectToken(p) != null)); |
| | | 49 | | } |
| | 0 | 50 | | catch (Exception) |
| | 0 | 51 | | { |
| | 0 | 52 | | return MatchScores.Mismatch; |
| | | 53 | | } |
| | 2 | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <inheritdoc cref="IObjectMatcher.IsMatch"/> |
| | | 57 | | public double IsMatch(object input) |
| | 3 | 58 | | { |
| | 3 | 59 | | if (input == null) |
| | 0 | 60 | | { |
| | 0 | 61 | | return MatchScores.Mismatch; |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | try |
| | 3 | 65 | | { |
| | 3 | 66 | | var o = input as JObject ?? JObject.FromObject(input); |
| | | 67 | | |
| | 9 | 68 | | return MatchScores.ToScore(_patterns.Select(p => o.SelectToken(p) != null)); |
| | | 69 | | } |
| | 0 | 70 | | catch (Exception) |
| | 0 | 71 | | { |
| | 0 | 72 | | return MatchScores.Mismatch; |
| | | 73 | | } |
| | 3 | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <inheritdoc cref="IStringMatcher.GetPatterns"/> |
| | | 77 | | public string[] GetPatterns() |
| | 1 | 78 | | { |
| | 1 | 79 | | return _patterns; |
| | 1 | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <inheritdoc cref="IMatcher.GetName"/> |
| | | 83 | | public string GetName() |
| | 1 | 84 | | { |
| | 1 | 85 | | return "JsonPathMatcher"; |
| | 1 | 86 | | } |
| | | 87 | | } |
| | | 88 | | } |