/// <summary> /// Parse a JSON response into a sequence of elements and also return /// the count of objects. This method abstracts out the differences /// between a raw array response and an inline count response. /// </summary> /// <param name="response">The JSON response.</param> /// <param name="totalCount"> /// The total count as requested via the IncludeTotalCount method. /// </param> /// <returns>The response as a JSON array.</returns> internal static JsonArray GetResponseSequence(IJsonValue response, out long totalCount) { double?inlineCount = null; // Try and get the values as an array JsonArray values = response.AsArray(); if (values == null) { // Otherwise try and get the values from the results property // (which is the case when we retrieve the count inline) values = response.Get(InlineCountResultsKey).AsArray(); inlineCount = response.Get(InlineCountCountKey).AsNumber(); if (values == null) { throw new InvalidOperationException( string.Format( CultureInfo.InvariantCulture, Resources.MobileServiceTables_GetResponseSequence_ExpectedArray, (response ?? JsonExtensions.Null()).Stringify())); } } // Get the count via the inline count or default an unspecified // count to -1 totalCount = inlineCount != null ? (long)inlineCount.Value : -1L; return(values); }
/// <summary> /// Get an element from a table by its ID. /// </summary> /// <param name="id">The ID of the element.</param> /// <param name="parameters"> /// A dictionary of user-defined parameters and values to include in the request URI query string. /// </param> /// <returns>The desired element as JSON object.</returns> private async Task <JsonObject> GetSingleValueAsync(object id, IDictionary <string, string> parameters) { // Create a query for just this item string query = string.Format( CultureInfo.InvariantCulture, "$filter={0} eq {1}", MobileServiceTableUrlBuilder.IdPropertyName, TypeExtensions.ToODataConstant(id)); // Send the query IJsonValue response = await this.ReadAsync(query, parameters); // Get the first element in the response JsonObject obj = response.AsObject(); if (obj == null) { JsonArray array = response.AsArray(); if (array != null && array.Count > 0) { obj = array.FirstOrDefault().AsObject(); } } if (obj == null) { throw new InvalidOperationException( string.Format( CultureInfo.InvariantCulture, Resources.MobileServiceTables_GetSingleValueAsync_NotSingleObject, (response ?? JsonExtensions.Null()).Stringify())); } return(obj); }
public void AsArray() { IJsonValue value = null; Assert.IsNull(value.AsArray()); Assert.IsNull(JsonExtensions.Null().AsArray()); Assert.IsNull(new JsonObject().AsArray()); Assert.IsNull(JsonValue.CreateBooleanValue(true).AsArray()); Assert.IsNull(JsonValue.CreateNumberValue(2.0).AsArray()); Assert.IsNull(JsonValue.CreateStringValue("2.0").AsArray()); Assert.IsNotNull(new JsonArray().AsArray()); }