2
0

StringMapTypeDeserializer.cs 4.5 KB

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