Exemplo n.º 1
0
    // Token: 0x06015702 RID: 87810 RVA: 0x0056F278 File Offset: 0x0056D478
    private static object ReadValue(Type inst_type, JsonReader reader)
    {
        reader.Read();
        if (reader.Token == JsonToken.ArrayEnd)
        {
            return(null);
        }
        if (reader.Token == JsonToken.Null)
        {
            if (!inst_type.IsClass)
            {
                throw new JsonException(string.Format("Can't assign null to an instance of type {0}", inst_type));
            }
            return(null);
        }
        else
        {
            if (reader.Token != JsonToken.Double && reader.Token != JsonToken.Int && reader.Token != JsonToken.Long && reader.Token != JsonToken.String && reader.Token != JsonToken.Boolean)
            {
                object obj = null;
                if (reader.Token == JsonToken.ArrayStart)
                {
                    JsonMapper.AddArrayMetadata(inst_type);
                    ArrayMetadata arrayMetadata = JsonMapper.array_metadata[inst_type];
                    if (!arrayMetadata.IsArray && !arrayMetadata.IsList)
                    {
                        throw new JsonException(string.Format("Type {0} can't act as an array", inst_type));
                    }
                    IList list;
                    Type  elementType;
                    if (!arrayMetadata.IsArray)
                    {
                        list        = (IList)Activator.CreateInstance(inst_type);
                        elementType = arrayMetadata.ElementType;
                    }
                    else
                    {
                        list        = new ArrayList();
                        elementType = inst_type.GetElementType();
                    }
                    for (;;)
                    {
                        object value = JsonMapper.ReadValue(elementType, reader);
                        if (reader.Token == JsonToken.ArrayEnd)
                        {
                            break;
                        }
                        list.Add(value);
                    }
                    if (arrayMetadata.IsArray)
                    {
                        int count = list.Count;
                        obj = Array.CreateInstance(elementType, count);
                        for (int i = 0; i < count; i++)
                        {
                            ((Array)obj).SetValue(list[i], i);
                        }
                    }
                    else
                    {
                        obj = list;
                    }
                }
                else if (reader.Token == JsonToken.ObjectStart)
                {
                    JsonMapper.AddObjectMetadata(inst_type);
                    ObjectMetadata objectMetadata = JsonMapper.object_metadata[inst_type];
                    obj = Activator.CreateInstance(inst_type);
                    string text;
                    for (;;)
                    {
                        reader.Read();
                        if (reader.Token == JsonToken.ObjectEnd)
                        {
                            break;
                        }
                        text = (string)reader.Value;
                        if (objectMetadata.Properties.ContainsKey(text))
                        {
                            PropertyMetadata propertyMetadata = objectMetadata.Properties[text];
                            if (propertyMetadata.IsField)
                            {
                                ((FieldInfo)propertyMetadata.Info).SetValue(obj, JsonMapper.ReadValue(propertyMetadata.Type, reader));
                            }
                            else
                            {
                                PropertyInfo propertyInfo = (PropertyInfo)propertyMetadata.Info;
                                if (propertyInfo.CanWrite)
                                {
                                    propertyInfo.SetValue(obj, JsonMapper.ReadValue(propertyMetadata.Type, reader), null);
                                }
                                else
                                {
                                    JsonMapper.ReadValue(propertyMetadata.Type, reader);
                                }
                            }
                        }
                        else
                        {
                            if (!objectMetadata.IsDictionary)
                            {
                                goto Block_27;
                            }
                            ((IDictionary)obj).Add(text, JsonMapper.ReadValue(objectMetadata.ElementType, reader));
                        }
                    }
                    return(obj);

Block_27:
                    throw new JsonException(string.Format("The type {0} doesn't have the property '{1}'", inst_type, text));
                }
                return(obj);
            }
            Type type = reader.Value.GetType();
            if (inst_type.IsAssignableFrom(type))
            {
                return(reader.Value);
            }
            if (JsonMapper.custom_importers_table.ContainsKey(type) && JsonMapper.custom_importers_table[type].ContainsKey(inst_type))
            {
                ImporterFunc importerFunc = JsonMapper.custom_importers_table[type][inst_type];
                return(importerFunc(reader.Value));
            }
            if (JsonMapper.base_importers_table.ContainsKey(type) && JsonMapper.base_importers_table[type].ContainsKey(inst_type))
            {
                ImporterFunc importerFunc2 = JsonMapper.base_importers_table[type][inst_type];
                return(importerFunc2(reader.Value));
            }
            if (inst_type.IsEnum)
            {
                return(Enum.ToObject(inst_type, reader.Value));
            }
            MethodInfo convOp = JsonMapper.GetConvOp(inst_type, type);
            if (convOp != null)
            {
                return(convOp.Invoke(null, new object[]
                {
                    reader.Value
                }));
            }
            throw new JsonException(string.Format("Can't assign value '{0}' (type {1}) to type {2}", reader.Value, type, inst_type));
        }
    }