2
0

StringMapTypeDeserializer.cs 4.2 KB

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