Ejemplo n.º 1
0
        /// <summary>
        /// Parses a property starting with the colon after the property name.
        /// </summary>
        /// <param name="propertyName">The name of the property.</param>
        /// <param name="propertyNameTextAnnotation">The name text annotation for the property.</param>
        /// <returns>parsed object</returns>
        private JsonProperty ParsePropertyWithName(string propertyName, JsonPropertyNameTextAnnotation propertyNameTextAnnotation)
        {
            // Each name is followed by : (colon)
            ExceptionUtilities.Assert(this.tokenizer.TokenType == JsonTokenType.Colon, "Invalid Token");

            var nameValueSeparatorTextAnnotation = new JsonPropertyNameValueSeparatorTextAnnotation()
            {
                Text = this.tokenizer.TokenText
            };

            this.tokenizer.GetNextToken();

            // Colon is followed by a value
            ExceptionUtilities.Assert(this.IsValueType(this.tokenizer.TokenType), "Invalid Token");

            JsonValue value    = this.ParseValue();
            var       property = new JsonProperty(propertyName, value);

            property.SetAnnotation(propertyNameTextAnnotation);
            property.SetAnnotation(nameValueSeparatorTextAnnotation);

            return(property);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Parses Objects starting with left curly braces
        /// </summary>
        /// <returns>parsed object</returns>
        private JsonObject ParseObject()
        {
            // An object begins with '{' (left brace)
            ExceptionUtilities.Assert(this.tokenizer.TokenType == JsonTokenType.LeftCurly, "Invalid Token");

            // The stream cannot terminate without '}' (right brace)
            ExceptionUtilities.Assert(this.tokenizer.HasMoreTokens(), "Invalid End Of Stream");

            var startObjectTextAnnotation = new JsonStartObjectTextAnnotation()
            {
                Text = this.tokenizer.TokenText
            };

            this.tokenizer.GetNextToken();

            // An object is an unordered set of name/value pairs. So next token cannot be anything other than name or '}'
            ExceptionUtilities.Assert(this.tokenizer.TokenType == JsonTokenType.RightCurly || this.tokenizer.TokenType == JsonTokenType.String, "Invalid Token");

            var result = new JsonObject();

            result.SetAnnotation(startObjectTextAnnotation);
            JsonPropertySeparatorTextAnnotation propertySeparatorTextAnnotation = null;

            while (this.tokenizer.HasMoreTokens())
            {
                ExceptionUtilities.Assert(this.tokenizer.TokenType == JsonTokenType.String || this.tokenizer.TokenType == JsonTokenType.Comma || this.tokenizer.TokenType == JsonTokenType.RightCurly, "Invalid Token");

                // End of Object
                if (this.tokenizer.TokenType == JsonTokenType.RightCurly)
                {
                    break;
                }
                else if (this.tokenizer.TokenType == JsonTokenType.String)
                {
                    JsonPropertyNameTextAnnotation nameTextAnnotation;
                    string name = this.ParseName(out nameTextAnnotation);

                    JsonProperty property = this.ParsePropertyWithName(name, nameTextAnnotation);
                    if (propertySeparatorTextAnnotation != null)
                    {
                        property.SetAnnotation(propertySeparatorTextAnnotation);
                    }

                    propertySeparatorTextAnnotation = null;
                    result.Add(property);

                    // name/value pairs are separated by , (comma) or value can be followed by '}' (right brace)
                    ExceptionUtilities.Assert(this.tokenizer.TokenType == JsonTokenType.Comma || this.tokenizer.TokenType == JsonTokenType.RightCurly, "Invalid Token");
                }
                else if (this.tokenizer.TokenType == JsonTokenType.Comma)
                {
                    propertySeparatorTextAnnotation = new JsonPropertySeparatorTextAnnotation()
                    {
                        Text = this.tokenizer.TokenText
                    };
                    this.tokenizer.GetNextToken();

                    // Last property in the object should not be followed by a comma.
                    // Hence asserting that next token is yet another name.
                    ExceptionUtilities.Assert(this.tokenizer.TokenType == JsonTokenType.String, "Invalid Token");
                }
            }

            if (this.tokenizer.TokenType == JsonTokenType.RightCurly)
            {
                result.SetAnnotation(new JsonEndObjectTextAnnotation()
                {
                    Text = this.tokenizer.TokenText
                });

                // Parser should always be promoted to next token when leaving the loop
                if (this.tokenizer.HasMoreTokens())
                {
                    this.tokenizer.GetNextToken();
                }
            }

            return(result);
        }
        /// <summary>
        /// Parses a property starting with the colon after the property name.
        /// </summary>
        /// <param name="propertyName">The name of the property.</param>
        /// <param name="propertyNameTextAnnotation">The name text annotation for the property.</param>
        /// <returns>parsed object</returns>
        private JsonProperty ParsePropertyWithName(string propertyName, JsonPropertyNameTextAnnotation propertyNameTextAnnotation)
        {
            // Each name is followed by : (colon)
            ExceptionUtilities.Assert(this.tokenizer.TokenType == JsonTokenType.Colon, "Invalid Token");

            var nameValueSeparatorTextAnnotation = new JsonPropertyNameValueSeparatorTextAnnotation() { Text = this.tokenizer.TokenText };
            this.tokenizer.GetNextToken();

            // Colon is followed by a value
            ExceptionUtilities.Assert(this.IsValueType(this.tokenizer.TokenType), "Invalid Token");

            JsonValue value = this.ParseValue();
            var property = new JsonProperty(propertyName, value);
            property.SetAnnotation(propertyNameTextAnnotation);
            property.SetAnnotation(nameValueSeparatorTextAnnotation);

            return property;
        }