Пример #1
0
        static Dictionary <string, object> GetDictionary(object obj, IPropertyNameConverter propertyNameConverter = null)
        {
            var dictionary = new Dictionary <string, object>();

            foreach (PropertyInfo propertyInfo in obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                string propertyName;

                var propertyValue = propertyInfo.GetValue(obj);
                var propertyType  = propertyInfo.PropertyType;

                if (!propertyValue.IsNullOrDefault(propertyType))
                {
                    if (propertyInfo.CustomAttributes?.Count() > 0 &&
                        propertyInfo.GetCustomAttribute(typeof(MappingPropertyName)) is MappingPropertyName mappingProperty)
                    {
                        propertyName = mappingProperty.Name;
                    }
                    else if (propertyNameConverter != null)
                    {
                        propertyName = propertyNameConverter.Convert(propertyInfo.Name);
                    }
                    else
                    {
                        propertyName = Settings.PropertyNameConverter.Convert(propertyInfo.Name);
                    }

                    AddDictionaryValue(ref dictionary, propertyName, propertyValue, propertyInfo.PropertyType, propertyNameConverter);
                }
            }

            return(dictionary);
        }
        public static MutableDocument ToMutableDocument <T>(this T obj,
                                                            string id,
                                                            IPropertyNameConverter propertyNameConverter = null)
        {
            MutableDocument document;

            if (string.IsNullOrEmpty(id))
            {
                document = new MutableDocument();
            }
            else
            {
                document = new MutableDocument(id);
            }

            var  dictionary = GetDictionary(obj, propertyNameConverter);
            Type objType    = obj.GetType();

            if (!IsSimple(objType) && objType.IsClass)
            {
                //dictionary.Add("$type", obj.GetType().AssemblyQualifiedName);
            }

            if (dictionary != null)
            {
                document.SetData(dictionary);
            }

            return(document);
        }
        public static MutableDocument ToMutableDocument <T>(this T obj,
                                                            IPropertyNameConverter propertyNameConverter = null)
        {
            string id = (string)GetProperties(obj)
                        .FirstOrDefault(x => (x.Name == "Id" || x.Name == "id") && (x.PropertyType == typeof(string)))
                        ?.GetValue(obj);

            return(ToMutableDocument(obj, id, propertyNameConverter));
        }
        static void AddDictionaryValue(ref Dictionary <string, object> dictionary,
                                       string propertyName,
                                       object propertyValue,
                                       Type propertyType,
                                       IPropertyNameConverter propertyNameConverter = null)
        {
            if (propertyType == typeof(byte[]) || propertyType == typeof(Stream))
            {
                dictionary[propertyName] = new Blob(string.Empty, (byte[])propertyValue);
            }
            else if (propertyType == typeof(DateTime) || propertyType == typeof(DateTime?))
            {
                dictionary[propertyName] = new DateTimeOffset((DateTime)propertyValue);
            }
            else if (propertyType == typeof(TimeSpan))
            {
                dictionary[propertyName] = propertyValue.ToString();
            }
            else if (!propertyType.IsSimple() && !propertyType.IsEnum && propertyType.IsClass && propertyValue != null)
            {
                if (typeof(IEnumerable).IsAssignableFrom(propertyType))
                {
                    if (propertyType.IsArray && propertyType.GetElementType().IsSimple() ||
                        (!propertyType.IsArray && propertyValue is IList &&
                         propertyValue.GetType().GetTypeInfo().GenericTypeArguments[0].IsSimple()))
                    {
                        dictionary[propertyName] = propertyValue;
                    }
                    else
                    {
                        var items = propertyValue as IEnumerable;

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

                        foreach (var item in items)
                        {
                            dictionaries.Add(GetDictionary(item, propertyNameConverter));
                        }

                        dictionary[propertyName] = dictionaries.ToArray();
                    }
                }
                else
                {
                    dictionary[propertyName] = GetDictionary(propertyValue, propertyNameConverter);
                }
            }
            else if (propertyType.IsEnum)
            {
                dictionary[propertyName] = propertyValue.ToString();
            }
            else
            {
                dictionary[propertyName] = propertyValue;
            }
        }
        static Dictionary <string, object> GetDictionary(object obj, IPropertyNameConverter propertyNameConverter = null)
        {
            var dictionary = new Dictionary <string, object>();

            dictionary.Add("$type", obj.GetType().AssemblyQualifiedName);

            foreach (PropertyInfo propertyInfo in GetProperties(obj))
            {
                string propertyName;

                var propertyValue = propertyInfo.GetValue(obj);
                var propertyType  = propertyInfo.PropertyType;

                if (propertyType.IsEnum)
                {
                    var attribute = propertyInfo.PropertyType.GetMember(propertyValue.ToString()).FirstOrDefault()?.GetCustomAttribute <EnumMemberAttribute>();
                    if (attribute != null)
                    {
                        propertyValue = attribute.Value;
                    }
                }

                if (propertyValue != null)
                {
                    if (propertyInfo.CustomAttributes?.Count() > 0 &&
                        propertyInfo.GetCustomAttribute(typeof(MappingPropertyName)) is MappingPropertyName mappingProperty)
                    {
                        propertyName = mappingProperty.Name;
                    }
                    else if (propertyInfo.CustomAttributes?.Count() > 0 &&
                             propertyInfo.GetCustomAttribute(typeof(JsonPropertyAttribute)) is JsonPropertyAttribute jsonProperty)
                    {
                        propertyName = jsonProperty.PropertyName;
                    }
                    else if (propertyNameConverter != null)
                    {
                        propertyName = propertyNameConverter.Convert(propertyInfo.Name);
                    }
                    else
                    {
                        propertyName = Settings.PropertyNameConverter.Convert(propertyInfo.Name);
                    }

                    AddDictionaryValue(ref dictionary, propertyName, propertyValue, propertyValue.GetType(), propertyNameConverter);
                }
            }

            return(dictionary);
        }
        static Dictionary <string, object> GetDictionary(object obj, IPropertyNameConverter propertyNameConverter = null)
        {
            var dictionary = new Dictionary <string, object>();

            var properties = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(pi => !Attribute.IsDefined(pi, typeof(JsonIgnoreAttribute)))?.ToList();

            foreach (PropertyInfo propertyInfo in properties)
            {
                string propertyName;

                var propertyValue = propertyInfo.GetValue(obj);
                var propertyType  = propertyInfo.PropertyType;

                if (propertyType.IsEnum)
                {
                    var attribute = propertyInfo.PropertyType.GetMember(propertyValue.ToString()).FirstOrDefault()?.GetCustomAttribute <EnumMemberAttribute>();
                    if (attribute != null)
                    {
                        propertyValue = attribute.Value;
                    }
                }

                if (propertyValue != null)
                {
                    if (propertyInfo.CustomAttributes?.Count() > 0 &&
                        propertyInfo.GetCustomAttribute(typeof(MappingPropertyName)) is MappingPropertyName mappingProperty)
                    {
                        propertyName = mappingProperty.Name;
                    }
                    else if (propertyInfo.CustomAttributes?.Count() > 0 &&
                             propertyInfo.GetCustomAttribute(typeof(JsonPropertyAttribute)) is JsonPropertyAttribute jsonProperty)
                    {
                        propertyName = jsonProperty.PropertyName;
                    }
                    else if (propertyNameConverter != null)
                    {
                        propertyName = propertyNameConverter.Convert(propertyInfo.Name);
                    }
                    else
                    {
                        propertyName = Settings.PropertyNameConverter.Convert(propertyInfo.Name);
                    }

                    AddDictionaryValue(ref dictionary, propertyName, propertyValue, propertyInfo.PropertyType, propertyNameConverter);
                }
            }

            return(dictionary);
        }
Пример #7
0
 public ResourceSerializer(
     object value,
     ApiResource type,
     Uri baseUrl,
     IUrlPathBuilder urlBuilder,
     PaginationContext paginationContext,
     IncludeContext includeContext,
     IPropertyNameConverter propertyNameConverter = null)
 {
     _propertyNameConverter = propertyNameConverter ?? new DefaultPropertyNameConverter();
     _urlBuilder            = urlBuilder;
     _resource           = type;
     _value              = value;
     _baseUrl            = baseUrl;
     _paginationContext  = paginationContext;
     _includeContext     = includeContext;
     _includedGraphPaths = IncludedGraphPathsFromContext(includeContext);
 }
        public static MutableDocument ToMutableDocument <T>(this T obj,
                                                            string id = null,
                                                            IPropertyNameConverter propertyNameConverter = null)
        {
            MutableDocument document;

            if (id != null)
            {
                document = new MutableDocument(id);
            }
            else
            {
                document = new MutableDocument();
            }

            var dictionary = GetDictionary(obj, propertyNameConverter);

            if (dictionary != null)
            {
                document.SetData(dictionary);
            }

            return(document);
        }
Пример #9
0
 public JsonApiContractResolver(IPropertyNameConverter nameConverter)
 {
     _nameConverter = nameConverter;
 }
 public static MutableDocument ToMutableDocument <T>(this T obj,
                                                     IPropertyNameConverter propertyNameConverter)
 {
     return(ToMutableDocument(obj, null, propertyNameConverter));
 }
Пример #11
0
 public ResourceDeserializer(JToken @object, Type target, IPropertyNameConverter propertyNameConverter = null)
 {
     _object = @object;
     _target = target;
     _propertyNameConverter = propertyNameConverter ?? new DefaultPropertyNameConverter();
 }
 public SourceContractResolver(IPropertyNameConverter nameConverter, ApiResource apiResource)
 {
     _apiResource   = apiResource;
     _nameConverter = nameConverter;
     _depth         = 0;
 }