コード例 #1
0
        /// <summary>
        /// Sets json object annotation text with whitespaces.
        /// </summary>
        /// <param name="transformedPayload">Transformed payload with json annotations.</param>
        private void SetWhiteSpaceJsonObjectAnnotations(JsonValue transformedPayload)
        {
            JsonEndObjectTextAnnotation   jsonEndObjectTextAnnotation   = new JsonEndObjectTextAnnotation();
            JsonStartObjectTextAnnotation jsonStartObjectTextAnnotation = new JsonStartObjectTextAnnotation();

            jsonEndObjectTextAnnotation.Text   = "\t\n } \t\n";
            jsonStartObjectTextAnnotation.Text = "\t\n { \t\n";

            transformedPayload.SetAnnotation <JsonEndObjectTextAnnotation>(jsonEndObjectTextAnnotation);
            transformedPayload.SetAnnotation <JsonStartObjectTextAnnotation>(jsonStartObjectTextAnnotation);
        }
コード例 #2
0
        /// <summary>
        /// Writes a text representation of the specified <paramref name="objectValue"/> into a text writer.
        /// </summary>
        /// <param name="objectValue">The JSON value to write.</param>
        private void WriteObject(JsonObject objectValue)
        {
            var startObjectTextAnnotation = objectValue.GetAnnotation <JsonStartObjectTextAnnotation>()
                                            ?? JsonStartObjectTextAnnotation.GetDefault(this.writer);
            var endObjectTextAnnotation = objectValue.GetAnnotation <JsonEndObjectTextAnnotation>()
                                          ?? JsonEndObjectTextAnnotation.GetDefault(this.writer);

            this.writer.Write(startObjectTextAnnotation.Text);

            bool first = true;

            foreach (JsonProperty propertyValue in objectValue.Properties)
            {
                var propertySeparatorTextAnnotation = propertyValue.GetAnnotation <JsonPropertySeparatorTextAnnotation>()
                                                      ?? JsonPropertySeparatorTextAnnotation.GetDefault(first);
                first = false;

                this.writer.Write(propertySeparatorTextAnnotation.Text);
                this.WriteValue(propertyValue);
            }

            this.writer.Write(endObjectTextAnnotation.Text);
        }
コード例 #3
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);
        }