| | | 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 : IMatcher |
| | | 14 | | { |
| | | 15 | | private readonly string[] _patterns; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Initializes a new instance of the <see cref="JsonPathMatcher"/> class. |
| | | 19 | | /// </summary> |
| | | 20 | | /// <param name="patterns">The patterns.</param> |
| | 2 | 21 | | public JsonPathMatcher([NotNull] params string[] patterns) |
| | 2 | 22 | | { |
| | 2 | 23 | | Check.NotNull(patterns, nameof(patterns)); |
| | | 24 | | |
| | 2 | 25 | | _patterns = patterns; |
| | 2 | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Determines whether the specified input is match. |
| | | 30 | | /// </summary> |
| | | 31 | | /// <param name="input">The input string</param> |
| | | 32 | | /// <returns>A value between 0.0 - 1.0 of the similarity.</returns> |
| | | 33 | | public double IsMatch(string input) |
| | 2 | 34 | | { |
| | 2 | 35 | | if (input == null) |
| | 0 | 36 | | return MatchScores.Mismatch; |
| | | 37 | | |
| | | 38 | | try |
| | 2 | 39 | | { |
| | 2 | 40 | | JObject o = JObject.Parse(input); |
| | | 41 | | |
| | 4 | 42 | | return MatchScores.ToScore(_patterns.Select(p => o.SelectToken(p) != null)); |
| | | 43 | | } |
| | 0 | 44 | | catch (Exception) |
| | 0 | 45 | | { |
| | 0 | 46 | | return MatchScores.Mismatch; |
| | | 47 | | } |
| | 2 | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Gets the patterns. |
| | | 52 | | /// </summary> |
| | | 53 | | /// <returns>Pattern</returns> |
| | | 54 | | public string[] GetPatterns() |
| | 0 | 55 | | { |
| | 0 | 56 | | return _patterns; |
| | 0 | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Gets the name. |
| | | 61 | | /// </summary> |
| | | 62 | | /// <returns>Name</returns> |
| | | 63 | | public string GetName() |
| | 0 | 64 | | { |
| | 0 | 65 | | return "JsonPathMatcher"; |
| | 0 | 66 | | } |
| | | 67 | | } |
| | | 68 | | } |