using System;
using JetBrains.Annotations;
using Newtonsoft.Json.Linq;
using WireMock.Validation;
namespace WireMock.Matchers
{
///
/// JSONPathMatcher
///
///
public class JsonPathMatcher : IMatcher
{
private readonly string _pattern;
///
/// Initializes a new instance of the class.
///
/// The pattern.
public JsonPathMatcher([NotNull] string pattern)
{
Check.NotNull(pattern, nameof(pattern));
_pattern = pattern;
}
///
/// Determines whether the specified input is match.
///
/// The input.
///
/// true if the specified input is match; otherwise, false.
///
public bool IsMatch(string input)
{
if (input == null)
return false;
try
{
JObject o = JObject.Parse(input);
JToken token = o.SelectToken(_pattern);
return token != null;
}
catch (Exception)
{
return false;
}
}
///
/// Gets the pattern.
///
/// Pattern
public string GetPattern()
{
return _pattern;
}
}
}