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(); } while (true) { object obj2 = JsonMapper.ReadValue(elementType, reader); if (obj2 == null && reader.Token == JsonToken.ArrayEnd) { break; } list.Add(obj2); } 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; while (true) { 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) { if (!reader.SkipNonMembers) { goto Block_29; } JsonMapper.ReadSkip(reader); } else { ((IDictionary)obj).Add(text, JsonMapper.ReadValue(objectMetadata.ElementType, reader)); } } return(obj); Block_29: 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)); } }
private static object ReadValue(Type inst_type, JsonReader reader) { reader.Read(); if (reader.Token == JsonToken.ArrayEnd) { return((object)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}", (object)inst_type)); } return((object)null); } if (reader.Token == JsonToken.Double || reader.Token == JsonToken.Int || (reader.Token == JsonToken.Long || reader.Token == JsonToken.String) || reader.Token == JsonToken.Boolean) { 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)) { return(JsonMapper.custom_importers_table[type][inst_type](reader.Value)); } if (JsonMapper.base_importers_table.ContainsKey(type) && JsonMapper.base_importers_table[type].ContainsKey(inst_type)) { return(JsonMapper.base_importers_table[type][inst_type](reader.Value)); } if (inst_type.IsEnum) { return(Enum.ToObject(inst_type, reader.Value)); } MethodInfo convOp = JsonMapper.GetConvOp(inst_type, type); if (!(convOp != (MethodInfo)null)) { throw new JsonException(string.Format("Can't assign value '{0}' (type {1}) to type {2}", reader.Value, (object)type, (object)inst_type)); } return(convOp.Invoke((object)null, new object[1] { reader.Value })); } object obj1 = (object)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", (object)inst_type)); } IList list; Type elementType; if (!arrayMetadata.IsArray) { list = (IList)Activator.CreateInstance(inst_type); elementType = arrayMetadata.ElementType; } else { list = (IList) new ArrayList(); elementType = inst_type.GetElementType(); } while (true) { object obj2 = JsonMapper.ReadValue(elementType, reader); if (obj2 != null || reader.Token != JsonToken.ArrayEnd) { list.Add(obj2); } else { break; } } if (arrayMetadata.IsArray) { int count = list.Count; obj1 = (object)Array.CreateInstance(elementType, count); for (int index = 0; index < count; ++index) { ((Array)obj1).SetValue(list[index], index); } } else { obj1 = (object)list; } } else if (reader.Token == JsonToken.ObjectStart) { JsonMapper.AddObjectMetadata(inst_type); ObjectMetadata objectMetadata = JsonMapper.object_metadata[inst_type]; obj1 = Activator.CreateInstance(inst_type); string key; while (true) { reader.Read(); if (reader.Token != JsonToken.ObjectEnd) { key = (string)reader.Value; if (objectMetadata.Properties.ContainsKey(key)) { PropertyMetadata property = objectMetadata.Properties[key]; if (property.IsField) { ((FieldInfo)property.Info).SetValue(obj1, JsonMapper.ReadValue(property.Type, reader)); } else { PropertyInfo info = (PropertyInfo)property.Info; if (info.CanWrite) { info.SetValue(obj1, JsonMapper.ReadValue(property.Type, reader), (object[])null); } else { JsonMapper.ReadValue(property.Type, reader); } } } else if (!objectMetadata.IsDictionary) { if (reader.SkipNonMembers) { JsonMapper.ReadSkip(reader); } else { break; } } else { ((IDictionary)obj1).Add((object)key, JsonMapper.ReadValue(objectMetadata.ElementType, reader)); } } else { goto label_45; } } throw new JsonException(string.Format("The type {0} doesn't have the property '{1}'", (object)inst_type, (object)key)); } label_45: return(obj1); }