예제 #1
0
        /// <summary>
        /// Parses Json value or property.
        /// </summary>
        /// <returns>Parsed value.</returns>
        public JsonValue ParseValueOrProperty()
        {
            JsonValue          value       = this.ParseValue();
            JsonPrimitiveValue stringValue = value as JsonPrimitiveValue;

            if (stringValue != null && stringValue.Value != null && stringValue.Value is string)
            {
                if (this.tokenizer.HasMoreTokens())
                {
                    JsonPrimitiveValueTextAnnotation textAnnotation             = stringValue.GetAnnotation <JsonPrimitiveValueTextAnnotation>();
                    JsonPropertyNameTextAnnotation   propertyNameTextAnnotation = null;
                    if (textAnnotation != null)
                    {
                        propertyNameTextAnnotation = new JsonPropertyNameTextAnnotation()
                        {
                            Text = textAnnotation.Text
                        };
                    }

                    return(this.ParsePropertyWithName((string)stringValue.Value, propertyNameTextAnnotation));
                }
                else
                {
                    return(stringValue);
                }
            }
            else
            {
                return(value);
            }
        }
예제 #2
0
        /// <summary>
        /// Parses the property name as string
        /// </summary>
        /// <returns>Parsed name.</returns>
        private string ParseName(out JsonPropertyNameTextAnnotation nameTextAnnotation)
        {
            string result = (string)this.tokenizer.Value;

            nameTextAnnotation = new JsonPropertyNameTextAnnotation()
            {
                Text = this.tokenizer.TokenText
            };
            this.tokenizer.GetNextToken();
            return(result);
        }
예제 #3
0
        /// <summary>
        /// Writes a text representation of the specified <paramref name="propertyValue"/> into a text writer.
        /// </summary>
        /// <param name="propertyValue">The JSON value to write.</param>
        private void WriteProperty(JsonProperty propertyValue)
        {
            var nameTextAnnotation = propertyValue.GetAnnotation <JsonPropertyNameTextAnnotation>()
                                     ?? JsonPropertyNameTextAnnotation.GetDefault(propertyValue.Name);
            var nameValueSeparatorTextAnnotation = propertyValue.GetAnnotation <JsonPropertyNameValueSeparatorTextAnnotation>()
                                                   ?? JsonPropertyNameValueSeparatorTextAnnotation.GetDefault();

            this.writer.Write(nameTextAnnotation.Text);
            this.writer.Write(nameValueSeparatorTextAnnotation.Text);
            this.WriteValue(propertyValue.Value);
        }
예제 #4
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);
        }