/// <summary>
        /// Initializes a new instance of the <see cref="JsonDynamicContract"/> class.
        /// </summary>
        /// <param name="underlyingType">The underlying type for the contract.</param>
        public JsonDynamicContract(Type underlyingType)
            : base(underlyingType)
        {
            ContractType = JsonContractType.Dynamic;

            Properties = new JsonPropertyCollection(UnderlyingType);
        }
    /// <summary>
    /// Initializes a new instance of the <see cref="JsonObjectContract"/> class.
    /// </summary>
    /// <param name="underlyingType">The underlying type for the contract.</param>
    public JsonObjectContract(Type underlyingType)
      : base(underlyingType)
    {
      ContractType = JsonContractType.Object;

      Properties = new JsonPropertyCollection(UnderlyingType);
      ConstructorParameters = new JsonPropertyCollection(UnderlyingType);
    }
Exemplo n.º 3
0
        /// <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(JsonObjectContract contract)
            {
                IList <JsonProperty> properties = base.CreateProperties(contract);

                JsonPropertyCollection c = new JsonPropertyCollection();

                CollectionUtils.AddRange(c, (IEnumerable)properties.Where(m => m.PropertyName != "Root"));

                return(c);
            }
            protected override IList <JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
            {
                IList <JsonProperty> properties = base.CreateProperties(type, memberSerialization);

                JsonPropertyCollection c = new JsonPropertyCollection(type);

                c.AddRange(properties.Where(m => m.PropertyName != "Root"));

                return(c);
            }
Exemplo n.º 6
0
        /// <summary>
        /// Creates a list of <see cref="JsonPathProperty"/> from the <paramref name="properties"/>
        /// that has been attributed with the <see cref="JsonPathAttribute"/>.
        /// </summary>
        /// <param name="properties"></param>
        protected virtual List <JsonPathProperty> GetJsonPathProperties(JsonPropertyCollection properties)
        {
            var jsonPathProperties = from jsonProperty in properties
                                     let attribute = jsonProperty.GetAttribute <JsonPathAttribute>()
                                                     where attribute != null
                                                     select new JsonPathProperty
            {
                JsonProperty = jsonProperty,
                Expression   = attribute.Expression
            };

            return(jsonPathProperties.ToList());
        }
Exemplo n.º 7
0
        protected override IList <JsonProperty> CreateConstructorParameters(ConstructorInfo constructor, JsonPropertyCollection memberProperties)
        {
            var constructorParameters = constructor.GetParameters();

            JsonPropertyCollection parameterCollection = new JsonPropertyCollection(constructor.DeclaringType);

            foreach (ParameterInfo parameterInfo in constructorParameters)
            {
                var matchingMemberProperty = (parameterInfo.Name != null) ? memberProperties.GetClosestMatchProperty(parameterInfo.Name) : null;

                // Constructor type must be assignable from property type.
                // Note that this is the only difference between this method and the method it overrides in DefaultContractResolver.
                // In DefaultContractResolver, the types must match exactly.
                if (matchingMemberProperty != null)
                {
                    var memberType             = matchingMemberProperty.PropertyType;
                    var memberTypeIsGeneric    = memberType.IsGenericType;
                    var memberGenericArguments = memberType.GetGenericArguments();
                    var parameterTypeIsArray   = parameterInfo.ParameterType.IsArray;
                    var parameterElementType   = parameterInfo.ParameterType.GetElementType();
                    if (parameterTypeIsArray &&
                        memberTypeIsGeneric &&
                        memberGenericArguments.Length == 1 &&
                        memberType.IsAssignableTo(typeof(IEnumerable <>).MakeGenericType(parameterElementType)))
                    {
                        // NO-OP - this allows for the constructor parameter to be a "params" array while still using a collection property as the source.
                    }
                    else if (memberType.IsAssignableTo(parameterInfo.ParameterType))
                    {
                        // NO-OP - vanilla assignable type to constructor check.
                    }
                    else
                    {
                        // no way to do this so null out and the let the next step error with a clean message.
                        matchingMemberProperty = null;
                    }
                }

                if (matchingMemberProperty != null || parameterInfo.Name != null)
                {
                    var property = this.CreatePropertyFromConstructorParameterWithConstructorInfo(matchingMemberProperty, parameterInfo, constructor);

                    if (property != null)
                    {
                        parameterCollection.AddProperty(property);
                    }
                }
            }

            return(parameterCollection);
        }
Exemplo n.º 8
0
        //////////////////////////////////////////////////
        ///
        /// <author>Nicholas Paldino</author>
        /// <created>2014-03-21</created>
        /// <summary>Given a <paramref name="type"/>, resolves
        /// the <see cref="JsonContract"/> for that type.</summary>
        /// <param name="type">The <see cref="Type"/> that the contract
        /// is to be resolved to.</param>
        /// <returns>The <see cref="JsonContract"/> that was resolved from
        /// the type.</returns>
        ///
        //////////////////////////////////////////////////
        public JsonContract ResolveContract(Type type)
        {
            // Validate parameters.
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            // Call the decorated instance.
            JsonContract contract = _contractResolver.ResolveContract(type);

            // The properties collection.
            JsonPropertyCollection properties = null;

            // Check for type.
            var objectContract = contract as JsonObjectContract;

            // Assign.
            if (objectContract != null)
            {
                properties = objectContract.Properties;
            }
            else
            {
                // Type sniff.
                var dynamicContract = contract as JsonDynamicContract;

                // Assign.
                if (dynamicContract != null)
                {
                    properties = dynamicContract.Properties;
                }
            }

            // If the properties is null, return the contract.
            if (properties == null)
            {
                return(contract);
            }

            // Cycle through the properties.
            foreach (JsonProperty property in properties)
            {
                // Make writable.
                property.Writable = true;
            }

            // Return the contract.
            return(contract);
        }
Exemplo n.º 9
0
        protected override IList <JsonProperty> CreateProperties(Type type, MemberSerialization serialization)
        {
            var properties = new JsonPropertyCollection(type);

            foreach (JsonProperty property in GetSerializableMembers(type).Select(m => CreateProperty(m, serialization)).WhereNotNull())
            {
                properties.AddProperty(property);
            }
            return(properties
                   .OrderBy(p => p.Order ?? -1)
                   .ThenBy(IsPropertyCollection)
                   .ThenBy(GetPropertyTypeDepth)
                   .ThenBy(p => p.PropertyName)
                   .ToList());
        }
Exemplo n.º 10
0
        /// <inheritdoc />
        protected override IList <JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
        {
            if (typeof(INHibernateProxy).IsAssignableFrom(type))
            {
                return(new List <JsonProperty>()); // They will be copied from the base type
            }

            var members = GetSerializableMembers(type);

            if (members == null)
            {
                throw new JsonSerializationException("Null collection of serializable members returned.");
            }

            var propertyMembers    = new Dictionary <JsonProperty, MemberInfo>(members.Count);
            var modelConfiguration = _breezeConfigurator.GetModelConfiguration(type);
            var metadata           = GetTypeMetadata(type);
            var properties         = new JsonPropertyCollection(type);

            foreach (var member in members)
            {
                var property = CreateProperty(member, memberSerialization, metadata);
                propertyMembers.Add(property, member);
                properties.AddProperty(property);
            }

            foreach (var syntheticMember in modelConfiguration.SyntheticMembers.Values.Where(o => o.Added))
            {
                properties.AddProperty(CreateSyntheticProperty(syntheticMember));
            }

            if (metadata != null)
            {
                foreach (var syntheticProperty in metadata.SyntheticForeignKeyProperties.Values)
                {
                    properties.AddProperty(CreateSyntheticForeignKeyProperty(
                                               type,
                                               syntheticProperty,
                                               modelConfiguration.GetSyntheticMember(syntheticProperty.Name),
                                               modelConfiguration.GetMember(syntheticProperty.AssociationPropertyName)));
                }
            }

            ApplyModelConfiguration(type, properties, modelConfiguration, metadata, propertyMembers);

            return(properties.OrderBy(p => p.Order ?? -1).ToList());
        }
Exemplo n.º 11
0
        protected override IList <JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
        {
            if (!type.Implements <IDwarf>())
            {
                return(base.CreateProperties(type, memberSerialization));
            }

            var members = GetSerializableMembers(type);

            if (members == null)
            {
                throw new JsonSerializationException("Null collection of seralizable members returned.");
            }

            var properties = new JsonPropertyCollection(type);

            foreach (var member in members)
            {
                var property = CreateProperty(member, memberSerialization);

                if (member.GetCustomAttributes <IgnoreDataMemberAttribute>().Any())
                {
                    continue;
                }

                if (member.GetCustomAttributes <JsonIdOnlyAttribute>().Any())
                {
                    property.PropertyName += "Id";
                    property.Converter     = jsonIdOnlyConverter;
                }

                if (property != null)
                {
                    properties.AddProperty(property);
                }
            }

            var orderedProperties = properties.OrderBy(p => p.Order ?? -1).ToList();

            orderedProperties.Move(orderedProperties.FirstOrDefault(x => x.PropertyName == "id"), 0);
            orderedProperties.Move(orderedProperties.FirstOrDefault(x => x.PropertyName == "isSaved"), 1);

            return(orderedProperties);
        }
Exemplo n.º 12
0
        private Type ResolveImplType(Type objectType, JObject givenJObject, JsonSerializer serializer)
        {
            Type targetType = ResolveImplTypeUsingTypeResolverMethod(objectType, givenJObject, serializer);

            if (targetType != null)
            {
                return(targetType);
            }

            IEnumerable <Type> knownImplTypes =
                System.Attribute.GetCustomAttributes(objectType)   // Select all custom attribute
                .Where(ca => ca is KnownTypeAttribute)             // filter which are KnownTypeAttribute
                .Select(kta => (kta as KnownTypeAttribute).Type);  // return type of Attribute as type of KnownTypeAttribute

            var jsonKeysInJsonString         = givenJObject.Select <KeyValuePair <string, JToken>, string>(kv => kv.Key).ToList();
            var candidateJsonPropertiesUnion = new JsonPropertyCollection(objectType).Select(p => p.PropertyName);

            foreach (var candidateImplType in knownImplTypes)
            {
                var jsonContract = serializer.ContractResolver.ResolveContract(candidateImplType) as JsonObjectContract;
                candidateJsonPropertiesUnion = candidateJsonPropertiesUnion.Union(jsonContract.Properties.Select(p => p.PropertyName));
            }

            var relevantJsonKeysInJson = jsonKeysInJsonString.Intersect(candidateJsonPropertiesUnion);

            foreach (var candidateImplType in knownImplTypes)
            {
                var jsonContract        = serializer.ContractResolver.ResolveContract(candidateImplType) as JsonObjectContract;
                var jsonPropsInType     = jsonContract.Properties;
                var jsonPropNamesInType = jsonPropsInType.Select(p => p.PropertyName);
                var missingProperties   = relevantJsonKeysInJson.Except(jsonPropNamesInType);
                if (missingProperties.FirstOrDefault() == null)
                {
                    return(candidateImplType);
                }
            }

            throw new FabricSerializationException(
                      string.Format("KnownTypeJsonConverter(): None of the known types: {0} has all the properties contained in given json string. Properties in json string: {1}.",
                                    string.Join(",", knownImplTypes.Select(t => t.Name)), // comma separated names of known types.
                                    string.Join(",", jsonKeysInJsonString)));             // comma separated keys in json object
        }
Exemplo n.º 13
0
        private void ApplyModelConfiguration(
            Type type,
            JsonPropertyCollection properties,
            ModelConfiguration modelConfiguration,
            ModelMetadata metadata,
            Dictionary <JsonProperty, MemberInfo> propertyMembers)
        {
            foreach (var property in properties)
            {
                var memberConfiguration = modelConfiguration.GetMember(property.UnderlyingName);
                if (memberConfiguration != null)
                {
                    ConfigureProperty(property, memberConfiguration);
                }

                var isMappedProperty       = metadata?.AllProperties.Contains(property.UnderlyingName);
                var syntheticConfiguration = modelConfiguration.GetSyntheticMember(property.UnderlyingName);
                if (syntheticConfiguration == null &&
                    isMappedProperty == false &&
                    memberConfiguration?.Serialize != true &&
                    memberConfiguration?.Deserialize != true)
                {
                    // Do not serialize a non mapped entity property by default (we do not want to expose data that the client will not use)
                    property.Ignored = memberConfiguration?.Ignored ?? true;
                }

                if (syntheticConfiguration?.SerializeFunction != null ||
                    memberConfiguration?.SerializeFunction != null ||
                    propertyMembers.TryGetValue(property, out var member) && member.IsProperty() && !member.IsAutoProperty(true))
                {
                    // Throw when a non auto or synthetic property lazy loads an uninitialized association (e.g. CustomerId => Organization.Customer.Id)
                    property.ValueProvider = CreateLazyLoadGuard(property.ValueProvider, type, property.UnderlyingName);
                }

                if (isMappedProperty == true)
                {
                    property.Writable = memberConfiguration?.Deserialize ?? true; // Non public mapped property setter shall be writable by default
                }
            }
        }
        protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
        {
            List<MemberInfo> members = this.GetSerializableMembers(type);
            if (members == null)
            {
                throw new JsonSerializationException("Null collection of seralizable members returned.");
            }

            JsonPropertyCollection properties = new JsonPropertyCollection(type);

            foreach (MemberInfo member in members)
            {
                JsonProperty property = this.CreateProperty(member, memberSerialization);

                if (property != null)
                {
                    properties.AddProperty(property);
                }
            }

            // IList<JsonProperty> orderedProperties = properties.OrderBy(p => p.Order ?? -1).ToList();
            return properties;
        }
        protected override IList <JsonProperty> CreateConstructorParameters(
            ConstructorInfo constructor,
            JsonPropertyCollection memberProperties)
        {
            var constructorParameters = constructor.GetParameters();

            var result = new JsonPropertyCollection(constructor.DeclaringType);

            foreach (var parameterInfo in constructorParameters)
            {
                var matchingMemberProperty = (parameterInfo.Name != null) ? memberProperties.GetClosestMatchProperty(parameterInfo.Name) : null;

                // Constructor type must be assignable from property type.
                // Note that this is the only difference between this method and the method it overrides in DefaultContractResolver.
                // In DefaultContractResolver, the types must match exactly.
                if (matchingMemberProperty != null)
                {
                    var memberType             = matchingMemberProperty.PropertyType;
                    var memberTypeIsGeneric    = memberType.IsGenericType;
                    var memberGenericArguments = memberType.GenericTypeArguments;
                    var parameterTypeIsArray   = parameterInfo.ParameterType.IsArray;
                    var parameterElementType   = parameterInfo.ParameterType.GetElementType();
                    if (parameterTypeIsArray &&
                        memberTypeIsGeneric &&
                        memberGenericArguments.Length == 1 &&
                        memberType.IsAssignableTo(typeof(IEnumerable <>).MakeGenericType(parameterElementType)))
                    {
                        // NO-OP
                        // this allows for the constructor parameter to be a "params" array while still using a collection property as the source.
                    }
                    else if (memberType.IsAssignableTo(parameterInfo.ParameterType))
                    {
                        // NO-OP
                        // The property type and the constructor parameter type are equal.
                        // OR
                        // The property type is assignable to the constructor parameter type.
                        // In this case, the constructor is taking a less derived type and converting it
                        // to a more derived type before assigning to the property
                        // (e.g. constructor takes IEnumerable<string>, but property is an IReadOnlyCollection<string>
                        //       because the constructor calls .ToList() before assigning to the property).
                    }
                    else if (parameterInfo.ParameterType.IsAssignableTo(memberType))
                    {
                        // NO-OP
                        // The constructor parameter type is assignable to the property type.
                        // In this case, the constructor is taking a more derived type and assigning
                        // it to the property that is less derived.
                        // (e.g. constructor takes IReadOnlyCollection<string> and assigns it to a property
                        //       of type IEnumerable<string>).
                    }
                    else
                    {
                        // no way to do this so null out and the let the next step error with a clean message.
                        matchingMemberProperty = null;
                    }
                }

                if (matchingMemberProperty != null || parameterInfo.Name != null)
                {
                    var property = this.CreatePropertyFromConstructorParameterWithConstructorInfo(matchingMemberProperty, parameterInfo, constructor);

                    if (property != null)
                    {
                        result.AddProperty(property);
                    }
                }
            }

            return(result);
        }
Exemplo n.º 16
0
        /// <summary>
        /// Creates properties for the given <see cref="JsonObjectContract"/>.
        /// </summary>
        /// <param name="contract">The contract to create properties for.</param>
        /// <returns>Properties for the given <see cref="JsonObjectContract"/>.</returns>
        protected virtual IList<JsonProperty> CreateProperties(JsonObjectContract contract)
        {
            List<MemberInfo> members = GetSerializableMembers(contract.UnderlyingType);
              if (members == null)
            throw new JsonSerializationException("Null collection of seralizable members returned.");

              JsonPropertyCollection properties = new JsonPropertyCollection(contract);

              foreach (MemberInfo member in members)
              {
            JsonProperty property = CreateProperty(contract, member);

            if (property != null)
              properties.AddProperty(property);
              }

              return properties;
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="JsonObjectContract"/> class.
 /// </summary>
 /// <param name="underlyingType">The underlying type for the contract.</param>
 public JsonObjectContract(Type underlyingType)
     : base(underlyingType)
 {
     Properties = new JsonPropertyCollection();
       CreatedType = underlyingType;
 }
 protected override IList<JsonProperty> CreateProperties (Type type, MemberSerialization serialization)
 {
     var properties = new JsonPropertyCollection(type);
     foreach (JsonProperty property in GetSerializableMembers(type).Select(m => CreateProperty(m, serialization)).WhereNotNull())
         properties.AddProperty(property);
     return properties
         .OrderBy(p => p.Order ?? -1)
         .ThenBy(IsPropertyCollection)
         .ThenBy(GetPropertyTypeDepth)
         .ThenBy(p => p.PropertyName)
         .ToList();
 }
Exemplo n.º 19
0
        protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
        {
            var objectType = type.GetBaseObjectType() ?? type;

            if (!_propertyies.ContainsKey(objectType))
            {
                var props = base.CreateProperties(objectType, memberSerialization);

                if (!objectType.IsBaseObject())
                {
                    _propertyies.Add(objectType, props);

                    return _propertyies[objectType];
                }

                if (_config.TypeEntity == objectType)
                {
                    var resProps = new List<JsonProperty>();

                    foreach (var prop in props)
                    {
                        if (prop.PropertyType.IsBaseObject() || prop.PropertyType.IsBaseCollection())
                        {
                            if (_editors.All(x => x.PropertyName != prop.PropertyName)) continue;
                        }

                        resProps.Add(prop);
                    }

                    _propertyies.Add(objectType, resProps);
                }
                else
                {
                    var config = _uiFasade.GetViewModelConfig(objectType);

                    if (config == null) return props;

                    if (objectType.IsDefined(typeof(ComplexTypeAttribute), false)) return props;

                    var editor = _editors.FirstOrDefault(x => x.EditorType == objectType);

                    if (editor == null) return props;

                    if (editor.EditorTemplate == "EasyCollection")
                    {
                        var typeEasyCollection = editor.ViewModelConfig.TypeEntity;

                        if (!_propertyies.ContainsKey(typeEasyCollection))
                        {
                            var properties = new JsonPropertyCollection(typeEasyCollection);

                            properties.AddProperty(CreateProperty(typeEasyCollection.GetProperty("ID"),
                                memberSerialization));

                            if (editor.ViewModelConfig.LookupProperty != "ID")
                                properties.AddProperty(CreateProperty(
                                    typeEasyCollection.GetProperty(editor.ViewModelConfig.LookupProperty), memberSerialization));

                            _propertyies.Add(typeEasyCollection, properties);
                        }
                    }

                    if (editor.Relationship == Relationship.OneToMany) return props;

                    var sysColumns = _uiFasade.GetEditors(objectType)
                        .Where(
                            c =>
                                c.IsSystemPropery &&
                                (c.PropertyName != "RowVersion" && c.PropertyName != "Hidden" &&
                                 c.PropertyName != "SortOrder")).ToList();

                    props =
                        props.Where(
                            p =>
                                p.PropertyName == config.LookupProperty ||
                                sysColumns.Any(x => x.PropertyName == p.PropertyName && x.IsSystemPropery)).ToList();

                    _propertyies.Add(objectType, props);
                }
            }

            return _propertyies[objectType];
        }
        protected override IList <JsonProperty> CreateConstructorParameters(System.Reflection.ConstructorInfo constructor, JsonPropertyCollection memberProperties)
        {
            var parameters = base.CreateConstructorParameters(constructor, memberProperties);

            foreach (var jsonProperty in parameters)
            {
                //set obfuscated name
                jsonProperty.PropertyName = GetObfuscatedName(parameters.IndexOf(jsonProperty));
            }
            return(parameters);
        }
        private static Expression <ObjectConstructor <object> > CreateFactory(Type type, JsonPropertyCollection properties, List <Func <ModelObjectSerializationContext, object> > injectors, ParameterExpression context)
        {
            var arrayArg    = Expression.Parameter(typeof(object[]));
            var constructor = type.GetConstructor(properties.Select(p => p.PropertyType).ToArray());
            var arguments   = properties.Select(CreateArgumentExpression);

            return(Expression.Lambda <ObjectConstructor <object> >(
                       Expression.New(constructor, arguments),
                       arrayArg));

            UnaryExpression CreateArgumentExpression(JsonProperty property, int i) =>
            Expression.Convert(
                injectors[i] != null ?
                (Expression)Expression.Invoke(Expression.Constant(injectors[i]), context) :
                Expression.ArrayIndex(arrayArg, Expression.Constant(i)),
                property.PropertyType);
        }
Exemplo n.º 22
0
 protected virtual IList<JsonProperty> CreateConstructorParameters(ConstructorInfo constructor, JsonPropertyCollection memberProperties)
 {
   ParameterInfo[] parameters = constructor.GetParameters();
   JsonPropertyCollection propertyCollection = new JsonPropertyCollection(constructor.DeclaringType);
   foreach (ParameterInfo parameterInfo in parameters)
   {
     JsonProperty matchingMemberProperty = parameterInfo.Name != null ? memberProperties.GetClosestMatchProperty(parameterInfo.Name) : (JsonProperty) null;
     if (matchingMemberProperty != null && matchingMemberProperty.PropertyType != parameterInfo.ParameterType)
       matchingMemberProperty = (JsonProperty) null;
     JsonProperty constructorParameter = this.CreatePropertyFromConstructorParameter(matchingMemberProperty, parameterInfo);
     if (constructorParameter != null)
       propertyCollection.AddProperty(constructorParameter);
   }
   return (IList<JsonProperty>) propertyCollection;
 }
Exemplo n.º 23
0
        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);
        }
Exemplo n.º 24
0
 protected virtual IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
 {
   List<MemberInfo> serializableMembers = this.GetSerializableMembers(type);
   if (serializableMembers == null)
     throw new JsonSerializationException("Null collection of seralizable members returned.");
   JsonPropertyCollection propertyCollection = new JsonPropertyCollection(type);
   foreach (MemberInfo member in serializableMembers)
   {
     JsonProperty property = this.CreateProperty(member, memberSerialization);
     if (property != null)
       propertyCollection.AddProperty(property);
   }
   return (IList<JsonProperty>) Enumerable.ToList<JsonProperty>((IEnumerable<JsonProperty>) Enumerable.OrderBy<JsonProperty, int>((IEnumerable<JsonProperty>) propertyCollection, (Func<JsonProperty, int>) (p => p.Order ?? -1)));
 }
        protected override IList <JsonProperty> CreateConstructorParameters(ConstructorInfo constructor, JsonPropertyCollection memberProperties)
        {
            var constructorParameters = constructor.GetParameters();

            JsonPropertyCollection parameterCollection = new JsonPropertyCollection(constructor.DeclaringType);

            foreach (ParameterInfo parameterInfo in constructorParameters)
            {
                JsonProperty matchingMemberProperty = (parameterInfo.Name != null) ? memberProperties.GetClosestMatchProperty(parameterInfo.Name) : null;

                // Constructor type must be assignable from property type.
                // Note that this is the only difference between this method and the method it overrides in DefaultContractResolver.
                // In DefaultContractResolver, the types must match exactly.
                if (matchingMemberProperty != null && !parameterInfo.ParameterType.IsAssignableFrom(matchingMemberProperty.PropertyType))
                {
                    matchingMemberProperty = null;
                }

                if (matchingMemberProperty != null || parameterInfo.Name != null)
                {
                    JsonProperty property = CreatePropertyFromConstructorParameterWithConstructorInfo(matchingMemberProperty, parameterInfo, constructor);

                    if (property != null)
                    {
                        parameterCollection.AddProperty(property);
                    }
                }
            }

            return(parameterCollection);
        }
Exemplo n.º 26
0
        protected override IList <JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
        {
            var serializableMembers = GetSerializableMembers(type).Cast <PropertyInfo>();

            if (serializableMembers == null)
            {
                throw new JsonSerializationException("Null collection of seralizable members returned.");
            }

            var typeDefinition     = _ServerConfiguration.GetScimTypeDefinition(type);
            var propertyCollection = new JsonPropertyCollection(type);

            foreach (var member in serializableMembers)
            {
                var property = CreateProperty(member, memberSerialization);
                if (property != null)
                {
                    var  state             = _InstanceState;
                    var  propertyNameTable = state.NameTable;
                    bool lockTaken         = false;
                    try
                    {
                        Monitor.Enter(propertyNameTable, ref lockTaken);
                        property.PropertyName = state.NameTable.Add(property.PropertyName);

                        IScimTypeAttributeDefinition attributeDefinition;
                        if (typeDefinition != null && typeDefinition.AttributeDefinitions.TryGetValue(member, out attributeDefinition))
                        {
                            // Internal json.net deserialization logic will hit the ShouldDeserialize delegate before checking property.Writable
                            // Still, we'll set Writable to accurately reflect this
                            property.Writable = attributeDefinition.Mutability != Mutability.ReadOnly;  // deserializable?

                            // setting property.Readable will shortcut the ShouldSerialize delegate
                            // INTERNAL JSON.NET LOGIC
                            // if (!property.Ignored && property.Readable && ShouldSerialize(writer, property, value) && IsSpecified(writer, property, value))
                            property.Readable = attributeDefinition.Mutability != Mutability.WriteOnly; // serializable?

                            // Only attach a conditional deserialization delegate if the attribute is not ReadWrite.
                            if (attributeDefinition.Mutability != Mutability.ReadWrite)
                            {
                                property.ShouldDeserialize = o =>
                                {
                                    if (attributeDefinition.Mutability == Mutability.ReadOnly)
                                    {
                                        return(false);
                                    }

                                    return(true);
                                };
                            }

                            // Only attach a conditional serialization delegate if the attribute is not Always returned.
                            if (attributeDefinition.Returned != Returned.Always)
                            {
                                var existingShouldSerializeFunc = property.ShouldSerialize;
                                property.ShouldSerialize = o =>
                                {
                                    if (attributeDefinition.Mutability == Mutability.WriteOnly || attributeDefinition.Returned == Returned.Never)
                                    {
                                        return(false);
                                    }

                                    var httpMethod = AmbientRequestService.HttpMethod;
                                    if (attributeDefinition.Returned == Returned.Default ||
                                        (attributeDefinition.Returned == Returned.Request && (httpMethod == HttpMethod.Post || httpMethod == _Patch || httpMethod == HttpMethod.Put)))
                                    {
                                        var queryOptions = AmbientRequestService.QueryOptions;
                                        if (queryOptions.Attributes.Any() && !queryOptions.Attributes.Contains(property.PropertyName))
                                        {
                                            return(false);
                                        }

                                        if (queryOptions.ExcludedAttributes.Any() && queryOptions.ExcludedAttributes.Contains(property.PropertyName))
                                        {
                                            return(false);
                                        }
                                    }

                                    if (existingShouldSerializeFunc != null)
                                    {
                                        return(existingShouldSerializeFunc(o));
                                    }

                                    return(true);
                                };
                            }
                        }
                    }
                    finally
                    {
                        if (lockTaken)
                        {
                            Monitor.Exit(propertyNameTable);
                        }
                    }

                    propertyCollection.AddProperty(property);
                }
            }

            return(propertyCollection.OrderBy(p => p.Order ?? -1).ToList());
        }
Exemplo n.º 27
0
 /// <summary>
 /// Initializes a new instance of the <see cref="JsonObjectContract"/> class.
 /// </summary>
 /// <param name="underlyingType">The underlying type for the contract.</param>
 public JsonObjectContract(Type underlyingType)
   : base(underlyingType)
 {
   Properties = new JsonPropertyCollection(UnderlyingType);
 }
Exemplo n.º 28
0
        protected override IList <JsonProperty> CreateConstructorParameters(ConstructorInfo constructor, JsonPropertyCollection memberProperties)
        {
            var rslt = base.CreateConstructorParameters(constructor, memberProperties);

            return(rslt);
        }
        internal static void ExtendProperties(this IDictionary <string, Schema> schemaProperties, JsonPropertyCollection jsonProperties)
        {
            if (schemaProperties is null)
            {
                return;
            }

            foreach (var schemaProperty in schemaProperties)
            {
                var jsonProperty = jsonProperties.FirstOrDefault(x => x.PropertyName == schemaProperty.Key);
                schemaProperty.Value.ExtendProperty(jsonProperty);
            }
        }
            protected override IList <JsonProperty> CreateConstructorParameters(ConstructorInfo constructor, JsonPropertyCollection memberProperties)
            {
                var properties = base.CreateConstructorParameters(constructor, memberProperties);

                foreach (var property in properties.Where(p => p.PropertyType == typeof(string)))
                {
                    property.DefaultValue         = ConstructorParametersRespectDefaultValue.DefaultValue;
                    property.DefaultValueHandling = DefaultValueHandling.Populate;
                }

                return(properties);
            }
Exemplo n.º 31
0
        protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
        {
            var serializableMembers = GetSerializableMembers(type).Cast<PropertyInfo>();
            if (serializableMembers == null)
                throw new JsonSerializationException("Null collection of seralizable members returned.");

            var typeDefinition = _ServerConfiguration.GetScimTypeDefinition(type);
            var propertyCollection = new JsonPropertyCollection(type);
            foreach (var member in serializableMembers)
            {
                var property = CreateProperty(member, memberSerialization);
                if (property != null)
                {
                    var state = _InstanceState;
                    var propertyNameTable = state.NameTable;
                    bool lockTaken = false;
                    try
                    {
                        Monitor.Enter(propertyNameTable, ref lockTaken);
                        property.PropertyName = state.NameTable.Add(property.PropertyName);

                        IScimTypeAttributeDefinition attributeDefinition;
                        if (typeDefinition != null && typeDefinition.AttributeDefinitions.TryGetValue(member, out attributeDefinition))
                        {
                            // Internal json.net deserialization logic will hit the ShouldDeserialize delegate before checking property.Writable
                            // Still, we'll set Writable to accurately reflect this
                            property.Writable = attributeDefinition.Mutability != Mutability.ReadOnly;  // deserializable?

                            // setting property.Readable will shortcut the ShouldSerialize delegate
                            // INTERNAL JSON.NET LOGIC
                            // if (!property.Ignored && property.Readable && ShouldSerialize(writer, property, value) && IsSpecified(writer, property, value))
                            property.Readable = attributeDefinition.Mutability != Mutability.WriteOnly; // serializable?

                            // Only attach a conditional deserialization delegate if the attribute is not ReadWrite.
                            if (attributeDefinition.Mutability != Mutability.ReadWrite)
                            {
                                property.ShouldDeserialize = o =>
                                {
                                    if (attributeDefinition.Mutability == Mutability.ReadOnly)
                                        return false;

                                    return true;
                                };
                            }

                            // Only attach a conditional serialization delegate if the attribute is not Always returned.
                            if (attributeDefinition.Returned != Returned.Always)
                            {
                                var existingShouldSerializeFunc = property.ShouldSerialize;
                                property.ShouldSerialize = o =>
                                {
                                    if (attributeDefinition.Mutability == Mutability.WriteOnly || attributeDefinition.Returned == Returned.Never)
                                        return false;

                                    var httpMethod = AmbientRequestService.HttpMethod;
                                    if (attributeDefinition.Returned == Returned.Default ||
                                        (attributeDefinition.Returned == Returned.Request && (httpMethod == HttpMethod.Post || httpMethod == _Patch || httpMethod == HttpMethod.Put)))
                                    {
                                        var queryOptions = AmbientRequestService.QueryOptions;
                                        if (queryOptions.Attributes.Any() && !queryOptions.Attributes.Contains(property.PropertyName))
                                            return false;

                                        if (queryOptions.ExcludedAttributes.Any() && queryOptions.ExcludedAttributes.Contains(property.PropertyName))
                                            return false;
                                    }

                                    if (existingShouldSerializeFunc != null)
                                        return existingShouldSerializeFunc(o);

                                    return true;
                                };
                            }
                        }
                    }
                    finally
                    {
                        if (lockTaken)
                            Monitor.Exit(propertyNameTable);
                    }

                    propertyCollection.AddProperty(property);
                }
            }

            return propertyCollection.OrderBy(p => p.Order ?? -1).ToList();
        }
        private Expression <Func <ModelObjectSerializationContext, ObjectConstructor <object> > > TryCreateFactoryProvider(Type type, JsonPropertyCollection properties)
        {
            var injectors = (from p in properties select _modelObjectSpecialValueProvider.TryGetInjector(type, p)).ToList();

            if (injectors.Any(i => i != null))
            {
                var closureContext = Expression.Parameter(typeof(ModelObjectSerializationContext));
                return(Expression.Lambda <Func <ModelObjectSerializationContext, ObjectConstructor <object> > >(
                           CreateFactory(type, properties, injectors, closureContext),
                           closureContext));
            }
            return(null);
        }