/// <summary> /// Gets the serializable members for the type. /// </summary> /// <param name="objectType">The type to get serializable members for.</param> /// <returns>The serializable members for the type.</returns> protected virtual List <MemberInfo> GetSerializableMembers(Type objectType) { #if !PocketPC && !NET20 DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(objectType); #endif List <MemberInfo> defaultMembers = ReflectionUtils.GetFieldsAndProperties(objectType, DefaultMembersSearchFlags) .Where(m => !ReflectionUtils.IsIndexedProperty(m)).ToList(); List <MemberInfo> allMembers = ReflectionUtils.GetFieldsAndProperties(objectType, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static) .Where(m => !ReflectionUtils.IsIndexedProperty(m)).ToList(); List <MemberInfo> serializableMembers = new List <MemberInfo>(); foreach (MemberInfo member in allMembers) { // exclude members that are compiler generated if set if (SerializeCompilerGeneratedMembers || !member.IsDefined(typeof(CompilerGeneratedAttribute), true)) { if (defaultMembers.Contains(member)) { // add all members that are found by default member search serializableMembers.Add(member); } else { // add members that are explicitly marked with JsonProperty/DataMember attribute if (JsonTypeReflector.GetAttribute <JsonPropertyAttribute>(member) != null) { serializableMembers.Add(member); } #if !PocketPC && !NET20 else if (dataContractAttribute != null && JsonTypeReflector.GetAttribute <DataMemberAttribute>(member) != null) { serializableMembers.Add(member); } #endif } } } #if !PocketPC && !SILVERLIGHT && !NET20 Type match; // don't include EntityKey on entities objects... this is a bit hacky if (objectType.AssignableToTypeName("System.Data.Objects.DataClasses.EntityObject", out match)) { serializableMembers = serializableMembers.Where(ShouldSerializeEntityMember).ToList(); } #endif return(serializableMembers); }
private void InitializeContract(JsonContract contract) { JsonContainerAttribute containerAttribute = JsonTypeReflector.GetJsonContainerAttribute(contract.NonNullableUnderlyingType); if (containerAttribute != null) { contract.IsReference = containerAttribute._isReference; } #if !PocketPC && !NET20 else { DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(contract.NonNullableUnderlyingType); // doesn't have a null value if (dataContractAttribute != null && dataContractAttribute.IsReference) { contract.IsReference = true; } } #endif contract.Converter = ResolveContractConverter(contract.NonNullableUnderlyingType); // then see whether object is compadible with any of the built in converters contract.InternalConverter = JsonSerializer.GetMatchingConverter(BuiltInConverters, contract.NonNullableUnderlyingType); if (ReflectionUtils.HasDefaultConstructor(contract.CreatedType, true) || contract.CreatedType.IsValueType) { contract.DefaultCreator = GetDefaultCreator(contract.CreatedType); contract.DefaultCreatorNonPublic = (!contract.CreatedType.IsValueType && ReflectionUtils.GetDefaultConstructor(contract.CreatedType) == null); } ResolveCallbackMethods(contract, contract.NonNullableUnderlyingType); }
private void SetPropertySettingsFromAttributes(JsonProperty property, ICustomAttributeProvider attributeProvider, string name, Type declaringType, MemberSerialization memberSerialization, out bool allowNonPublicAccess, out bool hasExplicitAttribute) { hasExplicitAttribute = false; #if !PocketPC && !NET20 DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(declaringType); DataMemberAttribute dataMemberAttribute; if (dataContractAttribute != null && attributeProvider is MemberInfo) { dataMemberAttribute = JsonTypeReflector.GetDataMemberAttribute((MemberInfo)attributeProvider); } else { dataMemberAttribute = null; } #endif JsonPropertyAttribute propertyAttribute = JsonTypeReflector.GetAttribute <JsonPropertyAttribute>(attributeProvider); if (propertyAttribute != null) { hasExplicitAttribute = true; } bool hasIgnoreAttribute = (JsonTypeReflector.GetAttribute <JsonIgnoreAttribute>(attributeProvider) != null); string mappedName; if (propertyAttribute != null && propertyAttribute.PropertyName != null) { mappedName = propertyAttribute.PropertyName; } #if !PocketPC && !NET20 else if (dataMemberAttribute != null && dataMemberAttribute.Name != null) { mappedName = dataMemberAttribute.Name; } #endif else { mappedName = name; } property.PropertyName = ResolvePropertyName(mappedName); property.UnderlyingName = name; if (propertyAttribute != null) { property.Required = propertyAttribute.Required; property.Order = propertyAttribute._order; } #if !PocketPC && !NET20 else if (dataMemberAttribute != null) { property.Required = (dataMemberAttribute.IsRequired) ? Required.AllowNull : Required.Default; property.Order = (dataMemberAttribute.Order != -1) ? (int?)dataMemberAttribute.Order : null; } #endif else { property.Required = Required.Default; } property.Ignored = (hasIgnoreAttribute || (memberSerialization == MemberSerialization.OptIn && propertyAttribute == null #if !PocketPC && !NET20 && dataMemberAttribute == null #endif )); // resolve converter for property // the class type might have a converter but the property converter takes presidence property.Converter = JsonTypeReflector.GetJsonConverter(attributeProvider, property.PropertyType); property.MemberConverter = JsonTypeReflector.GetJsonConverter(attributeProvider, property.PropertyType); DefaultValueAttribute defaultValueAttribute = JsonTypeReflector.GetAttribute <DefaultValueAttribute>(attributeProvider); property.DefaultValue = (defaultValueAttribute != null) ? defaultValueAttribute.Value : null; property.NullValueHandling = (propertyAttribute != null) ? propertyAttribute._nullValueHandling : null; property.DefaultValueHandling = (propertyAttribute != null) ? propertyAttribute._defaultValueHandling : null; property.ReferenceLoopHandling = (propertyAttribute != null) ? propertyAttribute._referenceLoopHandling : null; property.ObjectCreationHandling = (propertyAttribute != null) ? propertyAttribute._objectCreationHandling : null; property.TypeNameHandling = (propertyAttribute != null) ? propertyAttribute._typeNameHandling : null; property.IsReference = (propertyAttribute != null) ? propertyAttribute._isReference : null; allowNonPublicAccess = false; if ((DefaultMembersSearchFlags & BindingFlags.NonPublic) == BindingFlags.NonPublic) { allowNonPublicAccess = true; } if (propertyAttribute != null) { allowNonPublicAccess = true; } #if !PocketPC && !NET20 if (dataMemberAttribute != null) { allowNonPublicAccess = true; hasExplicitAttribute = true; } #endif }