StringMapTypeDeserializer.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.Serialization;
  4. using System.Linq;
  5. using System.Reflection;
  6. namespace ServiceStack.Serialization
  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)
  16. {
  17. PropertySetFn = propertySetFn;
  18. PropertyParseStringFn = propertyParseStringFn;
  19. }
  20. public Action<object, object> PropertySetFn;
  21. public Func<string,object> PropertyParseStringFn;
  22. public Type PropertyType;
  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. //Don't JSV-decode string values for string properties
  30. if (propertyType == typeof(string))
  31. return s => s;
  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(type, propertyInfo);
  44. var propertyType = propertyInfo.PropertyType;
  45. var propertyParseStringFn = GetParseFn(propertyType);
  46. var propertySerializer = new PropertySerializerEntry(propertySetFn, propertyParseStringFn) { PropertyType = propertyType };
  47. propertySetterMap[propertyInfo.Name] = propertySerializer;
  48. }
  49. }
  50. public object PopulateFromMap(object instance, IDictionary<string, string> keyValuePairs)
  51. {
  52. string propertyName = null;
  53. string propertyTextValue = null;
  54. PropertySerializerEntry propertySerializerEntry = null;
  55. if (instance == null)
  56. instance = _CreateInstanceFn(type);
  57. foreach (var pair in keyValuePairs.Where(x => !string.IsNullOrEmpty(x.Value)))
  58. {
  59. propertyName = pair.Key;
  60. propertyTextValue = pair.Value;
  61. if (!propertySetterMap.TryGetValue(propertyName, out propertySerializerEntry))
  62. {
  63. if (propertyName == "v")
  64. {
  65. continue;
  66. }
  67. continue;
  68. }
  69. if (propertySerializerEntry.PropertySetFn == null)
  70. {
  71. continue;
  72. }
  73. if (propertySerializerEntry.PropertyType == typeof(bool))
  74. {
  75. //InputExtensions.cs#530 MVC Checkbox helper emits extra hidden input field, generating 2 values, first is the real value
  76. propertyTextValue = LeftPart(propertyTextValue, ',');
  77. }
  78. var value = propertySerializerEntry.PropertyParseStringFn(propertyTextValue);
  79. if (value == null)
  80. {
  81. continue;
  82. }
  83. propertySerializerEntry.PropertySetFn(instance, value);
  84. }
  85. return instance;
  86. }
  87. public static string LeftPart(string strVal, char needle)
  88. {
  89. if (strVal == null) return null;
  90. var pos = strVal.IndexOf(needle);
  91. return pos == -1
  92. ? strVal
  93. : strVal.Substring(0, pos);
  94. }
  95. }
  96. internal class TypeAccessor
  97. {
  98. public static Action<object, object> GetSetPropertyMethod(Type type, PropertyInfo propertyInfo)
  99. {
  100. if (!propertyInfo.CanWrite || propertyInfo.GetIndexParameters().Any()) return null;
  101. var setMethodInfo = propertyInfo.SetMethod;
  102. return (instance, value) => setMethodInfo.Invoke(instance, new[] { value });
  103. }
  104. }
  105. }