Example #1
0
        /// <summary>
        /// If a Type Hint is present then this method attempts to
        /// use it and move any previously parsed data over.
        /// </summary>
        /// <param name="objectType">reference to the objectType</param>
        /// <param name="memberMap">reference to the memberMap</param>
        /// <param name="result">the previous result</param>
        /// <param name="typeInfo">the type info string to use</param>
        /// <returns></returns>
        private Object ProcessTypeHint(
            ref Type objectType,
            ref Dictionary <string, MemberInfo> memberMap,
            IDictionary result,
            string typeInfo)
        {
            if (String.IsNullOrEmpty(typeInfo))
            {
                return(result);
            }

            Type hintedType = Type.GetType(typeInfo, false);

            if (hintedType == null)
            {
                return(result);
            }
            objectType = hintedType;

            object newResult = this.InstantiateObject(hintedType, ref memberMap);

            if (memberMap != null)
            {
                Type       memberType;
                MemberInfo memberInfo;

                // copy any values into new object
                foreach (object key in result.Keys)
                {
                    JsonReader.GetMemberInfo(memberMap, key as String, out memberType, out memberInfo);
                    this.SetMemberValue(newResult, memberType, memberInfo, result[key]);
                }
            }

            return(newResult);
        }
Example #2
0
        private object ReadObject(Type objectType)
        {
            if (this.Source[this.index] != JsonReader.OperatorObjectStart)
            {
                throw new JsonDeserializationException(JsonReader.ErrorExpectedObject, this.index);
            }

            Dictionary <string, MemberInfo> memberMap = null;
            Object result;

            if (objectType != null)
            {
                result = this.InstantiateObject(objectType, ref memberMap);
            }
            else
            {
                result = new Dictionary <String, Object>();
            }

            JsonToken token;

            do
            {
                Type       memberType;
                MemberInfo memberInfo;

                // consume opening brace or delim
                this.index++;
                if (this.index >= this.SourceLength)
                {
                    throw new JsonDeserializationException(JsonReader.ErrorUnterminatedObject, this.index);
                }

                // get next token
                token = this.Tokenize();
                if (token == JsonToken.ObjectEnd)
                {
                    break;
                }

                if (token != JsonToken.String)
                {
                    throw new JsonDeserializationException(JsonReader.ErrorExpectedPropertyName, this.index);
                }

                // parse object member value
                string memberName = (String)this.ReadString(null);

                // determine the type of the property/field
                JsonReader.GetMemberInfo(memberMap, memberName, out memberType, out memberInfo);

                // get next token
                token = this.Tokenize();
                if (token != JsonToken.NameDelim)
                {
                    throw new JsonDeserializationException(JsonReader.ErrorExpectedPropertyNameDelim, this.index);
                }

                // consume delim
                index++;
                if (this.index >= this.SourceLength)
                {
                    throw new JsonDeserializationException(JsonReader.ErrorUnterminatedObject, this.index);
                }

                // parse object member value
                object value = this.Read(memberType, false);

                if (result is IDictionary)
                {
                    if (objectType == null &&
                        !String.IsNullOrEmpty(this.typeHintName) &&
                        this.typeHintName.Equals(memberName, StringComparison.InvariantCulture))
                    {
                        result = this.ProcessTypeHint(ref objectType, ref memberMap, (IDictionary)result, value as string);
                    }
                    else
                    {
                        ((IDictionary)result)[memberName] = value;
                    }
                }
                else if (objectType.GetInterface(JsonWriter.TypeGenericIDictionary) != null)
                {
                    throw new JsonDeserializationException(JsonReader.ErrorGenericIDictionary, this.index);
                }
                else
                {
                    this.SetMemberValue(result, memberType, memberInfo, value);
                }

                // get next token
                token = this.Tokenize();
            } while (token == JsonToken.ValueDelim);

            if (token != JsonToken.ObjectEnd)
            {
                throw new JsonDeserializationException(JsonReader.ErrorUnterminatedObject, this.index);
            }

            // consume closing brace
            this.index++;

            return(result);
        }