private static IJsonWrapper ReadValue(WrapperFactory factory, JsonReader reader) { reader.Read(); if (reader.Token == JsonToken.ArrayEnd || reader.Token == JsonToken.Null) { return(null); } IJsonWrapper instance = factory(); if (reader.Token == JsonToken.String) { instance.SetString((string)reader.Value); return(instance); } if (reader.Token == JsonToken.Double) { instance.SetDouble((double)reader.Value); return(instance); } if (reader.Token == JsonToken.Int) { instance.SetInt((int)reader.Value); return(instance); } if (reader.Token == JsonToken.Long) { instance.SetLong((long)reader.Value); return(instance); } if (reader.Token == JsonToken.Boolean) { instance.SetBoolean((bool)reader.Value); return(instance); } if (reader.Token == JsonToken.ArrayStart) { instance.SetJsonType(JsonType.Array); while (true) { IJsonWrapper item = ReadValue(factory, reader); if (item == null && reader.Token == JsonToken.ArrayEnd) { break; } ((IList)instance).Add(item); } } else if (reader.Token == JsonToken.ObjectStart) { instance.SetJsonType(JsonType.Object); while (true) { reader.Read(); if (reader.Token == JsonToken.ObjectEnd) { break; } string property = (string)reader.Value; ((IDictionary)instance)[property] = ReadValue( factory, reader); } } return(instance); }
private static IJsonWrapper ReadValue(WrapperFactory factory, JsonReader reader) { reader.Read(); if (reader.Token == JsonToken.ArrayEnd || reader.Token == JsonToken.Null) { return(null); } IJsonWrapper jsonWrapper = factory(); if (reader.Token == JsonToken.String) { jsonWrapper.SetString((string)reader.Value); return(jsonWrapper); } if (reader.Token == JsonToken.Double) { jsonWrapper.SetDouble((double)reader.Value); return(jsonWrapper); } if (reader.Token == JsonToken.Int) { jsonWrapper.SetInt((int)reader.Value); return(jsonWrapper); } if (reader.Token == JsonToken.Long) { jsonWrapper.SetLong((long)reader.Value); return(jsonWrapper); } if (reader.Token == JsonToken.Boolean) { jsonWrapper.SetBoolean((bool)reader.Value); return(jsonWrapper); } if (reader.Token == JsonToken.ArrayStart) { jsonWrapper.SetJsonType(JsonType.Array); while (true) { bool flag = true; IJsonWrapper jsonWrapper2 = ReadValue(factory, reader); if (jsonWrapper2 == null && reader.Token == JsonToken.ArrayEnd) { break; } jsonWrapper.Add(jsonWrapper2); } } else if (reader.Token == JsonToken.ObjectStart) { jsonWrapper.SetJsonType(JsonType.Object); while (true) { bool flag = true; reader.Read(); if (reader.Token == JsonToken.ObjectEnd) { break; } string key = (string)reader.Value; jsonWrapper[key] = ReadValue(factory, reader); } } return(jsonWrapper); }
private static IJsonWrapper ReadValue(WrapperFactory factory, JsonReader reader) { reader.Read(); if (reader.Token == JsonToken.ArrayEnd || reader.Token == JsonToken.Null) { return((IJsonWrapper)null); } IJsonWrapper jsonWrapper1 = factory(); if (reader.Token == JsonToken.String) { jsonWrapper1.SetString((string)reader.Value); return(jsonWrapper1); } if (reader.Token == JsonToken.Double) { jsonWrapper1.SetDouble((double)reader.Value); return(jsonWrapper1); } if (reader.Token == JsonToken.Int) { jsonWrapper1.SetInt((int)reader.Value); return(jsonWrapper1); } if (reader.Token == JsonToken.Long) { jsonWrapper1.SetLong((long)reader.Value); return(jsonWrapper1); } if (reader.Token == JsonToken.Boolean) { jsonWrapper1.SetBoolean((bool)reader.Value); return(jsonWrapper1); } if (reader.Token == JsonToken.ArrayStart) { jsonWrapper1.SetJsonType(JsonType.Array); while (true) { IJsonWrapper jsonWrapper2 = JsonMapper.ReadValue(factory, reader); if (jsonWrapper2 != null || reader.Token != JsonToken.ArrayEnd) { jsonWrapper1.Add((object)jsonWrapper2); } else { break; } } } else if (reader.Token == JsonToken.ObjectStart) { jsonWrapper1.SetJsonType(JsonType.Object); while (true) { reader.Read(); if (reader.Token != JsonToken.ObjectEnd) { string str = (string)reader.Value; jsonWrapper1[(object)str] = (object)JsonMapper.ReadValue(factory, reader); } else { break; } } } return(jsonWrapper1); }
private static IJsonWrapper ReadValue(WrapperFactory factory, JsonReader reader) { reader.Read(); if (reader.Token == JsonToken.ArrayEnd || reader.Token == JsonToken.Null) { return(null); } IJsonWrapper instance = factory(); if (reader.Token == JsonToken.String) { instance.SetString((string)reader.Value); return(instance); } if (reader.Token == JsonToken.Double) { instance.SetDouble((double)reader.Value); return(instance); } if (reader.Token == JsonToken.Int) { instance.SetInt((int)reader.Value); return(instance); } if (reader.Token == JsonToken.Long) { instance.SetLong((long)reader.Value); return(instance); } if (reader.Token == JsonToken.Boolean) { instance.SetBoolean((bool)reader.Value); return(instance); } if (reader.Token == JsonToken.ArrayStart) { instance.SetJsonType(JsonType.Array); while (true) { IJsonWrapper item = ReadValue(factory, reader); // nij - added check to see if the item is not null. This is to handle arrays within arrays. // In those cases when the outer array read the inner array an item was returned back the current // reader.Token is at the ArrayEnd for the inner array. if (item == null && reader.Token == JsonToken.ArrayEnd) { break; } ((IList)instance).Add(item); } } else if (reader.Token == JsonToken.ObjectStart) { instance.SetJsonType(JsonType.Object); while (true) { reader.Read(); if (reader.Token == JsonToken.ObjectEnd) { break; } string property = (string)reader.Value; ((IDictionary)instance)[property] = ReadValue( factory, reader); } } return(instance); }
private static object ReadValue(Type inst_type, JsonReader reader) { reader.Read(); if (reader.Token == JsonToken.ArrayEnd) { return(null); } Type underlying_type = Nullable.GetUnderlyingType(inst_type); Type value_type = underlying_type ?? inst_type; if (reader.Token == JsonToken.Null) { if (inst_type.IsClass || underlying_type != null) { return(null); } throw new JsonException(String.Format( "Can't assign null to an instance of type {0}", inst_type)); } if (reader.Token == JsonToken.Double || reader.Token == JsonToken.Int || reader.Token == JsonToken.Long || reader.Token == JsonToken.String || reader.Token == JsonToken.Boolean) { Type json_type = reader.Value.GetType(); if (value_type.IsAssignableFrom(json_type)) { return(reader.Value); } // If there's a custom importer that fits, use it if (custom_importers_table.ContainsKey(json_type) && custom_importers_table[json_type].ContainsKey( value_type)) { ImporterFunc importer = custom_importers_table[json_type][value_type]; return(importer(reader.Value)); } // Maybe there's a base importer that works if (base_importers_table.ContainsKey(json_type) && base_importers_table[json_type].ContainsKey( value_type)) { ImporterFunc importer = base_importers_table[json_type][value_type]; return(importer(reader.Value)); } // Maybe it's an enum if (value_type.IsEnum) { return(Enum.ToObject(value_type, reader.Value)); } // Try using an implicit conversion operator MethodInfo conv_op = GetConvOp(value_type, json_type); if (conv_op != null) { return(conv_op.Invoke(null, new object[] { reader.Value })); } // No luck throw new JsonException(String.Format( "Can't assign value '{0}' (type {1}) to type {2}", reader.Value, json_type, inst_type)); } object instance = null; if (reader.Token == JsonToken.ArrayStart) { AddArrayMetadata(inst_type); ArrayMetadata t_data = array_metadata[inst_type]; if (!t_data.IsArray && !t_data.IsList) { throw new JsonException(String.Format( "Type {0} can't act as an array", inst_type)); } IList list; Type elem_type; if (!t_data.IsArray) { list = (IList)Activator.CreateInstance(inst_type); elem_type = t_data.ElementType; } else { list = new ArrayList(); elem_type = inst_type.GetElementType(); } while (true) { object item = ReadValue(elem_type, reader); if (item == null && reader.Token == JsonToken.ArrayEnd) { break; } list.Add(item); } if (t_data.IsArray) { int n = list.Count; instance = Array.CreateInstance(elem_type, n); for (int i = 0; i < n; i++) { ((Array)instance).SetValue(list[i], i); } } else { instance = list; } } else if (reader.Token == JsonToken.ObjectStart) { if (custom_object_convert.ContainsKey(value_type)) { WrapperFactory factory = delegate { return(new JsonData()); }; IJsonWrapper wrapper = factory(); wrapper.SetJsonType(JsonType.Object); while (true) { reader.Read(); if (reader.Token == JsonToken.ObjectEnd) { break; } string property = (string)reader.Value; ((IDictionary)wrapper)[property] = ReadValue( factory, reader); } ConvertFunc convert = custom_object_convert[value_type]; return(convert(wrapper)); } else { AddObjectMetadata(value_type); ObjectMetadata t_data = object_metadata[value_type]; instance = Activator.CreateInstance(value_type); while (true) { reader.Read(); if (reader.Token == JsonToken.ObjectEnd) { break; } string property = (string)reader.Value; if (t_data.Properties.ContainsKey(property)) { PropertyMetadata prop_data = t_data.Properties[property]; if (prop_data.IsField) { ((FieldInfo)prop_data.Info).SetValue( instance, ReadValue(prop_data.Type, reader)); } else { PropertyInfo p_info = (PropertyInfo)prop_data.Info; if (p_info.CanWrite) { p_info.SetValue( instance, ReadValue(prop_data.Type, reader), null); } else { ReadValue(prop_data.Type, reader); } } } else { if (!t_data.IsDictionary) { if (!reader.SkipNonMembers) { throw new JsonException(String.Format( "The type {0} doesn't have the " + "property '{1}'", inst_type, property)); } else { ReadSkip(reader); continue; } } ((IDictionary)instance).Add( property, ReadValue( t_data.ElementType, reader)); } } } } return(instance); }