Exemplo n.º 1
0
        /// <summary>
        /// Reads the properties of an entity reference link.
        /// </summary>
        /// <param name="entityReferenceLinks">The <see cref="ODataEntityReferenceLinks"/> instance to set the read property values on.</param>
        /// <param name="propertiesFoundBitField">The bit field with all the properties already read.</param>
        /// <returns>true if the method found the 'results' property; otherwise false.</returns>
        private bool ReadEntityReferenceLinkProperties(
            ODataEntityReferenceLinks entityReferenceLinks,
            ref ODataVerboseJsonReaderUtils.EntityReferenceLinksWrapperPropertyBitMask propertiesFoundBitField)
        {
            Debug.Assert(entityReferenceLinks != null, "entityReferenceLinks != null");
            this.JsonReader.AssertNotBuffering();

            while (this.JsonReader.NodeType == JsonNodeType.Property)
            {
                string propertyName = this.JsonReader.ReadPropertyName();
                switch (propertyName)
                {
                case JsonConstants.ODataResultsName:
                    ODataVerboseJsonReaderUtils.VerifyEntityReferenceLinksWrapperPropertyNotFound(
                        ref propertiesFoundBitField,
                        ODataVerboseJsonReaderUtils.EntityReferenceLinksWrapperPropertyBitMask.Results,
                        JsonConstants.ODataResultsName);
                    this.JsonReader.AssertNotBuffering();
                    return(true);

                case JsonConstants.ODataCountName:
                    ODataVerboseJsonReaderUtils.VerifyEntityReferenceLinksWrapperPropertyNotFound(
                        ref propertiesFoundBitField,
                        ODataVerboseJsonReaderUtils.EntityReferenceLinksWrapperPropertyBitMask.Count,
                        JsonConstants.ODataCountName);
                    object countValue = this.JsonReader.ReadPrimitiveValue();
                    long?  count      = (long?)ODataVerboseJsonReaderUtils.ConvertValue(
                        countValue,
                        EdmCoreModel.Instance.GetInt64(true),
                        this.MessageReaderSettings,
                        this.Version,
                        /*validateNullValue*/ true,
                        propertyName);
                    ODataVerboseJsonReaderUtils.ValidateCountPropertyInEntityReferenceLinks(count);
                    entityReferenceLinks.Count = count;
                    break;

                case JsonConstants.ODataNextLinkName:
                    ODataVerboseJsonReaderUtils.VerifyEntityReferenceLinksWrapperPropertyNotFound(
                        ref propertiesFoundBitField,
                        ODataVerboseJsonReaderUtils.EntityReferenceLinksWrapperPropertyBitMask.NextPageLink,
                        JsonConstants.ODataNextLinkName);
                    string nextLinkString = this.JsonReader.ReadStringValue(JsonConstants.ODataNextLinkName);
                    ODataVerboseJsonReaderUtils.ValidateEntityReferenceLinksStringProperty(nextLinkString, JsonConstants.ODataNextLinkName);
                    entityReferenceLinks.NextPageLink = this.ProcessUriFromPayload(nextLinkString);
                    break;

                default:
                    // Skip all unrecognized properties
                    this.JsonReader.SkipValue();
                    break;
                }
            }

            this.JsonReader.AssertNotBuffering();
            return(false);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Read a set of top-level entity reference links.
        /// </summary>
        /// <returns>An <see cref="ODataEntityReferenceLinks"/> representing the read links.</returns>
        internal ODataEntityReferenceLinks ReadEntityReferenceLinks()
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(this.JsonReader.NodeType == JsonNodeType.None, "Pre-Condition: expected JsonNodeType.None, the reader must not have been used yet.");
            this.JsonReader.AssertNotBuffering();

            // Set to true if the entity reference links are expected to have the 'results' wrapper.
            // Entity reference links are only expected to have a results wrapper if
            // (a) the protocol version is >= 2 AND
            // (b) we are reading a response
            // NOTE: OIPI does not specify a format for >= v2 entity reference links in requests; we thus use the v1 format and consequently do not expect a result wrapper.
            bool isResultsWrapperExpected = this.Version >= ODataVersion.V2 && this.ReadingResponse;

            ODataVerboseJsonReaderUtils.EntityReferenceLinksWrapperPropertyBitMask propertiesFoundBitField =
                ODataVerboseJsonReaderUtils.EntityReferenceLinksWrapperPropertyBitMask.None;
            ODataEntityReferenceLinks entityReferenceLinks = new ODataEntityReferenceLinks();

            // Read the response wrapper "d" if expected.
            this.ReadPayloadStart(false /*isReadingNestedPayload*/);

            if (isResultsWrapperExpected)
            {
                // Read the start object of the results wrapper object
                this.JsonReader.ReadStartObject();

                bool foundResultsProperty = this.ReadEntityReferenceLinkProperties(entityReferenceLinks, ref propertiesFoundBitField);

                if (!foundResultsProperty)
                {
                    throw new ODataException(Strings.ODataJsonEntityReferenceLinkDeserializer_ExpectedEntityReferenceLinksResultsPropertyNotFound);
                }
            }

            // Read the start of the content array of the links
            this.JsonReader.ReadStartArray();

            List <ODataEntityReferenceLink> links = new List <ODataEntityReferenceLink>();

            while (this.JsonReader.NodeType != JsonNodeType.EndArray)
            {
                // read another link
                ODataEntityReferenceLink entityReferenceLink = this.ReadSingleEntityReferenceLink();
                links.Add(entityReferenceLink);
            }

            // Read over the end object - note that this might be the last node in the input (in case there's no response wrapper)
            this.JsonReader.ReadEndArray();

            if (isResultsWrapperExpected)
            {
                this.ReadEntityReferenceLinkProperties(entityReferenceLinks, ref propertiesFoundBitField);

                // Read the end object of the results wrapper object
                this.JsonReader.ReadEndObject();
            }

            entityReferenceLinks.Links = new ReadOnlyEnumerable <ODataEntityReferenceLink>(links);

            // Read the end of the response wrapper "d" if expected.
            this.ReadPayloadEnd(false /*isReadingNestedPayload*/);

            Debug.Assert(this.JsonReader.NodeType == JsonNodeType.EndOfInput, "Post-Condition: expected JsonNodeType.EndOfInput");
            this.JsonReader.AssertNotBuffering();

            return(entityReferenceLinks);
        }