/// <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);
        }
예제 #2
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);
        }