123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442 |
- using System;
- using System.Collections;
- using System.Diagnostics;
- using System.Globalization;
- using System.Text;
- using System.Text.RegularExpressions;
- namespace TEAMModelOS.SDK.Extension.JsonPath
- {
- public delegate object JsonPathScriptEvaluator(string script, object value, string context);
- public delegate void JsonPathResultAccumulator(object value, string[] indicies);
- [Serializable]
- public sealed class JsonPathNode
- {
- private readonly object value;
- private readonly string path;
- public JsonPathNode(object value, string path)
- {
- if (path == null)
- throw new ArgumentNullException("path");
- if (path.Length == 0)
- throw new ArgumentException("path");
- this.value = value;
- this.path = path;
- }
- public object Value
- {
- get { return value; }
- }
- public string Path
- {
- get { return path; }
- }
- public override string ToString()
- {
- return Path + " = " + Value;
- }
- public static object[] ValuesFrom(ICollection nodes)
- {
- object[] values = new object[nodes != null ? nodes.Count : 0];
- if (values.Length > 0)
- {
- Debug.Assert(nodes != null);
- int i = 0;
- foreach (JsonPathNode node in nodes)
- values[i++] = node.Value;
- }
- return values;
- }
- public static string[] PathsFrom(ICollection nodes)
- {
- string[] paths = new string[nodes != null ? nodes.Count : 0];
- if (paths.Length > 0)
- {
- Debug.Assert(nodes != null);
- int i = 0;
- foreach (JsonPathNode node in nodes)
- paths[i++] = node.Path;
- }
- return paths;
- }
- }
- public sealed class JsonPathContext
- {
- public static readonly JsonPathContext Default = new JsonPathContext();
- private JsonPathScriptEvaluator eval;
- private IJsonPathValueSystem system;
- public JsonPathScriptEvaluator ScriptEvaluator
- {
- get { return eval; }
- set { eval = value; }
- }
- public IJsonPathValueSystem ValueSystem
- {
- get { return system; }
- set { system = value; }
- }
- public void SelectTo(object obj, string expr, JsonPathResultAccumulator output)
- {
- if (obj == null)
- throw new ArgumentNullException("obj");
- if (output == null)
- throw new ArgumentNullException("output");
- Interpreter i = new Interpreter(output, ValueSystem, ScriptEvaluator);
- expr = Normalize(expr);
- if (expr.Length >= 1 && expr[0] == '$') // ^\$:?
- expr = expr.Substring(expr.Length >= 2 && expr[1] == ';' ? 2 : 1);
- i.Trace(expr, obj, "$");
- }
- public JsonPathNode[] SelectNodes(object obj, string expr)
- {
- ArrayList list = new ArrayList();
- SelectNodesTo(obj, expr, list);
- return (JsonPathNode[])list.ToArray(typeof(JsonPathNode));
- }
- public IList SelectNodesTo(object obj, string expr, IList output)
- {
- ListAccumulator accumulator = new ListAccumulator(output != null ? output : new ArrayList());
- SelectTo(obj, expr, new JsonPathResultAccumulator(accumulator.Put));
- return output;
- }
- private static Regex RegExp(string pattern)
- {
- return new Regex(pattern, RegexOptions.ECMAScript);
- }
- private static string Normalize(string expr)
- {
- NormalizationSwap swap = new NormalizationSwap();
- expr = RegExp(@"[\['](\??\(.*?\))[\]']").Replace(expr, new MatchEvaluator(swap.Capture));
- expr = RegExp(@"'?\.'?|\['?").Replace(expr, ";");
- expr = RegExp(@";;;|;;").Replace(expr, ";..;");
- expr = RegExp(@";$|'?\]|'$").Replace(expr, string.Empty);
- expr = RegExp(@"#([0-9]+)").Replace(expr, new MatchEvaluator(swap.Yield));
- return expr;
- }
- private sealed class NormalizationSwap
- {
- private readonly ArrayList subx = new ArrayList(4);
- public string Capture(Match match)
- {
- Debug.Assert(match != null);
- int index = subx.Add(match.Groups[1].Value);
- return "[#" + index.ToString(CultureInfo.InvariantCulture) + "]";
- }
- public string Yield(Match match)
- {
- Debug.Assert(match != null);
- int index = int.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture);
- return (string)subx[index];
- }
- }
- public static string AsBracketNotation(string[] indicies)
- {
- if (indicies == null)
- throw new ArgumentNullException("indicies");
- StringBuilder sb = new StringBuilder();
- foreach (string index in indicies)
- {
- if (sb.Length == 0)
- {
- sb.Append('$');
- }
- else
- {
- sb.Append('[');
- if (RegExp(@"^[0-9*]+$").IsMatch(index))
- sb.Append(index);
- else
- sb.Append('\'').Append(index).Append('\'');
- sb.Append(']');
- }
- }
- return sb.ToString();
- }
- private static int ParseInt(string s)
- {
- return ParseInt(s, 0);
- }
- private static int ParseInt(string str, int defaultValue)
- {
- if (str == null || str.Length == 0)
- return defaultValue;
- try
- {
- return int.Parse(str, NumberStyles.None, CultureInfo.InvariantCulture);
- }
- catch (FormatException)
- {
- return defaultValue;
- }
- }
- private sealed class Interpreter
- {
- private readonly JsonPathResultAccumulator output;
- private readonly JsonPathScriptEvaluator eval;
- private readonly IJsonPathValueSystem system;
- private static readonly IJsonPathValueSystem defaultValueSystem = new BasicValueSystem();
- private static readonly char[] colon = new char[] { ':' };
- private static readonly char[] semicolon = new char[] { ';' };
- private delegate void WalkCallback(object member, string loc, string expr, object value, string path);
- public Interpreter(JsonPathResultAccumulator output, IJsonPathValueSystem valueSystem, JsonPathScriptEvaluator eval)
- {
- Debug.Assert(output != null);
- this.output = output;
- this.eval = eval != null ? eval : new JsonPathScriptEvaluator(NullEval);
- this.system = valueSystem != null ? valueSystem : defaultValueSystem;
- }
- public void Trace(string expr, object value, string path)
- {
- if (expr == null || expr.Length == 0)
- {
- Store(path, value);
- return;
- }
- int i = expr.IndexOf(';');
- string atom = i >= 0 ? expr.Substring(0, i) : expr;
- string tail = i >= 0 ? expr.Substring(i + 1) : string.Empty;
- if (value != null && system.HasMember(value, atom))
- {
- Trace(tail, Index(value, atom), path + ";" + atom);
- }
- else if (atom.Equals("*"))
- {
- Walk(atom, tail, value, path, new WalkCallback(WalkWild));
- }
- else if (atom.Equals(".."))
- {
- Trace(tail, value, path);
- Walk(atom, tail, value, path, new WalkCallback(WalkTree));
- }
- else if (atom.Length > 2 && atom[0] == '(' && atom[atom.Length - 1] == ')') // [(exp)]
- {
- Trace(eval(atom, value, path.Substring(path.LastIndexOf(';') + 1)) + ";" + tail, value, path);
- }
- else if (atom.Length > 3 && atom[0] == '?' && atom[1] == '(' && atom[atom.Length - 1] == ')') // [?(exp)]
- {
- Walk(atom, tail, value, path, new WalkCallback(WalkFiltered));
- }
- else if (RegExp(@"^(-?[0-9]*):(-?[0-9]*):?([0-9]*)$").IsMatch(atom)) // [start:end:step] Phyton slice syntax
- {
- Slice(atom, tail, value, path);
- }
- else if (atom.IndexOf(',') >= 0) // [name1,name2,...]
- {
- foreach (string part in RegExp(@"'?,'?").Split(atom))
- Trace(part + ";" + tail, value, path);
- }
- }
- private void Store(string path, object value)
- {
- if (path != null)
- output(value, path.Split(semicolon));
- }
- private void Walk(string loc, string expr, object value, string path, WalkCallback callback)
- {
- if (system.IsPrimitive(value))
- return;
- if (system.IsArray(value))
- {
- IList list = (IList)value;
- for (int i = 0; i < list.Count; i++)
- callback(i, loc, expr, value, path);
- }
- else if (system.IsObject(value))
- {
- foreach (string key in system.GetMembers(value))
- callback(key, loc, expr, value, path);
- }
- }
- private void WalkWild(object member, string loc, string expr, object value, string path)
- {
- Trace(member + ";" + expr, value, path);
- }
- private void WalkTree(object member, string loc, string expr, object value, string path)
- {
- object result = Index(value, member.ToString());
- if (result != null && !system.IsPrimitive(result))
- Trace("..;" + expr, result, path + ";" + member);
- }
- private void WalkFiltered(object member, string loc, string expr, object value, string path)
- {
- object result = eval(RegExp(@"^\?\((.*?)\)$").Replace(loc, "$1"),
- Index(value, member.ToString()), member.ToString());
- if (Convert.ToBoolean(result, CultureInfo.InvariantCulture))
- Trace(member + ";" + expr, value, path);
- }
- private void Slice(string loc, string expr, object value, string path)
- {
- IList list = value as IList;
- if (list == null)
- return;
- int length = list.Count;
- string[] parts = loc.Split(colon);
- int start = ParseInt(parts[0]);
- int end = ParseInt(parts[1], list.Count);
- int step = parts.Length > 2 ? ParseInt(parts[2], 1) : 1;
- start = (start < 0) ? Math.Max(0, start + length) : Math.Min(length, start);
- end = (end < 0) ? Math.Max(0, end + length) : Math.Min(length, end);
- for (int i = start; i < end; i += step)
- Trace(i + ";" + expr, value, path);
- }
- private object Index(object obj, string member)
- {
- return system.GetMemberValue(obj, member);
- }
- private static object NullEval(string expr, object value, string context)
- {
- //
- // @ symbol in expr must be interpreted specially to resolve
- // to value. In JavaScript, the implementation would look
- // like:
- //
- // return obj && value && eval(expr.replace(/@/g, "value"));
- //
- return null;
- }
- }
- private sealed class BasicValueSystem : IJsonPathValueSystem
- {
- public bool HasMember(object value, string member)
- {
- if (IsPrimitive(value))
- return false;
- IDictionary dict = value as IDictionary;
- if (dict != null)
- return dict.Contains(member);
- IList list = value as IList;
- if (list != null)
- {
- int index = ParseInt(member, -1);
- return index >= 0 && index < list.Count;
- }
- return false;
- }
- public object GetMemberValue(object value, string member)
- {
- if (IsPrimitive(value))
- throw new ArgumentException("value");
- IDictionary dict = value as IDictionary;
- if (dict != null)
- return dict[member];
- IList list = (IList)value;
- int index = ParseInt(member, -1);
- if (index >= 0 && index < list.Count)
- return list[index];
- return null;
- }
- public IEnumerable GetMembers(object value)
- {
- return ((IDictionary)value).Keys;
- }
- public bool IsObject(object value)
- {
- return value is IDictionary;
- }
- public bool IsArray(object value)
- {
- return value is IList;
- }
- public bool IsPrimitive(object value)
- {
- if (value == null)
- throw new ArgumentNullException("value");
- return Type.GetTypeCode(value.GetType()) != TypeCode.Object;
- }
- }
- private sealed class ListAccumulator
- {
- private readonly IList list;
- public ListAccumulator(IList list)
- {
- Debug.Assert(list != null);
- this.list = list;
- }
- public void Put(object value, string[] indicies)
- {
- list.Add(new JsonPathNode(value, JsonPathContext.AsBracketNotation(indicies)));
- }
- }
- }
- }
|