StringMapTypeDeserializer.cs 4.1 KB

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