protected override IList <JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization) { var list = base.CreateProperties(type, memberSerialization); if (typeof(Page).IsAssignableFrom(type)) { return(list); } if (type.GetCustomAttributes(typeof(JsonRequireAttribute), true).Length > 0) { return(list); } if (PropertyNames != null) { JsonPropertyCollection jsonProperties = new JsonPropertyCollection(type); foreach (var item in list) { if (PropertyNames.ContainsIgnoreCase(item.PropertyName) == false) { jsonProperties.Add(item); } } return(jsonProperties); } return(list); }
/// <summary> /// Creates properties for the given <see cref="JsonContract"/>. /// </summary> /// <param name="type">The type to create properties for.</param> /// /// <param name="memberSerialization">The member serialization mode for the type.</param> /// <returns>Properties for the given <see cref="JsonContract"/>.</returns> protected override IList <JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization) { var members = GetSerializableMembers(type); if (members == null) { throw new JsonSerializationException("Null collection of seralizable members returned."); } var properties = new JsonPropertyCollection(type); var modelConfiguration = _breezeConfigurator.GetModelConfiguration(type); foreach (var member in members) { var property = CreateProperty(type, member, memberSerialization, modelConfiguration); if (property != null) { var nameTable = DefaultContractResolverStateNameTableField.GetValue(this); // nametable is not thread-safe for multiple writers lock (nameTable) { property.PropertyName = (string)PropertyNameTableAddMethod .Invoke(nameTable, new object[] { property.PropertyName }); } properties.AddProperty(property); } } var syntheticPoperties = _getMetadataFunc(type).GetSyntheticProperties(); if (syntheticPoperties != null && syntheticPoperties.Any()) { foreach (var syntheticProp in syntheticPoperties) { var propertyName = ResolvePropertyName(syntheticProp.Name); // Skip manually added syntetic properties if (!properties.Any(x => x.PropertyName == propertyName)) { properties.Add(new JsonProperty { Readable = true, Writable = true, PropertyName = propertyName, PropertyType = syntheticProp.PkType.ReturnedClass, ValueProvider = new NHSyntheticPropertyValueProvider(syntheticProp) }); } } } IList <JsonProperty> orderedProperties = properties.OrderBy(p => p.Order ?? -1).ToList(); ApplySerializationRules(type, orderedProperties, memberSerialization); return(orderedProperties); }
protected override IList <JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization) { IList <JsonProperty> properties = base.CreateProperties(type, memberSerialization); JsonPropertyCollection c = new JsonPropertyCollection(type); foreach (JsonProperty property in properties.Where(m => m.PropertyName != "Root")) { c.Add(property); } return(c); }
internal static bool ResolveEntityProperties(IList <JsonProperty> jsonProperties, Type entityType, EntityTypeInfo entityTypeInfo, EntityService entityService, DefaultContractResolver resolver, Func <PropertyInfo, JsonProperty> createPropertyFunc) { if (entityService.ContainsEntityType(entityType)) { if (jsonProperties.Any(jp => jp.PropertyName == Defaults.MetadataPropertyName && jp.UnderlyingName == Defaults.DummyMetadataPropertyInfo.Name)) { return(false); //if we ever find the metadata property there, assume it has been resolved. } var _properties = new JsonPropertyCollection(entityType); var isComplex = entityType.IsComplex(); var propertiesToIgnore = entityTypeInfo.PropertiesToIgnore.ToArray(); foreach (var jsonProp in jsonProperties) { if (isComplex) { jsonProp.NullValueHandling = NullValueHandling.Include; //we need null values for complex types jsonProp.DefaultValueHandling = DefaultValueHandling.Include; //we need all properties serialized } if (!jsonProp.Ignored && propertiesToIgnore.Any(np => np.Name == jsonProp.UnderlyingName)) { jsonProp.Ignored = true; } _properties.Add(jsonProp); } lock (entityTypeInfo) { //check for complextypes var complexTypedProperties = entityTypeInfo.ComplexTypedProperties; var complexJsonProperties = new List <JsonProperty>(); if (complexTypedProperties?.Count > 0) { //filter to complexproperties var filteredJsonProperties = _properties? .Select(p => new { JsonProperty = p, PropertyInfo = complexTypedProperties.Where(pi => pi.Name == p.UnderlyingName) .FirstOrDefault() }) .Where(np => np.PropertyInfo != null) .ToDictionary(np => np.JsonProperty, np => np.PropertyInfo); Func <Type, IList <JsonProperty> > getResolvedPropertiesFunc = t => { var contract = resolver.ResolveContract(t) as JsonObjectContract; if (contract.Properties?.Count > 0) { ResolveEntityProperties (contract.Properties, t, entityService.GetEntityTypeInfo(t), entityService, resolver, createPropertyFunc); } return(contract.Properties); }; //generate new properties with new names for the complex types foreach (var complexTypedJsonProp in filteredJsonProperties) { //get the complexTypedProperty's own jsonproperties //include derived classes var derivedTypes = entityService .GetDerivedEntityTypes(complexTypedJsonProp.Key.PropertyType)? .Where(t => t.IsComplex()).ToList(); if (derivedTypes == null || derivedTypes.Count == 0) { entityService.AddEntityType(complexTypedJsonProp.Key.PropertyType); derivedTypes = new List <Type> { complexTypedJsonProp.Key.PropertyType }; } var childProperties = derivedTypes .SelectMany(dt => getResolvedPropertiesFunc(dt)?.Where (p => /*!p.Ignored &&*/ p.PropertyName != Defaults.MetadataPropertyName) ?? new JsonProperty[0], (dt, property) => new { DerivedType = dt, Property = property }) .Where(jp => jp.Property != null) .GroupBy(jp => jp.Property.PropertyName) .Select(jpg => jpg.FirstOrDefault()) .ToList(); foreach (var childProp in childProperties) { //add the child to this type's properties try { var newChildProp = GetComplexTypedPropertyChild (childProp.DerivedType, complexTypedJsonProp.Key, complexTypedJsonProp.Value, childProp.Property); _properties.AddProperty(newChildProp); complexJsonProperties.Add(newChildProp); } catch (JsonSerializationException e) { //for some reason member already exists and is duplicate } } //ignore all complex typed properties complexTypedJsonProp.Key.Ignored = true; } } var nextIdx = -1; //clear and re-add these properties jsonProperties.Clear(); var orderedProperties = _properties.OrderBy(p => p.Order ?? nextIdx++); foreach (var prop in orderedProperties) { jsonProperties.Add(prop); } //create metadata property and add it last var metadataJsonProperty = createPropertyFunc(Defaults.DummyMetadataPropertyInfo); metadataJsonProperty.PropertyName = Defaults.MetadataPropertyName; metadataJsonProperty.ValueProvider = new MetadataValueProvider(entityType, complexJsonProperties); metadataJsonProperty.ShouldSerialize = instance => { return(!(metadataJsonProperty.ValueProvider as MetadataValueProvider) .BuildMetadata(instance).IsEmpty()); }; metadataJsonProperty.Order = int.MaxValue; //try to make it the last serialized jsonProperties.Add(metadataJsonProperty); //assign and resolve these properties entityTypeInfo.JsonProperties = new List <JsonProperty>(jsonProperties); return(true); } } return(false); }