private void SerializeObject(object obj) { var lazyMgr = obj.As <ILazyManager>(); var flag = new AssertFlag(); var type = obj.GetType(); jsonWriter.WriteStartObject(); foreach (var acc in context.GetAccessorCache(type)) { if (acc.Filter(acc.PropertyInfo, lazyMgr)) { continue; } var value = acc.Accessor.GetValue(obj); if (option.IgnoreNull && value == null) { continue; } if (!flag.AssertTrue()) { jsonWriter.WriteComma(); } jsonWriter.WriteKey(SerializeName(acc.PropertyName)); JsonConvertContext.Current.Assign(acc.Accessor.PropertyInfo.Name, value, () => Serialize(value)); } jsonWriter.WriteEndObject(); }
private void SerializeObject(object obj, bool startEle) { var lazyMgr = obj.As <ILazyManager>(); var flag = new AssertFlag(); var type = obj.GetType(); if (startEle) { xmlWriter.WriteStartElement(GetElementName(type)); } var cache = context.GetAccessorCache(type); foreach (var acc in cache) { if (acc.Filter(acc.PropertyInfo, lazyMgr)) { continue; } var value = acc.Accessor.GetValue(obj); if (value == null) { continue; } context.SerializeInfo = new PropertySerialzeInfo(acc); var objType = acc.PropertyInfo.PropertyType == typeof(object) ? value.GetType() : acc.PropertyInfo.PropertyType; if (option.OutputStyle == OutputStyle.Attribute && objType.IsStringable()) { if (acc.Converter != null) { xmlWriter.WriteAttributeString(acc.PropertyName, acc.Converter.WriteObject(serializer, value)); } else { xmlWriter.WriteAttributeString(acc.PropertyName, value.ToString()); } } else { if (acc.Converter != null && acc.Converter is XmlConverter converter) { WriteXmlElement(acc.PropertyName, true, () => converter.WriteXml(serializer, xmlWriter, value)); } else { WriteXmlElement(acc.PropertyName, true, () => Serialize(value, type: objType)); } } context.SerializeInfo = null; } if (startEle) { xmlWriter.WriteEndElement(); } }
/// <summary> /// 使用标注的缺省值创建一个新对象。 /// </summary> /// <param name="objType"></param> /// <returns></returns> protected object CreateGeneralObject(Type objType) { var obj = objType.New(); foreach (var acc in context.GetAccessorCache(objType).Where(s => s.DefaultValue != null)) { acc.Accessor.SetValue(obj, acc.DefaultValue.ToType(acc.PropertyInfo.PropertyType)); } return(obj); }
private void SerializeObject(object obj) { var lazyMgr = obj.As <ILazyManager>(); var flag = new AssertFlag(); var type = obj.GetType(); jsonWriter.WriteStartObject(); foreach (var acc in context.GetAccessorCache(type)) { if (acc.Filter(acc.PropertyInfo, lazyMgr)) { continue; } var value = acc.Accessor.GetValue(obj); if (option.IgnoreNull && value == null) { continue; } if (!flag.AssertTrue()) { jsonWriter.WriteComma(); } jsonWriter.WriteKey(SerializeName(acc.PropertyName)); context.SerializeInfo = new PropertySerialzeInfo(acc); if (value == null) { jsonWriter.WriteNull(); } else { //如果在属性上指定了 JsonConverter if (acc.Converter is JsonConverter converter) { converter.WriteJson(serializer, jsonWriter, value); } else { var objType = acc.PropertyInfo.PropertyType == typeof(object) ? value.GetType() : acc.PropertyInfo.PropertyType; Serialize(value, objType); } } context.SerializeInfo = null; } jsonWriter.WriteEndObject(); }