StringMapTypeDeserializer.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. namespace Emby.Server.Implementations.Services
  5. {
  6. /// <summary>
  7. /// Serializer cache of delegates required to create a type from a string map (e.g. for REST urls)
  8. /// </summary>
  9. public class StringMapTypeDeserializer
  10. {
  11. internal class PropertySerializerEntry
  12. {
  13. public PropertySerializerEntry(Action<object, object> propertySetFn, Func<string, object> propertyParseStringFn, Type propertyType)
  14. {
  15. PropertySetFn = propertySetFn;
  16. PropertyParseStringFn = propertyParseStringFn;
  17. PropertyType = propertyType;
  18. }
  19. public Action<object, object> PropertySetFn { get; private set; }
  20. public Func<string, object> PropertyParseStringFn { get; private set; }
  21. public Type PropertyType { get; private set; }
  22. }
  23. private readonly Type type;
  24. private readonly Dictionary<string, PropertySerializerEntry> propertySetterMap
  25. = new Dictionary<string, PropertySerializerEntry>(StringComparer.OrdinalIgnoreCase);
  26. public Func<string, object> GetParseFn(Type propertyType)
  27. {
  28. if (propertyType == typeof(string))
  29. {
  30. return s => s;
  31. }
  32. return _GetParseFn(propertyType);
  33. }
  34. private readonly Func<Type, object> _CreateInstanceFn;
  35. private readonly Func<Type, Func<string, object>> _GetParseFn;
  36. public StringMapTypeDeserializer(Func<Type, object> createInstanceFn, Func<Type, Func<string, object>> getParseFn, Type type)
  37. {
  38. _CreateInstanceFn = createInstanceFn;
  39. _GetParseFn = getParseFn;
  40. this.type = type;
  41. foreach (var propertyInfo in RestPath.GetSerializableProperties(type))
  42. {
  43. var propertySetFn = TypeAccessor.GetSetPropertyMethod(propertyInfo);
  44. var propertyType = propertyInfo.PropertyType;
  45. var propertyParseStringFn = GetParseFn(propertyType);
  46. var propertySerializer = new PropertySerializerEntry(propertySetFn, propertyParseStringFn, propertyType);
  47. propertySetterMap[propertyInfo.Name] = propertySerializer;
  48. }
  49. }
  50. public object PopulateFromMap(object instance, IDictionary<string, string> keyValuePairs)
  51. {
  52. PropertySerializerEntry propertySerializerEntry = null;
  53. if (instance == null)
  54. {
  55. instance = _CreateInstanceFn(type);
  56. }
  57. foreach (var pair in keyValuePairs)
  58. {
  59. string propertyName = pair.Key;
  60. string propertyTextValue = pair.Value;
  61. if (propertyTextValue == null
  62. || !propertySetterMap.TryGetValue(propertyName, out propertySerializerEntry)
  63. || propertySerializerEntry.PropertySetFn == null)
  64. {
  65. continue;
  66. }
  67. if (propertySerializerEntry.PropertyType == typeof(bool))
  68. {
  69. //InputExtensions.cs#530 MVC Checkbox helper emits extra hidden input field, generating 2 values, first is the real value
  70. propertyTextValue = LeftPart(propertyTextValue, ',');
  71. }
  72. var value = propertySerializerEntry.PropertyParseStringFn(propertyTextValue);
  73. if (value == null)
  74. {
  75. continue;
  76. }
  77. propertySerializerEntry.PropertySetFn(instance, value);
  78. }
  79. return instance;
  80. }
  81. public static string LeftPart(string strVal, char needle)
  82. {
  83. if (strVal == null)
  84. {
  85. return null;
  86. }
  87. var pos = strVal.IndexOf(needle);
  88. return pos == -1
  89. ? strVal
  90. : strVal.Substring(0, pos);
  91. }
  92. }
  93. internal static class TypeAccessor
  94. {
  95. public static Action<object, object> GetSetPropertyMethod(PropertyInfo propertyInfo)
  96. {
  97. if (!propertyInfo.CanWrite || propertyInfo.GetIndexParameters().Length > 0)
  98. {
  99. return null;
  100. }
  101. var setMethodInfo = propertyInfo.SetMethod;
  102. return (instance, value) => setMethodInfo.Invoke(instance, new[] { value });
  103. }
  104. }
  105. }