Example #1
0
        /// <summary>
        /// Reads the next node from the <paramref name="jsonReader"/>, verifies that it is a Property node and returns the property name.
        /// </summary>
        /// <param name="jsonReader">The <see cref="JsonReader"/> to read from.</param>
        /// <returns>The property name of the property node read.</returns>
        internal static string ReadPropertyName(this IJsonReader jsonReader)
        {
            Debug.Assert(jsonReader != null, "jsonReader != null");

            jsonReader.ValidateNodeType(JsonNodeType.Property);
            string propertyName = jsonReader.GetPropertyName();

            jsonReader.ReadNext();
            return(propertyName);
        }
Example #2
0
        /// <summary>
        /// Skips over a JSON value (primitive, object or array), and append raw string to StringBuilder.
        /// </summary>
        /// <param name="jsonReader">The <see cref="JsonReader"/> to read from.</param>
        /// <param name="jsonRawValueStringBuilder">The StringBuilder to receive JSON raw string.</param>
        internal static void SkipValue(this IJsonReader jsonReader, StringBuilder jsonRawValueStringBuilder)
        {
            Debug.Assert(jsonReader != null, "jsonReader != null");
            using (StringWriter stringWriter = new StringWriter(jsonRawValueStringBuilder, CultureInfo.InvariantCulture))
            {
                JsonWriter jsonWriter = new JsonWriter(stringWriter, isIeee754Compatible: false);
                int        depth      = 0;
                do
                {
                    switch (jsonReader.NodeType)
                    {
                    case JsonNodeType.PrimitiveValue:
                        if (jsonReader.Value == null)
                        {
                            jsonWriter.WriteValue((string)null);
                        }
                        else
                        {
                            jsonWriter.WritePrimitiveValue(jsonReader.Value);
                        }

                        break;

                    case JsonNodeType.StartArray:
                        jsonWriter.StartArrayScope();
                        depth++;
                        break;

                    case JsonNodeType.StartObject:
                        jsonWriter.StartObjectScope();
                        depth++;
                        break;

                    case JsonNodeType.EndArray:
                        jsonWriter.EndArrayScope();
                        Debug.Assert(depth > 0, "Seen too many scope ends.");
                        depth--;
                        break;

                    case JsonNodeType.EndObject:
                        jsonWriter.EndObjectScope();
                        Debug.Assert(depth > 0, "Seen too many scope ends.");
                        depth--;
                        break;

                    case JsonNodeType.Property:
                        jsonWriter.WriteName(jsonReader.GetPropertyName());
                        break;

                    default:
                        Debug.Assert(
                            jsonReader.NodeType != JsonNodeType.EndOfInput,
                            "We should not have reached end of input, since the scopes should be well formed. Otherwise JsonReader should have failed by now.");
                        break;
                    }
                }while (jsonReader.Read() && depth > 0);

                if (depth > 0)
                {
                    // Not all open scopes were closed:
                    // "Invalid JSON. Unexpected end of input was found in JSON content. Not all object and array scopes were closed."
                    throw JsonReaderExtensions.CreateException(Strings.JsonReader_EndOfInputWithOpenScope);
                }

                jsonWriter.Flush();
            }
        }