/// <summary>
        /// Parses a string like Bag(foo) to return foo
        /// </summary>
        /// <param name="typeName">name of type</param>
        /// <returns>Element of the Bag type</returns>
        internal static string ParseBagElementTypeName(string typeName)
        {
            string elementTypeName;

            if (ODataUtilities.TryGetMultiValueElementTypeName(typeName, out elementTypeName))
            {
                return(elementTypeName);
            }

            return(null);
        }
        /// <summary>
        /// Gets the expected options for the given content type and version
        /// </summary>
        /// <param name="contentType">The content type of the payload</param>
        /// <param name="version">The current version</param>
        /// <param name="payloadUri">The payload URI</param>
        /// <returns>The payload options for the given content type and version</returns>
        public ODataPayloadOptions GetExpectedPayloadOptions(string contentType, DataServiceProtocolVersion version, ODataUri payloadUri)
        {
            ExceptionUtilities.CheckArgumentNotNull(contentType, "contentType");
            ExceptionUtilities.Assert(version != DataServiceProtocolVersion.Unspecified, "Version cannot be unspecified");

            var expected = expectedPayloadOptions;

            if (contentType.StartsWith(MimeTypes.ApplicationJsonODataLightNonStreaming, System.StringComparison.Ordinal) ||
                contentType.StartsWith(MimeTypes.ApplicationJsonODataLightStreaming, System.StringComparison.Ordinal))
            {
                expected = ODataPayloadOptions.IncludeTypeNames
                           | ODataPayloadOptions.IncludeMediaResourceSourceLinks
                           | ODataPayloadOptions.IncludeMediaResourceEditLinks
                           | ODataPayloadOptions.IncludeNamedMediaResourceSourceLinks
                           | ODataPayloadOptions.IncludeNamedMediaResourceEditLinks
                           | ODataPayloadOptions.IncludeEntityIdentifier
                           | ODataPayloadOptions.ConventionallyProducedNamedStreamSelfLink;

                var selectedPropertyNames = ODataUtilities.GetSelectedPropertyNamesFromUri(payloadUri, this.UriConverter).ToList();
                if (selectedPropertyNames.Any())
                {
                    EntitySet  payloadEntitySet  = null;
                    EntityType payloadEntityType = null;
                    if (payloadUri.TryGetExpectedEntitySetAndType(out payloadEntitySet, out payloadEntityType) &&
                        !ODataUtilities.ContainsAllIdentityPropertyNames(selectedPropertyNames, payloadEntityType))
                    {
                        // JSON Light projections without identity do not contain enough metadata to deduce
                        // id and link values.
                        expected = ODataPayloadOptions.IncludeTypeNames;
                    }
                }
            }
            else if (version < DataServiceProtocolVersion.V4)
            {
                // Type names for null values are only supported in V1 and V2.
                expected = expected | ODataPayloadOptions.IncludeTypeNamesForNullValues;
            }

            return(expected);
        }
예제 #3
0
        private bool TryGetCollectionType(JsonObject jsonObject, out ODataPayloadElement elem)
        {
            string typeName = this.GetMetadataPropertyValue(jsonObject, TypeFieldName);

            elem = null;

            // if this object has a property with.Name "results" and it is of type "JsonArray"
            // Example:
            //    "results": [
            //      { ... },
            //      { ... }
            //     ]
            // if it is EntitySet or ComplexCollection
            // EntitySets have "_Count" and "_Next". So process it before retrning.
            if (jsonObject.Properties.Any(prop => prop.Name == ResultsFieldName) &&
                jsonObject.Properties.Where(prop => prop.Name == ResultsFieldName).Single().Value.JsonType == JsonValueType.JsonArray)
            {
                string nextLink   = this.GetNextLink(jsonObject);
                string countField = this.GetInlineCount(jsonObject);

                ODataPayloadElementCollection coll = this.ConvertArray((JsonArray)jsonObject.Properties.Where(p => p.Name == ResultsFieldName).Single().Value, true);

                if (coll.ElementType == ODataPayloadElementType.EntitySetInstance)
                {
                    EntitySetInstance entitySet = coll as EntitySetInstance;
                    entitySet.NextLink = nextLink;
                    if (countField == null)
                    {
                        entitySet.InlineCount = null;
                    }
                    else
                    {
                        entitySet.InlineCount = Convert.ToInt64(countField, CultureInfo.InvariantCulture);
                    }

                    elem = coll;
                }
                else if (coll.ElementType == ODataPayloadElementType.LinkCollection)
                {
                    LinkCollection linkCollection = (LinkCollection)coll;
                    linkCollection.InlineCount = Convert.ToInt64(countField, CultureInfo.InvariantCulture);
                    linkCollection.NextLink    = nextLink;
                    elem = coll;
                }
                else if (coll.ElementType == ODataPayloadElementType.PrimitiveCollection)
                {
                    if (typeName != null)
                    {
                        PrimitiveMultiValue primitiveMultiValue = new PrimitiveMultiValue(typeName, false, ((PrimitiveCollection)coll).ToArray());
                        elem = primitiveMultiValue;
                    }
                    else
                    {
                        elem = coll;
                    }
                }
                else if (coll.ElementType == ODataPayloadElementType.ComplexInstanceCollection)
                {
                    if (typeName != null)
                    {
                        ComplexMultiValue complexMultiValue = new ComplexMultiValue(typeName, false, ((ComplexInstanceCollection)coll).ToArray());
                        elem = complexMultiValue;
                    }
                    else
                    {
                        elem = coll;
                    }
                }
                else
                {
                    ExceptionUtilities.Assert(coll.ElementType == ODataPayloadElementType.EmptyUntypedCollection, "Collection type unhandled: {0}", coll.ElementType);
                    string elementTypeName;
                    if (ODataUtilities.TryGetMultiValueElementTypeName(typeName, out elementTypeName))
                    {
                        ExceptionUtilities.CheckObjectNotNull(this.PrimitiveDataTypeConverter, "Cannot infer clr type from edm type without converter");

                        if (this.PrimitiveDataTypeConverter.ToClrType(elementTypeName) != null)
                        {
                            elem = new PrimitiveMultiValue(typeName, false);
                        }
                        else
                        {
                            elem = new ComplexMultiValue(typeName, false);
                        }
                    }
                    else
                    {
                        EmptyUntypedCollection emptyCollection = (EmptyUntypedCollection)coll;
                        if (countField != null)
                        {
                            emptyCollection.InlineCount = long.Parse(countField, CultureInfo.InvariantCulture);
                        }

                        if (nextLink != null)
                        {
                            emptyCollection.NextLink = nextLink;
                        }

                        elem = emptyCollection;
                    }
                }

                return(true);
            }

            return(false);
        }