StringMapTypeDeserializer.cs 4.4 KB

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