/// <summary> /// Reads the json object value from the jsonReader /// </summary> /// <param name="jsonReader">Json reader to read payload from the wire.</param> /// <param name="insideJsonObjectValue">true if the reader is positioned on the first property of the value which is a JSON Object /// (or the second property if the first one was odata.type).</param> /// <param name="inputContext">The input context with all the settings.</param> /// <param name="recursionDepth">The recursion depth to start with.</param> /// <returns>an instance of IDictionary containing the spatial value.</returns> private static IDictionary <string, object> ReadObjectValue(IJsonReader jsonReader, bool insideJsonObjectValue, ODataInputContext inputContext, int recursionDepth) { Debug.Assert(jsonReader != null, "jsonReader != null"); Debug.Assert(insideJsonObjectValue || jsonReader.NodeType == JsonNodeType.StartObject, "insideJsonObjectValue || jsonReader.NodeType == JsonNodeType.StartObject"); Debug.Assert( !insideJsonObjectValue || jsonReader.NodeType == JsonNodeType.Property || jsonReader.NodeType == JsonNodeType.EndObject, "!insideJsonObjectValue || jsonReader.NodeType == JsonNodeType.Property || jsonReader.NodeType == JsonNodeType.EndObject"); Debug.Assert(inputContext != null, "inputContext != null"); ValidationUtils.IncreaseAndValidateRecursionDepth(ref recursionDepth, inputContext.MessageReaderSettings.MessageQuotas.MaxNestingDepth); IDictionary <string, object> jsonValue = new Dictionary <string, object>(StringComparer.Ordinal); if (!insideJsonObjectValue) { // Note that if the insideJsonObjectValue is true we will ignore the odata.type instance annotation // which might have been there. This is OK since for spatial we only need the normal properties anyway. jsonReader.ReadNext(); } while (jsonReader.NodeType != JsonNodeType.EndObject) { // read the property name string propertyName = jsonReader.ReadPropertyName(); // read the property value object propertyValue = null; switch (jsonReader.NodeType) { case JsonNodeType.PrimitiveValue: propertyValue = jsonReader.ReadPrimitiveValue(); break; case JsonNodeType.StartArray: propertyValue = ReadArrayValue(jsonReader, inputContext, recursionDepth); break; case JsonNodeType.StartObject: propertyValue = ReadObjectValue(jsonReader, /*insideJsonObjectValue*/ false, inputContext, recursionDepth); break; default: Debug.Assert(false, "We should never reach here - There should be matching end element"); return(null); } jsonValue.Add(ODataAnnotationNames.RemoveAnnotationPrefix(propertyName), propertyValue); } jsonReader.ReadEndObject(); return(jsonValue); }
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()); } }