StringMapTypeDeserializer.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. propertySetterMap[propertyInfo.Name] = propertySerializer;
  44. }
  45. }
  46. public object PopulateFromMap(object instance, IDictionary<string, string> keyValuePairs)
  47. {
  48. string propertyName = null;
  49. string propertyTextValue = null;
  50. PropertySerializerEntry propertySerializerEntry = null;
  51. if (instance == null)
  52. instance = ServiceStackHost.Instance.CreateInstance(type);
  53. foreach (var pair in keyValuePairs.Where(x => !string.IsNullOrEmpty(x.Value)))
  54. {
  55. propertyName = pair.Key;
  56. propertyTextValue = pair.Value;
  57. if (!propertySetterMap.TryGetValue(propertyName, out propertySerializerEntry))
  58. {
  59. if (propertyName == "v")
  60. {
  61. continue;
  62. }
  63. continue;
  64. }
  65. if (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 = LeftPart(propertyTextValue, ',');
  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. public static string LeftPart(string strVal, char needle)
  84. {
  85. if (strVal == null) return null;
  86. var pos = strVal.IndexOf(needle);
  87. return pos == -1
  88. ? strVal
  89. : strVal.Substring(0, pos);
  90. }
  91. }
  92. internal class TypeAccessor
  93. {
  94. public static Action<object, object> GetSetPropertyMethod(Type type, PropertyInfo propertyInfo)
  95. {
  96. if (!propertyInfo.CanWrite || propertyInfo.GetIndexParameters().Any()) return null;
  97. var setMethodInfo = propertyInfo.SetMethod;
  98. return (instance, value) => setMethodInfo.Invoke(instance, new[] { value });
  99. }
  100. }
  101. }