/// <summary>
        /// Walks JSON tokens and validates and fires events on the supplied events instance.
        /// </summary>
        /// <param name="tokens"></param>
        /// <param name="events"></param>
        public void Walk(JSONTokenStream tokens, JSONWalkingEvents events)
        {
            _Start(tokens, events);

            tokens.MoveNext();

            if (JSONTokenType.EOF != tokens.Current.Type)
            {
                throw new JSONValidationException("Expected EOF but got: " + tokens.Current.Type);
            }
        }
        /// <summary>
        /// Reads an Array.  Fires ArrayStart, ArrayNext, and ArrayEnd.
        /// </summary>
        /// <param name="tokens"></param>
        /// <param name="events"></param>
        private void _Array(JSONTokenStream tokens, JSONWalkingEvents events)
        {
            if (null != events)
            {
                events.RaiseArrayStart();
            }

            tokens.MoveNext();
            JSONToken next = tokens.Current;

            while (JSONTokenType.ARRAY_END != next.Type)
            {
                _ArrayElement(tokens, events);

                tokens.MoveNext();
                next = tokens.Current;

                if (JSONTokenType.COMMA == next.Type)
                {
                    tokens.MoveNext();
                    next = tokens.Current;

                    if (null != events)
                    {
                        events.RaiseArrayNext();
                    }
                }
            }

            if (JSONTokenType.ARRAY_END == next.Type)
            {
                if (null != events)
                {
                    events.RaiseArrayEnd();
                }
                return;
            }
            else
            {
                throw new JSONValidationException("Expected VALUE or end of ARRAY but got: " + next.Type);
            }
        }
        /// <summary>
        /// Start walking the JSON structure.
        /// </summary>
        /// <param name="tokens"></param>
        /// <param name="events"></param>
        private void _Start(JSONTokenStream tokens, JSONWalkingEvents events)
        {
            tokens.MoveNext();
            JSONToken next = tokens.Current;

            if (JSONTokenType.OBJECT_START == next.Type)
            {
                _Object(tokens, events);
            }
            else if (JSONTokenType.ARRAY_START == next.Type)
            {
                _Array(tokens, events);
            }
            else
            {
                throw new JSONValidationException("Expected OBJECT or ARRAY but got: " + next.Type);
            }
        }
        /// <summary>
        /// Walk an Object.  Fires ObjectStart, ObjectKey, ObjectNext, and ObjectEnd
        /// </summary>
        /// <param name="tokens"></param>
        /// <param name="events"></param>
        private void _Object(JSONTokenStream tokens, JSONWalkingEvents events)
        {
            if (null != events)
            {
                events.RaiseObjectStart();
            }

            tokens.MoveNext();
            JSONToken next = tokens.Current;

            while (JSONTokenType.OBJECT_END != next.Type)
            {
                _ObjectField(tokens, events);

                tokens.MoveNext();
                next = tokens.Current;

                if (JSONTokenType.COMMA == next.Type)
                {
                    tokens.MoveNext();
                    next = tokens.Current;

                    if (null != events)
                    {
                        events.RaiseObjectNext();
                    }
                }
            }

            if (JSONTokenType.OBJECT_END == next.Type)
            {
                if (null != events)
                {
                    events.RaiseObjectEnd();
                }
                return;
            }
            else
            {
                throw new JSONValidationException("Expected VALUE or end of OBJECT but got: " + next.Type);
            }
        }
        private void _ObjectField(JSONTokenStream tokens, JSONWalkingEvents events)
        {
            JSONToken next = tokens.Current;

            if (JSONTokenType.STRING == next.Type)
            {
                if (null != events)
                {
                    events.RaiseObjectKey(next.StrValue);
                }

                tokens.MoveNext();
                next = tokens.Current;
                if (JSONTokenType.COLON == next.Type)
                {
                    tokens.MoveNext();
                    next = tokens.Current;
                    switch (next.Type)
                    {
                        case JSONTokenType.STRING:
                            if (null != events)
                            {
                                events.RaiseString(next.StrValue);
                            }
                            break;
                        case JSONTokenType.NUMBER:
                            if (null != events)
                            {
                                events.RaiseNumber(next.NumValue);
                            }
                            break;
                        case JSONTokenType.KEYWORD_TRUE:
                            if (null != events)
                            {
                                events.RaiseBoolean(true);
                            }
                            break;
                        case JSONTokenType.KEYWORD_FALSE:
                            if (null != events)
                            {
                                events.RaiseBoolean(false);
                            }
                            break;
                        case JSONTokenType.KEYWORD_NULL:
                            if (null != events)
                            {
                                events.RaiseNull();
                            }
                            break;
                        case JSONTokenType.OBJECT_START:
                            _Object(tokens, events);
                            break;
                        case JSONTokenType.ARRAY_START:
                            _Array(tokens, events);
                            break;
                        default:
                            throw new JSONValidationException("Expected value but got: " + next.Type);
                    }
                }
                else
                {
                    throw new JSONValidationException("Expected COLON but got: " + next.Type);
                }
            }
            else
            {
                throw new JSONValidationException("Expected STRING but got: " + next.Type);
            }
        }
Exemple #6
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);
        }
Exemple #7
0
 public JSONSelector(JSONWalkingEvents events, string[] paths)
     : this(events, paths, DefaultOptions)
 {
 }
Exemple #8
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;
        }
Exemple #9
0
        public static SQLConstraint GenerateSQLConstraint(IDatabaseService db, Dictionary<string, Type> typeMappings, string json)
        {
            var tokenizer = new JSONTokenizer(new StringReader(json));

            var events = new JSONWalkingEvents();

            var constraint = new JSONToSQLConstraint(db, events, typeMappings);

            JSONWalkingValidator walker = new JSONWalkingValidator();
            walker.Walk(tokenizer.GetEnumerator(), events);

            return constraint.GenerateSQLConstraint();
        }
Exemple #10
0
        public JSONToSQLConstraint(IDatabaseService db, JSONWalkingEvents events, Dictionary<string, Type> typeMappings)
        {
            if (null == db)
            {
                throw new ArgumentNullException("db");
            }

            if (null == events)
            {
                throw new ArgumentNullException("events");
            }

            if (null == typeMappings)
            {
                throw new ArgumentNullException("typeMappings");
            }

            _db = db;
            _events = events;
            _typeMappings = typeMappings;

            _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);

            _constraints = new Stack<Constraint>();
        }