GetMemberInfo() static private method

static private GetMemberInfo ( MemberInfo>.Dictionary memberMap, string memberName, MemberInfo &memberInfo ) : Type
memberMap MemberInfo>.Dictionary
memberName string
memberInfo System.Reflection.MemberInfo
return System.Type
        protected virtual object CoerceType(Type targetType, IDictionary value, out Dictionary <string, MemberInfo> memberMap)
        {
            object result = this.InstantiateObject(targetType, out memberMap);

            if (memberMap != null)
            {
                foreach (object obj in value.Keys)
                {
                    MemberInfo memberInfo2;
                    Type       memberInfo = TypeCoercionUtility.GetMemberInfo(memberMap, obj as string, out memberInfo2);
                    this.SetMemberValue(result, memberInfo, memberInfo2, value[obj]);
                }
            }
            return(result);
        }
Ejemplo n.º 2
0
        private object CoerceType(Type targetType, IDictionary value, out Dictionary <string, MemberInfo> memberMap)
        {
            object newValue = this.InstantiateObject(targetType, out memberMap);

            if (memberMap != null)
            {
                // copy any values into new object
                foreach (object key in value.Keys)
                {
                    MemberInfo memberInfo;
                    Type       memberType = TypeCoercionUtility.GetMemberInfo(memberMap, key as String, out memberInfo);
                    this.SetMemberValue(newValue, memberType, memberInfo, value[key]);
                }
            }
            return(newValue);
        }
Ejemplo n.º 3
0
        internal object CoerceType(Type targetType, IDictionary newValues, object existingObject, out Dictionary <string, MemberInfo> memberMap)
        {
            // don't incurr the cost of member map for dictionaries
            if (typeof(IDictionary).IsAssignableFrom(targetType) && existingObject is IDictionary)
            {
                memberMap = null;
            }
            else
            {
                memberMap = this.CreateMemberMap(targetType);
            }

            if (memberMap != null)
            {
                // copy any values into new object
                foreach (object key in newValues.Keys)
                {
                    MemberInfo memberInfo;
                    Type       memberType = TypeCoercionUtility.GetMemberInfo(memberMap, key as String, out memberInfo);
                    this.SetMemberValue(existingObject, memberType, memberInfo, newValues[key]);
                }
            }
            else
            {
                // set the values
                IDictionary newValueDictionary = existingObject as IDictionary;

                if (newValueDictionary == null)
                {
                    return(existingObject);
                }

                foreach (object key in newValues.Keys)
                {
                    newValueDictionary[key as string] = newValues[key];
                }
            }
            return(existingObject);
        }
Ejemplo n.º 4
0
        protected virtual object ReadObject(Type objectType)
        {
            if (Source[index] != '{')
            {
                throw new JsonDeserializationException("Expected JSON object.", index);
            }
            Type type = null;
            Dictionary <string, MemberInfo> memberMap = null;
            object obj;

            if (objectType != null)
            {
                obj = Settings.Coercion.InstantiateObject(objectType, out memberMap);
                if (memberMap == null)
                {
                    Type @interface = objectType.GetInterface("System.Collections.Generic.IDictionary`2");
                    if (@interface != null)
                    {
                        Type[] genericArguments = @interface.GetGenericArguments();
                        if (genericArguments.Length == 2)
                        {
                            if (genericArguments[0] != typeof(string))
                            {
                                throw new JsonDeserializationException($"Types which implement Generic IDictionary<TKey, TValue> need to have string keys to be deserialized. ({objectType})", index);
                            }
                            if (genericArguments[1] != typeof(object))
                            {
                                type = genericArguments[1];
                            }
                        }
                    }
                }
            }
            else
            {
                obj = new Dictionary <string, object>();
            }
            JsonToken jsonToken;

            do
            {
                index++;
                if (index >= SourceLength)
                {
                    throw new JsonDeserializationException("Unterminated JSON object.", index);
                }
                jsonToken = Tokenize(Settings.AllowUnquotedObjectKeys);
                switch (jsonToken)
                {
                default:
                    throw new JsonDeserializationException("Expected JSON object property name.", index);

                case JsonToken.String:
                case JsonToken.UnquotedName:
                {
                    string     text = ((jsonToken != JsonToken.String) ? ReadUnquotedKey() : ((string)ReadString(null)));
                    Type       type2;
                    MemberInfo memberInfo;
                    if (type == null && memberMap != null)
                    {
                        type2 = TypeCoercionUtility.GetMemberInfo(memberMap, text, out memberInfo);
                    }
                    else
                    {
                        type2      = type;
                        memberInfo = null;
                    }
                    jsonToken = Tokenize();
                    if (jsonToken != JsonToken.NameDelim)
                    {
                        throw new JsonDeserializationException("Expected JSON object property name delimiter.", index);
                    }
                    index++;
                    if (index >= SourceLength)
                    {
                        throw new JsonDeserializationException("Unterminated JSON object.", index);
                    }
                    object obj2 = Read(type2, typeIsHint: false);
                    if (obj is IDictionary)
                    {
                        if (objectType == null && Settings.IsTypeHintName(text))
                        {
                            obj = Settings.Coercion.ProcessTypeHint((IDictionary)obj, obj2 as string, out objectType, out memberMap);
                        }
                        else
                        {
                            ((IDictionary)obj)[text] = obj2;
                        }
                    }
                    else
                    {
                        if (objectType.GetInterface("System.Collections.Generic.IDictionary`2") != null)
                        {
                            throw new JsonDeserializationException($"Types which implement Generic IDictionary<TKey, TValue> also need to implement IDictionary to be deserialized. ({objectType})", index);
                        }
                        Settings.Coercion.SetMemberValue(obj, type2, memberInfo, obj2);
                    }
                    goto IL_0270;
                }

                case JsonToken.ObjectEnd:
                    break;
                }
                break;
IL_0270:
                jsonToken = Tokenize();
            }while (jsonToken == JsonToken.ValueDelim);
            if (jsonToken != JsonToken.ObjectEnd)
            {
                throw new JsonDeserializationException("Unterminated JSON object.", index);
            }
            index++;
            return(obj);
        }
Ejemplo n.º 5
0
        private object ReadObject(Type objectType)
        {
            if (this.Source[this.index] != JsonReader.OperatorObjectStart)
            {
                throw new JsonDeserializationException(JsonReader.ErrorExpectedObject, this.index);
            }

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

            if (objectType != null)
            {
                result = this.Settings.Coercion.InstantiateObject(objectType, out memberMap);

                if (memberMap == null)
                {
                    // this allows specific IDictionary<string, T> to deserialize T
                    Type genericDictionary = objectType.GetInterface(JsonReader.TypeGenericIDictionary);
                    if (genericDictionary != null)
                    {
                        Type[] genericArgs = genericDictionary.GetGenericArguments();
                        if (genericArgs.Length == 2)
                        {
                            if (genericArgs[0] != typeof(String))
                            {
                                throw new JsonDeserializationException(
                                          String.Format(JsonReader.ErrorGenericIDictionaryKeys, objectType),
                                          this.index);
                            }

                            if (genericArgs[1] != typeof(Object))
                            {
                                genericDictionaryType = genericArgs[1];
                            }
                        }
                    }
                }
            }
            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(this.Settings.AllowUnquotedObjectKeys);
                if (token == JsonToken.ObjectEnd)
                {
                    break;
                }

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

                // parse object member value
                string memberName = (token == JsonToken.String) ?
                                    (String)this.ReadString(null) :
                                    this.ReadUnquotedKey();

                if (genericDictionaryType == null && memberMap != null)
                {
                    // determine the type of the property/field
                    memberType = TypeCoercionUtility.GetMemberInfo(memberMap, memberName, out memberInfo);
                }
                else
                {
                    memberType = genericDictionaryType;
                    memberInfo = null;
                }

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

                // consume delim
                this.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 && this.Settings.IsTypeHintName(memberName))
                    {
                        result = this.Settings.Coercion.ProcessTypeHint((IDictionary)result, value as string, out objectType, out memberMap);
                    }
                    else
                    {
                        ((IDictionary)result)[memberName] = value;
                    }
                }
                else if (objectType.GetInterface(JsonReader.TypeGenericIDictionary) != null)
                {
                    throw new JsonDeserializationException(
                              String.Format(JsonReader.ErrorGenericIDictionary, objectType),
                              this.index);
                }
                else
                {
                    this.Settings.Coercion.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);
        }
Ejemplo n.º 6
0
        private void PopulateObject(object result, Type objectType, Dictionary <string, MemberInfo> memberMap, Type genericDictionaryType)
        {
            if (this.Source[this.index] != JsonReader.OperatorObjectStart)
            {
                throw new JsonDeserializationException(JsonReader.ErrorExpectedObject, this.index);
            }

            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(this.Settings.AllowUnquotedObjectKeys);
                if (token == JsonToken.ObjectEnd)
                {
                    break;
                }

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

                // parse object member value
                string memberName = (token == JsonToken.String) ?
                                    (String)this.ReadString(null) :
                                    this.ReadUnquotedKey();

                if (genericDictionaryType == null && memberMap != null)
                {
                    // determine the type of the property/field
                    memberType = TypeCoercionUtility.GetMemberInfo(memberMap, memberName, out memberInfo);
                }
                else
                {
                    memberType = genericDictionaryType;
                    memberInfo = null;
                }

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

                // consume delim
                this.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 && this.Settings.IsTypeHintName(memberName))
                    {
                        result = this.Settings.Coercion.ProcessTypeHint((IDictionary)result, value as string, out objectType, out memberMap);
                    }
                    else
                    {
                        ((IDictionary)result)[memberName] = value;
                    }
                }
                else if (objectType.GetInterface(JsonReader.TypeGenericIDictionary) != null)
                {
                    throw new JsonDeserializationException(
                              String.Format(JsonReader.ErrorGenericIDictionary, objectType),
                              this.index);
                }
                else
                {
                    this.Settings.Coercion.SetMemberValue(result, memberType, memberInfo, value);
                }

                /*
                 * if (memberInfo == null && memberType == null)
                 *          {
                 *  Debug.LogErrorFormat("Can't find MemberInfo with Name - {0}, Type - {1}. You need to create it in object: {2}",
                 *                      memberName, value.GetType().Name, result.GetType().Name);
                 *          }
                 */
                // 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;
        }
Ejemplo n.º 7
0
        protected virtual object ReadObject(Type objectType)
        {
            if (this.Source[this.index] != '{')
            {
                throw new JsonDeserializationException("Expected JSON object.", this.index);
            }
            Type type = null;
            Dictionary <string, MemberInfo> dictionary = null;
            object obj;

            if (objectType != null)
            {
                obj = this.Settings.Coercion.InstantiateObject(objectType, out dictionary);
                if (dictionary == null)
                {
                    Type @interface = objectType.GetInterface("System.Collections.Generic.IDictionary`2");
                    if (@interface != null)
                    {
                        Type[] genericArguments = @interface.GetGenericArguments();
                        if (genericArguments.Length == 2)
                        {
                            if (genericArguments[0] != typeof(string))
                            {
                                throw new JsonDeserializationException(string.Format("Types which implement Generic IDictionary<TKey, TValue> need to have string keys to be deserialized. ({0})", objectType), this.index);
                            }
                            if (genericArguments[1] != typeof(object))
                            {
                                type = genericArguments[1];
                            }
                        }
                    }
                }
            }
            else
            {
                obj = new Dictionary <string, object>();
            }
            JsonToken jsonToken;

            for (;;)
            {
                this.index++;
                if (this.index >= this.SourceLength)
                {
                    break;
                }
                jsonToken = this.Tokenize(this.Settings.AllowUnquotedObjectKeys);
                if (jsonToken == JsonToken.ObjectEnd)
                {
                    goto Block_9;
                }
                if (jsonToken != JsonToken.String && jsonToken != JsonToken.UnquotedName)
                {
                    goto Block_11;
                }
                string     text = (jsonToken != JsonToken.String) ? this.ReadUnquotedKey() : ((string)this.ReadString(null));
                MemberInfo memberInfo;
                Type       type2;
                if (type == null && dictionary != null)
                {
                    type2 = TypeCoercionUtility.GetMemberInfo(dictionary, text, out memberInfo);
                }
                else
                {
                    type2      = type;
                    memberInfo = null;
                }
                jsonToken = this.Tokenize();
                if (jsonToken != JsonToken.NameDelim)
                {
                    goto Block_15;
                }
                this.index++;
                if (this.index >= this.SourceLength)
                {
                    goto Block_16;
                }
                object obj2 = this.Read(type2, false);
                if (obj is IDictionary)
                {
                    if (objectType == null && this.Settings.IsTypeHintName(text))
                    {
                        obj = this.Settings.Coercion.ProcessTypeHint((IDictionary)obj, obj2 as string, out objectType, out dictionary);
                    }
                    else
                    {
                        ((IDictionary)obj)[text] = obj2;
                    }
                }
                else
                {
                    if (objectType.GetInterface("System.Collections.Generic.IDictionary`2") != null)
                    {
                        goto Block_20;
                    }
                    this.Settings.Coercion.SetMemberValue(obj, type2, memberInfo, obj2);
                }
                jsonToken = this.Tokenize();
                if (jsonToken != JsonToken.ValueDelim)
                {
                    goto IL_281;
                }
            }
            throw new JsonDeserializationException("Unterminated JSON object.", this.index);
Block_9:
            goto IL_281;
Block_11:
            throw new JsonDeserializationException("Expected JSON object property name.", this.index);
Block_15:
            throw new JsonDeserializationException("Expected JSON object property name delimiter.", this.index);
Block_16:
            throw new JsonDeserializationException("Unterminated JSON object.", this.index);
Block_20:
            throw new JsonDeserializationException(string.Format("Types which implement Generic IDictionary<TKey, TValue> also need to implement IDictionary to be deserialized. ({0})", objectType), this.index);
IL_281:
            if (jsonToken != JsonToken.ObjectEnd)
            {
                throw new JsonDeserializationException("Unterminated JSON object.", this.index);
            }
            this.index++;
            return(obj);
        }
Ejemplo n.º 8
0
        private object ReadObject(Type objectType)
        {
            object    obj2;
            JsonToken token;

            if (this.Source[this.index] != '{')
            {
                throw new JsonDeserializationException("Expected JSON object.", this.index);
            }
            Type type = null;
            Dictionary <string, MemberInfo> memberMap = null;

            if (objectType != null)
            {
                #region 这里是映射JsonWriter(619行)所设定的特殊类型
                if (objectType == typeof(Vector2))
                {
                    obj2      = new Vector2();
                    memberMap = this.Settings.Coercion.CreateFieldMemberMap(objectType);
                }
                else if (objectType == typeof(Vector3))
                {
                    obj2      = new Vector3();
                    memberMap = this.Settings.Coercion.CreateFieldMemberMap(objectType);
                }
                else if (objectType == typeof(Vector4))
                {
                    obj2      = new Vector4();
                    memberMap = this.Settings.Coercion.CreateFieldMemberMap(objectType);
                }
                #endregion
                else
                {
                    obj2 = this.Settings.Coercion.InstantiateObject(objectType, out memberMap);
                }

                if (memberMap == null)
                {
                    Type type2 = objectType.GetInterface("System.Collections.Generic.IDictionary`2");
                    if (type2 != null)
                    {
                        Type[] genericArguments = type2.GetGenericArguments();
                        if (genericArguments.Length == 2)
                        {
                            if (genericArguments[0] != typeof(string))
                            {
                                throw new JsonDeserializationException(string.Format("Types which implement Generic IDictionary<TKey, TValue> need to have string keys to be deserialized. ({0})", objectType), this.index);
                            }
                            if (genericArguments[1] != typeof(object))
                            {
                                type = genericArguments[1];
                            }
                        }
                    }
                }
            }
            else
            {
                obj2 = new Dictionary <string, object>();
            }
            do
            {
                Type       type3;
                MemberInfo info;
                this.index++;
                if (this.index >= this.SourceLength)
                {
                    throw new JsonDeserializationException("Unterminated JSON object.", this.index);
                }
                token = this.Tokenize(this.Settings.AllowUnquotedObjectKeys);
                if (token == JsonToken.ObjectEnd)
                {
                    break;
                }
                if ((token != JsonToken.String) && (token != JsonToken.UnquotedName))
                {
                    throw new JsonDeserializationException("Expected JSON object property name.", this.index);
                }
                string memberName = (token != JsonToken.String) ? this.ReadUnquotedKey() : ((string)this.ReadString(null));
                if ((type == null) && (memberMap != null))
                {
                    type3 = TypeCoercionUtility.GetMemberInfo(memberMap, memberName, out info);
                }
                else
                {
                    type3 = type;
                    info  = null;
                }
                if (this.Tokenize() != JsonToken.NameDelim)
                {
                    throw new JsonDeserializationException("Expected JSON object property name delimiter.", this.index);
                }
                this.index++;
                if (this.index >= this.SourceLength)
                {
                    throw new JsonDeserializationException("Unterminated JSON object.", this.index);
                }
                object obj3 = this.Read(type3, false);
                if (obj2 is IDictionary)
                {
                    if ((objectType == null) && this.Settings.IsTypeHintName(memberName))
                    {
                        obj2 = this.Settings.Coercion.ProcessTypeHint((IDictionary)obj2, obj3 as string, out objectType, out memberMap);
                    }
                    else
                    {
                        ((IDictionary)obj2)[memberName] = obj3;
                    }
                }
                else
                {
                    if (objectType.GetInterface("System.Collections.Generic.IDictionary`2") != null)
                    {
                        throw new JsonDeserializationException(string.Format("Types which implement Generic IDictionary<TKey, TValue> also need to implement IDictionary to be deserialized. ({0})", objectType), this.index);
                    }
                    this.Settings.Coercion.SetMemberValue(obj2, type3, info, obj3);
                }
                token = this.Tokenize();
            }while (token == JsonToken.ValueDelim);
            if (token != JsonToken.ObjectEnd)
            {
                throw new JsonDeserializationException("Unterminated JSON object.", this.index);
            }
            this.index++;
            return(obj2);
        }
Ejemplo n.º 9
0
        private object ReadObject(Type objectType)
        {
            if (this.Source[this.index] != JsonReader.OperatorObjectStart)
            {
                throw new JsonDeserializationException(JsonReader.ErrorExpectedObject, this.index);
            }

            // If this is a Date, then we should be reading an EJSON Date
            if (objectType != null &&
                objectType == typeof(DateTime))
            {
                // Read an EJSON result instead
                var ejsonDateResult = this.ReadObject(typeof(Meteor.EJSON.EJSONDate)) as Meteor.EJSON.EJSONDate;
                var returnDate      = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddTicks(ejsonDateResult.date * 10000L);
                return(returnDate);
            }

            // If this is a byte[] destination and we're reading an object, we're handling an EJSON UInt8 array
            if (objectType != null &&
                objectType == typeof(byte[]))
            {
                var ejsonBinaryResult = this.ReadObject(typeof(Meteor.EJSON.EJSONUInt8Array)) as Meteor.EJSON.EJSONUInt8Array;
                var returnBytes       = System.Convert.FromBase64String(ejsonBinaryResult.binary);
                return(returnBytes);
            }

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

            if (objectType != null)
            {
                result = this.Settings.Coercion.InstantiateObject(objectType, out memberMap);

                if (memberMap == null)
                {
                    // this allows specific IDictionary<string, T> to deserialize T
                    Type genericDictionary = objectType.GetInterface(JsonReader.TypeGenericIDictionary);
                    if (genericDictionary != null)
                    {
                        Type[] genericArgs = genericDictionary.GetGenericArguments();
                        if (genericArgs.Length == 2)
                        {
                            if (genericArgs[0] != typeof(String))
                            {
                                throw new JsonDeserializationException(
                                          String.Format(JsonReader.ErrorGenericIDictionaryKeys, objectType),
                                          this.index);
                            }

                            if (genericArgs[1] != typeof(Object))
                            {
                                genericDictionaryType = genericArgs[1];
                            }
                        }
                    }
                }
            }
            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(this.Settings.AllowUnquotedObjectKeys);
                if (token == JsonToken.ObjectEnd)
                {
                    break;
                }

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

                // parse object member value
                string memberName = (token == JsonToken.String) ?
                                    (String)this.ReadString(null) :
                                    this.ReadUnquotedKey();

                if (genericDictionaryType == null && memberMap != null)
                {
                    // determine the type of the property/field
                    memberType = TypeCoercionUtility.GetMemberInfo(memberMap, memberName, out memberInfo);
                }
                else
                {
                    memberType = genericDictionaryType;
                    memberInfo = null;
                }

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

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

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

                if (result is IDictionary)
                {
                    if (objectType == null && this.Settings.IsTypeHintName(memberName))
                    {
                        result = this.Settings.Coercion.ProcessTypeHint((IDictionary)result, value as string, out objectType, out memberMap);
                    }
                    else
                    {
                        ((IDictionary)result)[memberName] = value;
                    }
                }
                else if (objectType.GetInterface(JsonReader.TypeGenericIDictionary) != null)
                {
                    throw new JsonDeserializationException(
                              String.Format(JsonReader.ErrorGenericIDictionary, objectType),
                              this.index);
                }
                else
                {
                    this.Settings.Coercion.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);
        }
Ejemplo n.º 10
0
        private void PopulateObject(ref object result, Type objectType, Dictionary <string, MemberInfo> memberMap, Type genericDictionaryType)
        {
            if (this.Source[this.index] != JsonReader.OperatorObjectStart)
            {
                throw new JsonDeserializationException(JsonReader.ErrorExpectedObject, this.index);
            }

#if WINPHONE_8
            IDictionary idict = result as IDictionary;
#else
            IDictionary idict = result as IDictionary;

            if (idict == null && objectType.GetInterface(JsonReader.TypeGenericIDictionary) != null)
            {
                throw new JsonDeserializationException(
                          String.Format(JsonReader.ErrorGenericIDictionary, objectType),
                          this.index);
            }
#endif

            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(this.Settings.AllowUnquotedObjectKeys);
                if (token == JsonToken.ObjectEnd)
                {
                    break;
                }

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

                // parse object member value
                string memberName = (token == JsonToken.String) ?
                                    (String)this.ReadString(null) :
                                    this.ReadUnquotedKey();

                //
                if (genericDictionaryType == null && memberMap != null)
                {
                    // determine the type of the property/field
                    memberType = TypeCoercionUtility.GetMemberInfo(memberMap, memberName, out memberInfo);
                }
                else
                {
                    memberType = genericDictionaryType;
                    memberInfo = null;
                }

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

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

                object value;

                // Reference to previously deserialized value
                if (Settings.HandleCyclicReferences && memberName == "@ref")
                {
                    // parse object member value
                    int refId = (int)this.Read(typeof(int), false);

                    // Change result object to the one previously deserialized
                    result = previouslyDeserialized[refId];

                    // get next token
                    // this will probably be the end of the object
                    token = this.Tokenize();
                    continue;
                }
                else
                {
                    // Normal serialized value

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

                if (idict != null)
                {
                    if (objectType == null && this.Settings.IsTypeHintName(memberName))
                    {
                        result = this.Settings.Coercion.ProcessTypeHint(idict, value as string, out objectType, out memberMap);
                    }
                    else
                    {
                        idict[memberName] = value;
                    }
                }
                else
                {
                    this.Settings.Coercion.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;
        }