Example #1
0
        /// <summary>
        /// Converts the given name/value pair into a property element.
        /// And infers the type of property from the converted value.
        /// </summary>
        /// <param name="jsonProperty">the property value</param>
        /// <returns>the converted property</returns>
        private PropertyInstance ConvertProperty(JsonProperty jsonProperty)
        {
            if (jsonProperty.Value.JsonType == JsonValueType.JsonPrimitiveValue && ((JsonPrimitiveValue)jsonProperty.Value).Value == null)
            {
                return(new NullPropertyInstance()
                {
                    Name = jsonProperty.Name
                });
            }
            else
            {
                ODataPayloadElement elem = this.ConvertValue(jsonProperty.Value);
                ExceptionUtilities.CheckObjectNotNull(elem, "Converted property value was null");

                if (elem.ElementType == ODataPayloadElementType.PrimitiveValue)
                {
                    return(new PrimitiveProperty(jsonProperty.Name, (PrimitiveValue)elem));
                }
                else if (elem.ElementType == ODataPayloadElementType.ComplexInstance)
                {
                    return(new ComplexProperty(jsonProperty.Name, (ComplexInstance)elem));
                }
                else if (elem.ElementType == ODataPayloadElementType.EntityInstance)
                {
                    return(new NavigationPropertyInstance(jsonProperty.Name, new ExpandedLink(elem)));
                }
                else if (elem.ElementType == ODataPayloadElementType.DeferredLink)
                {
                    DeferredLink deferredLink = (DeferredLink)elem;
                    return(new NavigationPropertyInstance(jsonProperty.Name, deferredLink));
                }
                else if (elem.ElementType == ODataPayloadElementType.EntitySetInstance)
                {
                    return(new NavigationPropertyInstance(jsonProperty.Name, elem));
                }
                else if (elem.ElementType == ODataPayloadElementType.ComplexMultiValue)
                {
                    ComplexMultiValue complexMultiValue = (ComplexMultiValue)elem;
                    return(new ComplexMultiValueProperty(jsonProperty.Name, complexMultiValue));
                }
                else if (elem.ElementType == ODataPayloadElementType.PrimitiveMultiValue)
                {
                    PrimitiveMultiValue primitiveMultiValue = (PrimitiveMultiValue)elem;
                    return(new PrimitiveMultiValueProperty(jsonProperty.Name, primitiveMultiValue));
                }
                else if (elem.ElementType == ODataPayloadElementType.ComplexInstanceCollection)
                {
                    ComplexInstanceCollection complexCollection = (ComplexInstanceCollection)elem;
                    return(new ComplexMultiValueProperty(jsonProperty.Name, new ComplexMultiValue(null, false, complexCollection.ToArray())));
                }
                else if (elem.ElementType == ODataPayloadElementType.PrimitiveCollection)
                {
                    PrimitiveCollection primitiveCollection = (PrimitiveCollection)elem;
                    return(new PrimitiveMultiValueProperty(jsonProperty.Name, new PrimitiveMultiValue(null, false, primitiveCollection.ToArray())));
                }
                else if (elem.ElementType == ODataPayloadElementType.NamedStreamInstance)
                {
                    NamedStreamInstance nsi = (NamedStreamInstance)elem;
                    nsi.Name = jsonProperty.Name;
                    return(nsi);
                }
                else
                {
                    ExceptionUtilities.Assert(elem.ElementType == ODataPayloadElementType.EmptyUntypedCollection, "Do not know how to handle element of type" + elem.ElementType);
                    return(new EmptyCollectionProperty(jsonProperty.Name, (EmptyUntypedCollection)elem));
                }
            }
        }