/// <summary> /// Read JSON Array value. /// </summary> /// <param name="jsonReader">The <see cref="IJsonReader"/> to read from.</param> /// <returns>The <see cref="JsonArrayValue"/> generated.</returns> public static JsonArrayValue ReadAsArray(this IJsonReader jsonReader) { EdmUtil.CheckArgumentNull(jsonReader, "jsonReader"); // Supports to read from Begin if (jsonReader.NodeKind == JsonNodeKind.None) { jsonReader.Read(); } // Make sure the input is an Array jsonReader.ValidateNodeKind(JsonNodeKind.StartArray); JsonArrayValue arrayValue = new JsonArrayValue(); // Consume the "[" tag. jsonReader.Read(); while (jsonReader.NodeKind != JsonNodeKind.EndArray) { arrayValue.Add(jsonReader.ReadAsJsonValue()); } // Consume the "]" tag. jsonReader.Read(); return(arrayValue); }
/// <summary> /// Read JSON Object value. /// </summary> /// <param name="jsonReader">The <see cref="IJsonReader"/> to read from.</param> /// <returns>The <see cref="JsonObjectValue"/> generated.</returns> public static JsonObjectValue ReadAsObject(this IJsonReader jsonReader) { EdmUtil.CheckArgumentNull(jsonReader, "jsonReader"); // Supports to read from Begin if (jsonReader.NodeKind == JsonNodeKind.None) { jsonReader.Read(); } // Make sure the input is an object jsonReader.ValidateNodeKind(JsonNodeKind.StartObject); JsonObjectValue objectValue = new JsonObjectValue(); // Consume the "{" tag. jsonReader.Read(); while (jsonReader.NodeKind != JsonNodeKind.EndObject) { // Get the property name and move json reader to next token string propertyName = jsonReader.ReadPropertyName(); // Shall we throw the exception or just ignore it and make the last win? if (objectValue.ContainsKey(propertyName)) { throw new Exception(/*Strings.JsonReader_DuplicatedProperty(propertyName)*/); } // save as propertyName/PropertyValue pair objectValue[propertyName] = jsonReader.ReadAsJsonValue(); } // Consume the "}" tag. jsonReader.Read(); return(objectValue); }