Example #1
0
        public JSONSelector(JSONWalkingEvents events, string[] paths, JSONSelectorOptions options)
        {
            if (null == events)
            {
                throw new ArgumentNullException("events");
            }

            _events                 = events;
            paths                   = paths ?? new string[0];

            if (options.IgnoreArrays())
            {
                // Filter out paths containing array specifiers
                _paths              = paths.Where(p => -1 == p.IndexOf(ArraySpecifier))
                                        .ToDictionary<string, string>(p => p);
            }
            else
            {
                _paths              = paths.ToDictionary<string, string>(p => p);
            }

            _selectedValues         = new List<SelectedValue>();
            _options                = options;

            _objectPathStack        = new Stack<string>();
            _objectPath             = string.Empty;
            _memberPath             = string.Empty;

            _arrayIndex             = -1;
            _arrayIndexStack        = new Stack<int>();

            _events.ObjectStart     += new JSONEventHandler(_events_ObjectStart);
            _events.ObjectEnd       += new JSONEventHandler(_events_ObjectEnd);
            _events.ObjectKey       += new JSONEventHandler<string>(_events_ObjectKey);

            _events.ArrayStart      += new JSONEventHandler(_events_ArrayStart);
            _events.ArrayEnd        += new JSONEventHandler(_events_ArrayEnd);
            _events.ArrayNext       += new JSONEventHandler(_events_ArrayNext);

            _events.String          += new JSONEventHandler<string>(_events_String);
            _events.Number          += new JSONEventHandler<decimal>(_events_Number);
            _events.Null            += new JSONEventHandler(_events_Null);
            _events.Boolean         += new JSONEventHandler<bool>(_events_Boolean);
        }
Example #2
0
        public static SelectedValue[] SelectValues(TextReader json, string[] selectors, JSONSelectorOptions options)
        {
            if (null == json)
            {
                return new SelectedValue[0];
            }

            // Create tokenizer for the JSON text - this is fed to a Tree Walking Validator
            JSONTokenizer tokenizer = new JSONTokenizer(json);

            // Create an events object for the Tree Walker - events are fired as the walker
            // traverses nodes in the tree.
            JSONWalkingEvents events = new JSONWalkingEvents();

            // The selector subscribes to the Walker Events and selects the appropriate values from the
            // JSON tree.
            JSONSelector selector = new JSONSelector(events, selectors);

            // A JSON Tree Walker that validates the structure of the JSON and also fires events
            // while traversing the tree.
            JSONWalkingValidator walker = new JSONWalkingValidator();
            walker.Walk(tokenizer.GetEnumerator(), events);

            // Retrieve the values selected from the JSON.
            return selector.SelectedValues;
        }
Example #3
0
 private static bool IsOptionSet(this JSONSelectorOptions options, JSONSelectorOptions flagToCheck)
 {
     return (flagToCheck == (flagToCheck & options));
 }
Example #4
0
        public static SelectedValue[] SelectValues(string json, string[] selectors, JSONSelectorOptions options)
        {
            if (string.IsNullOrEmpty(json))
            {
                return new SelectedValue[0];
            }

            return SelectValues(new StringReader(json), selectors, options);
        }