JsonPropertyInfo.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System;
  2. using System.Collections;
  3. using System.Reflection;
  4. namespace FastReport.Utils.Json.Serialization
  5. {
  6. internal class JsonPropertyInfo
  7. {
  8. public PropertyInfo Info { get; }
  9. public string Name { get; }
  10. public bool IgnoreNullValue { get; }
  11. public bool IsPrimitive =>
  12. Info.PropertyType.IsPrimitive;
  13. public bool IsDateTime =>
  14. Info.PropertyType == typeof(DateTime);
  15. public bool IsCollection
  16. {
  17. get
  18. {
  19. var type = Info.PropertyType;
  20. var result = type.IsArray || typeof(IEnumerable).IsAssignableFrom(type);
  21. return result;
  22. }
  23. }
  24. public bool IsEnum => Info.PropertyType.IsEnum;
  25. internal static JsonPropertyInfo Parse(PropertyInfo propertyInfo)
  26. {
  27. var attr = Attribute.GetCustomAttribute(propertyInfo, typeof(JsonPropertyAttribute)) as JsonPropertyAttribute;
  28. string propName = attr?.PropertyName ?? propertyInfo.Name;
  29. bool ignoreNull = attr?.IgnoreNullValue ?? true;
  30. var propInfo = new JsonPropertyInfo(propertyInfo, propName, ignoreNull);
  31. return propInfo;
  32. }
  33. public JsonPropertyInfo(PropertyInfo propertyInfo,
  34. string propertyName,
  35. bool ignoreNullValue = true)
  36. {
  37. Info = propertyInfo;
  38. Name = propertyName;
  39. IgnoreNullValue = ignoreNullValue;
  40. }
  41. }
  42. }