public ObjectSubclassInfo(Type type, ConstructorInfo constructor) { TypeInfo = type.GetTypeInfo(); ClassName = GetClassName(TypeInfo); Constructor = constructor; PropertyMappings = type.GetProperties() .Select(prop => Tuple.Create(prop, prop.GetCustomAttribute<ParseFieldNameAttribute>(true))) .Where(t => t.Item2 != null) .Select(t => Tuple.Create(t.Item1, t.Item2.FieldName)) .ToDictionary(t => t.Item1.Name, t => t.Item2); }
/** Creates a member map for the type */ private Dictionary<string, MemberInfo> CreateMemberMap(Type objectType) { Dictionary<string, MemberInfo> memberMap; if (this.MemberMapCache.TryGetValue(objectType, out memberMap)) { // map was stored in cache return memberMap; } // create a new map memberMap = new Dictionary<string, MemberInfo>(); // load properties into property map PropertyInfo[] properties = objectType.GetProperties( BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance ); foreach (PropertyInfo info in properties) { if (!info.CanRead || !info.CanWrite) { continue; } if (JsonIgnoreAttribute.IsJsonIgnore(info)) { continue; } string jsonName = JsonNameAttribute.GetJsonName(info); if (String.IsNullOrEmpty(jsonName)) { memberMap[info.Name] = info; } else { memberMap[jsonName] = info; } } // load public fields into property map FieldInfo[] fields = objectType.GetFields( BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance ); foreach (FieldInfo info in fields) { if (!info.IsPublic && info.GetCustomAttributes(typeof(JsonMemberAttribute),true).Length == 0) { continue; } if (JsonIgnoreAttribute.IsJsonIgnore(info)) { continue; } string jsonName = JsonNameAttribute.GetJsonName(info); if (String.IsNullOrEmpty(jsonName)) { memberMap[info.Name] = info; } else { memberMap[jsonName] = info; } } // store in cache for repeated usage this.MemberMapCache[objectType] = memberMap; return memberMap; }
protected virtual void WriteObject(object value, Type type, bool serializePrivate) { bool appendDelim = false; if (settings.HandleCyclicReferences && !type.IsValueType) { int prevIndex = 0; if (this.previouslySerializedObjects.TryGetValue (value, out prevIndex)) { this.Writer.Write(JsonReader.OperatorObjectStart); this.WriteObjectProperty("@ref", prevIndex); this.WriteLine(); this.Writer.Write(JsonReader.OperatorObjectEnd); return; } else { this.previouslySerializedObjects.Add (value, this.previouslySerializedObjects.Count); } } this.Writer.Write(JsonReader.OperatorObjectStart); this.depth++; if (this.depth > this.settings.MaxDepth) { throw new JsonSerializationException(String.Format(JsonWriter.ErrorMaxDepth, this.settings.MaxDepth)); } try { if (!String.IsNullOrEmpty(this.settings.TypeHintName)) { if (appendDelim) { this.WriteObjectPropertyDelim(); } else { appendDelim = true; } this.WriteObjectProperty(this.settings.TypeHintName, type.FullName+", "+type.Assembly.GetName().Name); } bool anonymousType = type.IsGenericType && (type.Name.StartsWith(JsonWriter.AnonymousTypePrefix) || type.Name.StartsWith(JsonWriter.AnonymousTypePrefix2)); // serialize public properties PropertyInfo[] properties = type.GetProperties(); foreach (PropertyInfo property in properties) { if (!property.CanRead) { if (Settings.DebugMode) Console.WriteLine ("Cannot serialize "+property.Name+" : cannot read"); continue; } if (!property.CanWrite && !anonymousType) { if (Settings.DebugMode) Console.WriteLine ("Cannot serialize "+property.Name+" : cannot write"); continue; } if (this.IsIgnored(type, property, value)) { if (Settings.DebugMode) Console.WriteLine ("Cannot serialize "+property.Name+" : is ignored by settings"); continue; } if (property.GetIndexParameters ().Length != 0) { if (Settings.DebugMode) Console.WriteLine ("Cannot serialize "+property.Name+" : is indexed"); continue; } object propertyValue = property.GetValue(value, null); if (this.IsDefaultValue(property, propertyValue)) { if (Settings.DebugMode) Console.WriteLine ("Cannot serialize "+property.Name+" : is default value"); continue; } if (appendDelim) this.WriteObjectPropertyDelim(); else appendDelim = true; // use Attributes here to control naming string propertyName = JsonNameAttribute.GetJsonName(property); if (String.IsNullOrEmpty(propertyName)) propertyName = property.Name; this.WriteObjectProperty(propertyName, propertyValue); } // serialize public fields FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); foreach (FieldInfo field in fields) { if (field.IsStatic || (!field.IsPublic && field.GetCustomAttributes(typeof(JsonMemberAttribute),true).Length == 0)) { if (Settings.DebugMode) Console.WriteLine ("Cannot serialize "+field.Name+" : not public or is static (and does not have a JsonMember attribute)"); continue; } if (this.IsIgnored(type, field, value)) { if (Settings.DebugMode) Console.WriteLine ("Cannot serialize "+field.Name+" : ignored by settings"); continue; } object fieldValue = field.GetValue(value); if (this.IsDefaultValue(field, fieldValue)) { if (Settings.DebugMode) Console.WriteLine ("Cannot serialize "+field.Name+" : is default value"); continue; } if (appendDelim) { this.WriteObjectPropertyDelim(); this.WriteLine(); } else { appendDelim = true; } // use Attributes here to control naming string fieldName = JsonNameAttribute.GetJsonName(field); if (String.IsNullOrEmpty(fieldName)) fieldName = field.Name; this.WriteObjectProperty(fieldName, fieldValue); } } finally { this.depth--; } if (appendDelim) this.WriteLine(); this.Writer.Write(JsonReader.OperatorObjectEnd); }