internal ElementWrapper(GumboElementNode node, NodeWrapper parent, WrapperFactory factory) : base(node, parent) { _Children = factory.CreateDisposalAwareLazy(() => ImmutableArray.CreateRange(node.GetChildren().OrderBy(x => x.index_within_parent).Select(x => factory.CreateNodeWrapper(x, this)))); _Attributes = factory.CreateDisposalAwareLazy(() => ImmutableArray.CreateRange(node.GetAttributes().Select(x => factory.CreateAttributeWrapper(x, this)))); _Value = factory.CreateDisposalAwareLazy(() => string.Concat(Children.Select(x => x is ElementWrapper ? ((ElementWrapper)x).Value : ((TextWrapper)x).Value))); StartPosition = node.element.start_pos; EndPosition = node.element.end_pos; Tag = node.element.tag; TagNamespace = node.element.tag_namespace; OriginalTag = NativeUtf8Helper.StringFromNativeUtf8( node.element.original_tag.data, (int)node.element.original_tag.length); OriginalTagName = GetTagNameFromOriginalTag(node.element); OriginalEndTag = NativeUtf8Helper.StringFromNativeUtf8( node.element.original_end_tag.data, (int)node.element.original_end_tag.length); NormalizedTagName = NativeUtf8Helper.StringFromNativeUtf8( NativeMethods.gumbo_normalized_tagname(node.element.tag)); }
public void ShouldWrapThirdPartyExtensions() { var wrapperFactory = new WrapperFactory(); Window window = LaunchPetShopWindow(); AutomationElement comboBox = window.Element.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, "petFoodInput")); Assert.IsAssignableFrom(typeof(SpecialComboBox), wrapperFactory.Wrap<SpecialComboBox>(comboBox, "Special Combo")); }
public void ShouldWrapElementsToComboBoxesOrEditableComboBoxesAsAppropriate() { var wrapperFactory = new WrapperFactory(); Window window = LaunchPetShopWindow(); AutomationElement editableComboBox = window.Element.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, "petTypeInput")); AutomationElement comboBox = window.Element.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, "petFoodInput")); Assert.IsAssignableFrom(typeof(EditableComboBox), wrapperFactory.Wrap<ComboBox>(editableComboBox, "anEditableComboBox")); Assert.IsAssignableFrom(typeof(ComboBox), wrapperFactory.Wrap<ComboBox>(comboBox, "aNormalComboBox")); }
internal DocumentWrapper(GumboDocumentNode node, WrapperFactory factory) : base(node, null) { _Children = factory.CreateDisposalAwareLazy(() => ImmutableArray.CreateRange(node.GetChildren().OrderBy(x => x.index_within_parent).Select(x => factory.CreateNodeWrapper(x, this)))); HasDocType = node.document.has_doctype; Name = NativeUtf8Helper.StringFromNativeUtf8(node.document.name); PublicIdentifier = NativeUtf8Helper.StringFromNativeUtf8(node.document.public_identifier); SystemIdentifier = NativeUtf8Helper.StringFromNativeUtf8(node.document.system_identifier); DocTypeQuirksMode = node.document.doc_type_quirks_mode; }
public void ShouldWrapElementsUsingTheGivenTypeOfWrapper() { var wrapperFactory = new WrapperFactory(); var anElement = AutomationElement.RootElement; Assert.IsAssignableFrom(typeof(TextBox), wrapperFactory.Wrap<TextBox>(anElement, "aTextBox")); Assert.IsAssignableFrom(typeof(RichTextBox), wrapperFactory.Wrap<RichTextBox>(anElement, "aRichTextBox")); Assert.IsAssignableFrom(typeof(ListBox), wrapperFactory.Wrap<ListBox>(anElement, "aListBox")); Assert.IsAssignableFrom(typeof(Button), wrapperFactory.Wrap<Button>(anElement, "aButton")); Assert.IsAssignableFrom(typeof(TextBlock), wrapperFactory.Wrap<TextBlock>(anElement, "aTextBlock")); Assert.IsAssignableFrom(typeof(Label), wrapperFactory.Wrap<Label>(anElement, "aLabel")); Assert.IsAssignableFrom(typeof(Tab), wrapperFactory.Wrap<Tab>(anElement, "aTab")); Assert.IsAssignableFrom(typeof(CheckBox), wrapperFactory.Wrap<CheckBox>(anElement, "aCheckBox")); Assert.IsAssignableFrom(typeof(RadioButton), wrapperFactory.Wrap<RadioButton>(anElement, "aRadioButton")); }
public GumboWrapper(string html, GumboWrapperOptions? options = null) { _Options = CreateOptions(options); _Html = NativeUtf8Helper.NativeUtf8FromString(html); _OutputPtr = NativeMethods.gumbo_parse(_Html); var output = Marshal.PtrToStructure<GumboOutput>(_OutputPtr); _GumboDocumentNode = output.GetDocument(); Errors = output.GetErrors(); var lazyFactory = new DisposalAwareLazyFactory(() => _IsDisposed, typeof(GumboWrapper).Name); _WrapperFactory = new WrapperFactory(lazyFactory); Document = (DocumentWrapper)_WrapperFactory.CreateNodeWrapper(_GumboDocumentNode); }
public static IJsonWrapper ToWrapper(WrapperFactory factory, JsonReader reader) { return(JsonMapper.a(factory, reader)); }
public static IJsonWrapper ToWrapper(WrapperFactory factory, string json) => ReadValue(factory, new JsonReader(json));
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); }
public Wrapper(IBuildable wrapped, WrapperFactory factory) { this.wrapped = wrapped; this.factory = factory; this.service = factory.Service; }
public static IJsonWrapper ToWrapper(WrapperFactory factory, string json) { JsonReader reader = new JsonReader(json); return(ReadValue(factory, reader)); }
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 wrapper = factory(); if (reader.Token == JsonToken.String) { wrapper.SetString((string)reader.Value); return(wrapper); } if (reader.Token == JsonToken.Double) { wrapper.SetDouble((double)reader.Value); return(wrapper); } if (reader.Token == JsonToken.Int) { wrapper.SetInt((int)reader.Value); return(wrapper); } if (reader.Token == JsonToken.Long) { wrapper.SetLong((long)reader.Value); return(wrapper); } if (reader.Token == JsonToken.Boolean) { wrapper.SetBoolean((bool)reader.Value); return(wrapper); } if (reader.Token == JsonToken.ArrayStart) { wrapper.SetJsonType(JsonType.Array); while (true) { IJsonWrapper wrapper2 = ReadValue(factory, reader); if (reader.Token == JsonToken.ArrayEnd) { return(wrapper); } wrapper.Add(wrapper2); } } if (reader.Token == JsonToken.ObjectStart) { wrapper.SetJsonType(JsonType.Object); while (true) { reader.Read(); if (reader.Token == JsonToken.ObjectEnd) { return(wrapper); } string str = (string)reader.Value; wrapper[str] = ReadValue(factory, reader); } } return(wrapper); }
public PersonalViewModel BuildPersonalViewModel() { var currentConvention = Actor.DisplayConvention.ConventionId; var currentUser = Actor.Me.Id; var myEngagement = Session.Query <ConventionEngagement>() .Include(x => x.ConventionId) .Include(x => x.ParticipantId) .Include(x => x.EventIds) .Include(x => x.SuggestedEventIds) .FirstOrDefault(x => x.ConventionId == currentConvention && x.ParticipantId == currentUser); var myRelatedEngagements = Session.Query <ConventionEngagement>() .Include(x => x.ParticipantId) .Include(x => x.EventIds) .Where(x => x.CreatorId == currentUser && x.ParticipantId != currentUser && x.ConventionId == currentConvention) .ToList(); if (myEngagement == null) { myEngagement = new ConventionEngagement { CreatorId = currentUser, ParticipantId = currentUser, ConventionId = currentConvention, ConventionStartDate = Actor.DisplayConvention.Days .Min(x => x.Date) .ToString("yyyy-MM-dd", CultureInfo.InvariantCulture), IsLongTerm = true, }; Session.Store(myEngagement); Session.SaveChanges(); } var allEvents = myEngagement.EventIds; allEvents.AddRange(myEngagement.SuggestedEventIds); var allParticipants = new List <string>(); foreach (var myRelatedEngagement in myRelatedEngagements) { allParticipants.Add(myRelatedEngagement.ParticipantId); allEvents.AddRange(myRelatedEngagement.EventIds); } // Preload Events Session .Include <Event>(x => x.ConventionDayId) .Load <Event>(allEvents); var result = new PersonalViewModel(); var wrapperFactory = new WrapperFactory(Session); result.MyEngagement = wrapperFactory.Wrap(myEngagement); result.RelatedEngagements = wrapperFactory.Wrap(myRelatedEngagements); return(result); }
private static IJsonWrapper a(WrapperFactory A_0, JsonReader A_1) { A_1.Read(); if (A_1.Token == JsonToken.ArrayEnd || A_1.Token == JsonToken.Null) { return(null); } IJsonWrapper jsonWrapper = A_0(); if (A_1.Token == JsonToken.String) { jsonWrapper.SetString((string)A_1.Value); return(jsonWrapper); } if (A_1.Token == JsonToken.Double) { jsonWrapper.SetDouble((double)A_1.Value); return(jsonWrapper); } if (A_1.Token == JsonToken.Int) { jsonWrapper.SetInt((int)A_1.Value); return(jsonWrapper); } if (A_1.Token == JsonToken.Long) { jsonWrapper.SetLong((long)A_1.Value); return(jsonWrapper); } if (A_1.Token == JsonToken.Boolean) { jsonWrapper.SetBoolean((bool)A_1.Value); return(jsonWrapper); } if (A_1.Token == JsonToken.ArrayStart) { jsonWrapper.SetJsonType(JsonType.Array); while (true) { IJsonWrapper jsonWrapper2 = JsonMapper.a(A_0, A_1); if (jsonWrapper2 == null && A_1.Token == JsonToken.ArrayEnd) { break; } jsonWrapper.Add(jsonWrapper2); } } else if (A_1.Token == JsonToken.ObjectStart) { jsonWrapper.SetJsonType(JsonType.Object); while (true) { A_1.Read(); if (A_1.Token == JsonToken.ObjectEnd) { break; } string key = (string)A_1.Value; jsonWrapper[key] = JsonMapper.a(A_0, A_1); } } return(jsonWrapper); }
public Wrapper(IBuildable wrapped, WrapperFactory factory) { this.wrapped = wrapped; this.factory = factory; }
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 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) { 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) { reader.Read(); if (reader.Token == JsonToken.ObjectEnd) { break; } string key = (string)reader.Value; jsonWrapper[key] = ReadValue(factory, reader); } } return(jsonWrapper); }
public static IJsonWrapper ToWrapper(WrapperFactory factory, string json) { JsonReader a_ = new JsonReader(json); return(JsonMapper.a(factory, a_)); }
public static IJsonWrapper ToWrapper(WrapperFactory factory, JsonReader reader) => ReadValue(factory, reader);
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 (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; }
public static IJsonWrapper ToWrapper(WrapperFactory factory, JsonReader reader) { return(ReadValue(factory, reader)); }
/** */ public static IJsonWrapper ToWrapper(WrapperFactory factory, JsonReader reader) { return ReadValue (factory, reader); }
/** */ public static IJsonWrapper ToWrapper(WrapperFactory factory, string json) { JsonReader reader = new JsonReader (json); return ReadValue (factory, reader); }
public Wrapper(IBuildable wrapped, WrapperFactory factory) { this.wrapped = wrapped; this.factory = factory; this.service = factory.Service; }
public Wrapper(IBuildable wrapped, WrapperFactory factory) { this.wrapped = wrapped; this.factory = factory; }
private static object ReadValue(Type inst_type, JsonReader reader) { reader.Read(inst_type); 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); } 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 (inst_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( inst_type)) { ImporterFunc importer = custom_importers_table[json_type][inst_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( inst_type)) { ImporterFunc importer = base_importers_table[json_type][inst_type]; return(importer(reader.Value)); } // Maybe it's an enum if (inst_type.IsEnum) { return(Enum.ToObject(inst_type, reader.Value)); } // Try using an implicit conversion operator MethodInfo conv_op = GetConvOp(inst_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) { AddObjectMetadata(inst_type); ObjectMetadata t_data = object_metadata[inst_type]; reader.Read(); //如果打开自动识别类型,那么就可以支持已经定义的动态类型 var AttrList = inst_type.GetCustomAttributes(typeof(LitJsonObjectAttribute), false); bool NoSelfType = false; foreach (var attr in AttrList) { var jsonObj = attr as LitJsonObjectAttribute; if (null != jsonObj && jsonObj.NoSelfType == true) { NoSelfType = true; } } string keyname = (string)reader.Value; if (NoSelfType == false && keyname == "selfType") //第一个字段为selftype 可以多态反射 { string classname = (string)ReadValue(typeof(string), reader); Type classtype = FindTypeByName(classname); if (null != classtype) { inst_type = classtype; AddObjectMetadata(inst_type); t_data = object_metadata[inst_type]; } reader.Read(); } if (inst_type.IsAbstract) { while (true) { if (reader.Token == JsonToken.ObjectEnd) { instance = null; break; } ReadSkip(reader); reader.Read(); } } else if (inst_type == typeof(object))//如果期望的类型为object那么会自动转换成JsonData { WrapperFactory factory = delegate { return(new JsonData()); }; instance = factory(); while (true) { if (reader.Token == JsonToken.ObjectEnd) { break; } string property = (string)reader.Value; ((IDictionary)instance)[property] = ReadValue(factory, reader); reader.Read(); } } else //按类的格式赋值 { if (reader.Token != JsonToken.ObjectEnd) { instance = Activator.CreateInstance(inst_type); } else { instance = null; } while (true) { 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); reader.Read(); continue; } } ((IDictionary)instance).Add( property, ReadValue( t_data.ElementType, reader)); } reader.Read(); } } } return(instance); }