internal static ODataValue ReadODataValue(this IJsonReader jsonReader) { if (jsonReader.NodeType == JsonNodeType.PrimitiveValue) { object primitiveValue = jsonReader.ReadPrimitiveValue(); return(primitiveValue.ToODataValue()); } else if (jsonReader.NodeType == JsonNodeType.StartObject) { jsonReader.ReadStartObject(); ODataResourceValue resourceValue = new ODataResourceValue(); IList <ODataProperty> propertyList = new List <ODataProperty>(); while (jsonReader.NodeType != JsonNodeType.EndObject) { ODataProperty property = new ODataProperty(); property.Name = jsonReader.ReadPropertyName(); property.Value = jsonReader.ReadODataValue(); propertyList.Add(property); } resourceValue.Properties = propertyList; jsonReader.ReadEndObject(); return(resourceValue); } else if (jsonReader.NodeType == JsonNodeType.StartArray) { jsonReader.ReadStartArray(); ODataCollectionValue collectionValue = new ODataCollectionValue(); IList <object> propertyList = new List <object>(); while (jsonReader.NodeType != JsonNodeType.EndArray) { propertyList.Add(jsonReader.ReadODataValue()); } collectionValue.Items = propertyList; jsonReader.ReadEndArray(); return(collectionValue); } else { return(jsonReader.ReadAsUntypedOrNullValue()); } }
/// <summary> /// Read the json array from the reader. /// </summary> /// <param name="jsonReader">JsonReader instance.</param> /// <param name="inputContext">The input context with all the settings.</param> /// <param name="recursionDepth">The recursion depth to start with.</param> /// <returns>a list of json objects.</returns> private static IEnumerable <object> ReadArrayValue(IJsonReader jsonReader, ODataInputContext inputContext, int recursionDepth) { Debug.Assert(jsonReader != null, "jsonReader != null"); Debug.Assert(jsonReader.NodeType == JsonNodeType.StartArray, "jsonReader.NodeType == JsonNodeType.StartArray"); Debug.Assert(inputContext != null, "inputContext != null"); ValidationUtils.IncreaseAndValidateRecursionDepth(ref recursionDepth, inputContext.MessageReaderSettings.MessageQuotas.MaxNestingDepth); List <object> array = new List <object>(); jsonReader.ReadNext(); while (jsonReader.NodeType != JsonNodeType.EndArray) { switch (jsonReader.NodeType) { case JsonNodeType.PrimitiveValue: array.Add(jsonReader.ReadPrimitiveValue()); break; case JsonNodeType.StartObject: array.Add(ReadObjectValue(jsonReader, /*insideJsonObjectValue*/ false, inputContext, recursionDepth)); break; case JsonNodeType.StartArray: array.Add(ReadArrayValue(jsonReader, inputContext, recursionDepth)); break; default: Debug.Assert(false, "We should never have got here - the valid states in array are primitive value or object"); return(null); } } jsonReader.ReadEndArray(); return(array); }