Example #1
0
        /// <summary>
        /// Forks the reader and reads ahead to find the id and type of an object reference
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <returns></returns>
        public static ResourceObjectReference ReadAheadToIdentifyObject(ForkableJsonReader reader)
        {
            var    lookAheadReader = reader.Fork();
            string id   = null;
            string type = null;

            foreach (var propName in ReaderUtil.IterateProperties(lookAheadReader))
            {
                if (propName == PropertyNames.Id)
                {
                    if (lookAheadReader.TokenType != JsonToken.String)
                    {
                        throw new JsonApiFormatException(lookAheadReader.FullPath,
                                                         $"Expected to find string at {lookAheadReader.FullPath}",
                                                         "The value of 'id' MUST be a string");
                    }
                    id = (string)lookAheadReader.Value;
                }

                else if (propName == PropertyNames.Type)
                {
                    if (lookAheadReader.TokenType != JsonToken.String)
                    {
                        throw new JsonApiFormatException(lookAheadReader.FullPath,
                                                         $"Expected to find string at {lookAheadReader.FullPath}",
                                                         "The value of 'type' MUST be a string");
                    }
                    type = (string)lookAheadReader.Value;
                }

                //we have the data we need no point continuing to read the reader
                if (id != null && type != null)
                {
                    break;
                }
            }

            return(new ResourceObjectReference(id, type));
        }
Example #2
0
        /// <summary>
        /// Forks the reader and reads ahead to find the id and type of an object reference
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <returns></returns>
        public static ResourceObjectReference ReadAheadToIdentifyObject(ForkableJsonReader reader)
        {
            var lookAheadReader = reader.Fork();
            var reference       = new ResourceObjectReference();

            foreach (var propName in ReaderUtil.IterateProperties(lookAheadReader))
            {
                if (propName == PropertyNames.Id)
                {
                    if (lookAheadReader.TokenType != JsonToken.String)
                    {
                        throw new JsonApiFormatException(lookAheadReader.FullPath,
                                                         $"Expected to find string at {lookAheadReader.FullPath}",
                                                         "The value of 'id' MUST be a string");
                    }
                    reference.Id = (string)lookAheadReader.Value;
                }

                else if (propName == PropertyNames.Type)
                {
                    if (lookAheadReader.TokenType != JsonToken.String)
                    {
                        throw new JsonApiFormatException(lookAheadReader.FullPath,
                                                         $"Expected to find string at {lookAheadReader.FullPath}",
                                                         "The value of 'type' MUST be a string");
                    }
                    reference.Type = (string)lookAheadReader.Value;
                }


                if (reference.Id != null && reference.Type != null)
                {
                    break;
                }
            }

            return(reference);
        }