internal Dictionary <string, Enum> GetEnumValues(Type type, IReflectionController controller) { if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>)) { type = type.GetGenericArguments()[0]; } var ns = Enum.GetNames(type); var vs = Enum.GetValues(type); var vm = new Dictionary <string, Enum> (ns.Length); var vc = _EnumValueCache; var n = controller.GetEnumValueFormat(type); var s = n != EnumValueFormat.Numeric ? NamingStrategy.GetStrategy((NamingConvention)n) : null; for (int i = ns.Length - 1; i >= 0; i--) { var en = ns[i]; var ev = (Enum)vs.GetValue(i); var m = type.GetMember(en)[0]; en = s != null?s.Rename(en) : null; var sn = controller.GetEnumValueName(m); if (String.IsNullOrEmpty(sn) == false) { en = sn; } vc[ev] = en; vm.Add(en ?? ns[i], ev); } return(vm); }
Dictionary <string, JsonMemberSetter> LoadMemberDeserializationSettings(JsonMemberSetter d, IReflectionController controller) { var member = d.Member.MemberInfo; var sd = new Dictionary <string, JsonMemberSetter> (); d.Converter = controller.GetMemberConverter(member); d.ItemConverter = controller.GetMemberItemConverter(member); var tn = controller.GetSerializedNames(member); if (tn == null) { sd.Add(d.MemberName, d); return(sd); } sd.Add(String.IsNullOrEmpty(tn.DefaultName) ? d.MemberName : tn.DefaultName, d); // polymorphic deserialization foreach (var item in tn) { var st = item.Key; var sn = item.Value; var dt = new JsonMemberSetter(d.OwnerTypeInfo, new MemberCache(st, d.MemberName, d.Member), this) { Converter = d.Converter, ItemConverter = d.ItemConverter }; AddSetter(sd, sn, dt); } return(sd); }
Dictionary <string, JsonMemberSetter> GetDeserializingProperties(JsonMemberSetter setter, IReflectionController controller) { var member = setter.Member.MemberInfo; //if (controller == null) { // return new Dictionary<string, JsonMemberSetter> { { setter.MemberName, setter } }; //} setter.CanWrite = controller.IsMemberDeserializable(member, setter.Member); if (setter.CanWrite == false) { if (/*d.TypeInfo.Reflection.AppendItem == null || */ member is PropertyInfo == false) { return(null); } } return(LoadMemberDeserializationSettings(setter, controller)); }
Dictionary <string, JsonMemberSetter> GetSetters(SerializationInfo typeInfo, MemberCache[] members, IReflectionController controller) { var sd = new Dictionary <string, JsonMemberSetter> (StringComparer.OrdinalIgnoreCase); foreach (var p in members) { var s = new JsonMemberSetter(typeInfo, p, this); if (s.Member.IsReadOnly && s.OwnerTypeInfo.Reflection.AppendItem != null && CanSerializePrivateMembers == false) { continue; } var dp = GetDeserializingProperties(s, controller); if (dp == null) { continue; } foreach (var item in dp) { AddSetter(sd, item.Key, item.Value); } } return(sd); }
JsonMemberGetter[] GetGetters(SerializationInfo typeInfo, MemberCache[] members, IReflectionController controller) { var r = new List <JsonMemberGetter> (members.Length); foreach (var m in members) { var g = new JsonMemberGetter(typeInfo, m, this); var mi = m.MemberInfo; g.Serializable = Constants.ToTriState(controller.IsMemberSerializable(mi, m)); if (g.Member.HasPublicGetter == false && CanSerializePrivateMembers == false) { continue; } g.Converter = controller.GetMemberConverter(mi); g.ItemConverter = controller.GetMemberItemConverter(mi); var dv = controller.GetNonSerializedValues(mi); if (dv != null) { var v = new List <object> (); foreach (var item in dv) { v.Add(item); } g.NonSerializedValues = v.ToArray(); } g.HasNonSerializedValue = g.NonSerializedValues != null; var tn = controller.GetSerializedNames(mi); if (tn != null) { if (String.IsNullOrEmpty(tn.DefaultName) == false && tn.DefaultName != g.SerializedName) { g.SpecificName = true; } g.SerializedName = tn.DefaultName ?? g.SerializedName; if (tn.Count > 0) { g.TypedNames = new Dictionary <Type, string> (tn); g.SpecificName = true; } } r.Add(g); } return(r.ToArray()); }
/// <summary> /// Creates a new instance of <see cref="SerializationManager"/> with a specific <see cref="IReflectionController"/>. /// </summary> /// <param name="controller">The controller to control object reflections before serialization.</param> public SerializationManager(IReflectionController controller) { _controller = controller ?? new JsonReflectionController(); }
/// <summary> /// Creates a new instance of <see cref="SerializationManager"/> with a specific <see cref="IReflectionController"/>. /// </summary> /// <param name="controller">The controller to control object reflections before serialization.</param> public SerializationManager(IReflectionController controller) { _controller = controller; }
static Dictionary <string, JsonMemberSetter> GetDeserializingProperties(JsonMemberSetter d, IReflectionController controller) { var member = d.Member.MemberInfo; if (controller == null) { return(new Dictionary <string, JsonMemberSetter> () { { d.MemberName, d } }); } if (controller.IsMemberDeserializable(member, d.Member) == false) { d.CanWrite = false; if (d.Member.MemberTypeReflection.AppendItem == null || member is PropertyInfo == false) { return(null); } } return(LoadMemberDeserializationSettings(d, controller)); }
static Dictionary <string, JsonMemberSetter> GetSetters(Type type, MemberCache[] members, IReflectionController controller) { var sd = new Dictionary <string, JsonMemberSetter> (StringComparer.OrdinalIgnoreCase); foreach (var p in members) { var d = new JsonMemberSetter(p); var dp = GetDeserializingProperties(d, controller); if (dp == null) { continue; } foreach (var item in dp) { AddPropertyInfo(sd, item.Key, item.Value); } } return(sd); }
/// <summary> /// Creates a new instance of <see cref="SerializationManager"/>. /// </summary> /// <remarks>The <see cref="ReflectionController"/> will be initialized to a new instance of <see cref="JsonReflectionController"/>.</remarks> public SerializationManager() { _controller = new JsonReflectionController(); }
internal static JsonMemberGetter[] GetGetters(Type type, MemberCache[] members, IReflectionController controller) { var r = new JsonMemberGetter[members.Length]; for (int i = r.Length - 1; i >= 0; i--) { var m = members[i]; var g = r[i] = new JsonMemberGetter(m); if (controller == null) { continue; } var mi = m.MemberInfo; g.Serializable = Constants.ToTriState(controller.IsMemberSerializable(mi, m)); g.Converter = controller.GetMemberConverter(mi); g.ItemConverter = controller.GetMemberItemConverter(mi); var dv = controller.GetNonSerializedValues(mi); if (dv != null) { var v = new List <object> (); foreach (var item in dv) { v.Add(item); } g.NonSerializedValues = v.ToArray(); } g.HasNonSerializedValue = g.NonSerializedValues != null; var tn = controller.GetSerializedNames(mi); if (tn != null) { if (String.IsNullOrEmpty(tn.DefaultName) == false && tn.DefaultName != g.SerializedName) { g.SpecificName = true; } g.SerializedName = tn.DefaultName ?? g.SerializedName; if (tn.Count > 0) { g.TypedNames = new Dictionary <Type, string> (tn); g.SpecificName = true; } } } return(r); }