Esempio n. 1
0
        public static object StoredToApi(Type type, object?o, ICustomPropertyConfigurationProvider?customPropertyConfigurationProvider)
        {
            if (o == null)
            {
                return(null);
            }

            var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);

            if (PropertyHelpers.IsSimpleType(type) || type == typeof(byte[]) || !properties.Any())
            {
                return(o);
            }

            var result = new Dictionary <string, object?>();

            foreach (var propertyInfo in properties)
            {
                var propertyValue = propertyInfo.GetValue(o);
                var customPropertyConfiguration = customPropertyConfigurationProvider?.TryGetConfiguration(o, propertyInfo);
                var resolvedProperty            = customPropertyConfiguration != null?customPropertyConfiguration.StoredToApi(propertyValue) : propertyValue;

                result[propertyInfo.Name] = resolvedProperty;
            }

            return(result);
        }
Esempio n. 2
0
        public static object StoredToApiDeep(object?o, ICustomPropertyConfigurationProvider?customPropertyConfigurationProvider)
        {
            if (o == null)
            {
                return(null);
            }

            if (o is IDictionary dictionary)
            {
                return(dictionary.Keys.Cast <object>().ToDictionary(
                           k => StoredToApiInternal(k, customPropertyConfigurationProvider),
                           k => StoredToApiInternal(dictionary[k], customPropertyConfigurationProvider)
                           ));
            }

            var type = o.GetType();

            if (!PropertyHelpers.IsSimpleType(type) && type != typeof(byte[]) && o is IEnumerable enumerable)
            {
                return(enumerable.Cast <object>().Select(x => StoredToApiInternal(x, customPropertyConfigurationProvider)).ToArray());
            }

            var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);

            if (PropertyHelpers.IsSimpleType(type) || type == typeof(byte[]) || !properties.Any())
            {
                return(o);
            }

            var result = new Dictionary <string, object>();

            foreach (var propertyInfo in properties)
            {
                var propertyValue = propertyInfo.GetValue(o);
                var customPropertyConfiguration = customPropertyConfigurationProvider?.TryGetConfiguration(o, propertyInfo);
                var resolvedProperty            = customPropertyConfiguration != null?customPropertyConfiguration.StoredToApi(propertyValue) : propertyValue;

                result[propertyInfo.Name] = StoredToApiDeep(resolvedProperty, customPropertyConfigurationProvider);
            }

            return(result);
        }