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 ServiceStackHost.Instance.GetParseFn(propertyType);
  33. }
  34. public StringMapTypeDeserializer(Type type)
  35. {
  36. this.type = type;
  37. foreach (var propertyInfo in type.GetSerializableProperties())
  38. {
  39. var propertySetFn = TypeAccessor.GetSetPropertyMethod(type, propertyInfo);
  40. var propertyType = propertyInfo.PropertyType;
  41. var propertyParseStringFn = GetParseFn(propertyType);
  42. var propertySerializer = new PropertySerializerEntry(propertySetFn, propertyParseStringFn) { PropertyType = propertyType };
  43. var attr = propertyInfo.AllAttributes<DataMemberAttribute>().FirstOrDefault();
  44. if (attr != null && attr.Name != null)
  45. {
  46. propertySetterMap[attr.Name] = propertySerializer;
  47. }
  48. propertySetterMap[propertyInfo.Name] = propertySerializer;
  49. }
  50. }
  51. public object PopulateFromMap(object instance, IDictionary<string, string> keyValuePairs)
  52. {
  53. string propertyName = null;
  54. string propertyTextValue = null;
  55. PropertySerializerEntry propertySerializerEntry = null;
  56. if (instance == null)
  57. instance = ServiceStackHost.Instance.CreateInstance(type);
  58. foreach (var pair in keyValuePairs.Where(x => !string.IsNullOrEmpty(x.Value)))
  59. {
  60. propertyName = pair.Key;
  61. propertyTextValue = pair.Value;
  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().Any()) return null;
  102. var setMethodInfo = propertyInfo.SetMethod;
  103. return (instance, value) => setMethodInfo.Invoke(instance, new[] { value });
  104. }
  105. }
  106. }