public object PopulateFromMap(object instance, IDictionary <string, string> keyValuePairs, List <string> ignoredWarningsOnPropertyNames = null) { var errors = new List <RequestBindingError>(); string propertyName = null; string propertyTextValue = null; PropertySerializerEntry propertySerializerEntry = null; if (instance == null) { instance = type.CreateInstance(); } foreach (var pair in keyValuePairs.Where(x => !string.IsNullOrEmpty(x.Value))) { try { propertyName = pair.Key; propertyTextValue = pair.Value; if (!propertySetterMap.TryGetValue(propertyName, out propertySerializerEntry)) { if (propertyName == "v") { int version; var hasVersion = instance as IHasVersion; if (hasVersion != null && int.TryParse(pair.Value, out version)) { hasVersion.Version = version; } continue; } var ignoredProperty = propertyName.ToLowerInvariant(); if (ignoredWarningsOnPropertyNames == null || !ignoredWarningsOnPropertyNames.Contains(ignoredProperty)) { Log.WarnFormat("Property '{0}' does not exist on type '{1}'", ignoredProperty, type.FullName); } continue; } if (propertySerializerEntry.PropertySetFn == null) { Log.WarnFormat("Could not set value of read-only property '{0}' on type '{1}'", propertyName, type.FullName); continue; } if (propertySerializerEntry.PropertyType == typeof(bool)) { //InputExtensions.cs#530 MVC Checkbox helper emits extra hidden input field, generating 2 values, first is the real value propertyTextValue = propertyTextValue.LeftPart(','); } var value = propertySerializerEntry.PropertyParseStringFn(propertyTextValue); if (value == null) { Log.WarnFormat("Could not create instance on '{0}' for property '{1}' with text value '{2}'", instance, propertyName, propertyTextValue); continue; } propertySerializerEntry.PropertySetFn(instance, value); } catch (Exception ex) { var error = new RequestBindingError { ErrorMessage = ex.Message }; if (propertyName != null) { error.PropertyName = propertyName; } if (propertyTextValue != null) { error.PropertyValueString = propertyTextValue; } if (propertySerializerEntry != null && propertySerializerEntry.PropertyType != null) { error.PropertyType = propertySerializerEntry.PropertyType; } errors.Add(error); } } if (errors.Count > 0) { var serializationException = new SerializationException($"Unable to bind to request '{type.Name}'"); serializationException.Data.Add("errors", errors); throw serializationException; } return(instance); }
private object PopulateFromKeyValue(object instance, string propertyName, string propertyTextValue, out PropertySerializerEntry propertySerializerEntry, List <RequestBindingError> errors, List <string> ignoredWarningsOnPropertyNames = null) { propertySerializerEntry = null; try { if (!propertySetterMap.TryGetValue(propertyName, out propertySerializerEntry)) { if (propertyName == "v") { int version; var hasVersion = instance as IHasVersion; if (hasVersion != null && int.TryParse(propertyTextValue, out version)) { hasVersion.Version = version; } return(instance); } var ignoredProperty = propertyName.ToLowerInvariant(); if (ignoredWarningsOnPropertyNames == null || !ignoredWarningsOnPropertyNames.Contains(ignoredProperty)) { Log.WarnFormat("Property '{0}' does not exist on type '{1}'", ignoredProperty, type.FullName); } return(instance); } if (propertySerializerEntry.PropertySetFn == null) { Log.WarnFormat("Could not set value of read-only property '{0}' on type '{1}'", propertyName, type.FullName); return(instance); } if (propertySerializerEntry.PropertyType == typeof(bool)) { //InputExtensions.cs#530 MVC Checkbox helper emits extra hidden input field, generating 2 values, first is the real value propertyTextValue = propertyTextValue.LeftPart(','); } var value = propertySerializerEntry.PropertyParseStringFn(propertyTextValue); if (value == null) { Log.WarnFormat("Could not create instance on '{0}' for property '{1}' with text value '{2}'", instance, propertyName, propertyTextValue); return(instance); } propertySerializerEntry.PropertySetFn(instance, value); } catch (Exception ex) { var error = new RequestBindingError { ErrorMessage = ex.Message }; if (propertyName != null) { error.PropertyName = propertyName; } if (propertyTextValue != null) { error.PropertyValueString = propertyTextValue; } if (propertySerializerEntry != null && propertySerializerEntry.PropertyType != null) { error.PropertyType = propertySerializerEntry.PropertyType; } errors.Add(error); } return(instance); }
public object PopulateFromMap(object instance, IDictionary<string, string> keyValuePairs, List<string> ignoredWarningsOnPropertyNames = null) { var errors = new List<RequestBindingError>(); string propertyName = null; string propertyTextValue = null; PropertySerializerEntry propertySerializerEntry = null; if (instance == null) instance = type.CreateInstance(); foreach (var pair in keyValuePairs.Where(x => !string.IsNullOrEmpty(x.Value))) { try { propertyName = pair.Key; propertyTextValue = pair.Value; if (!propertySetterMap.TryGetValue(propertyName, out propertySerializerEntry)) { if (propertyName == "v") { int version; var hasVersion = instance as IHasVersion; if (hasVersion != null && int.TryParse(pair.Value, out version)) { hasVersion.Version = version; } continue; } var ignoredProperty = propertyName.ToLowerInvariant(); if (ignoredWarningsOnPropertyNames == null || !ignoredWarningsOnPropertyNames.Contains(ignoredProperty)) { Log.WarnFormat("Property '{0}' does not exist on type '{1}'", ignoredProperty, type.FullName); } continue; } if (propertySerializerEntry.PropertySetFn == null) { Log.WarnFormat("Could not set value of read-only property '{0}' on type '{1}'", propertyName, type.FullName); continue; } if (propertySerializerEntry.PropertyType == typeof (bool)) { //InputExtensions.cs#530 MVC Checkbox helper emits extra hidden input field, generating 2 values, first is the real value propertyTextValue = propertyTextValue.LeftPart(','); } var value = propertySerializerEntry.PropertyParseStringFn(propertyTextValue); if (value == null) { Log.WarnFormat("Could not create instance on '{0}' for property '{1}' with text value '{2}'", instance, propertyName, propertyTextValue); continue; } propertySerializerEntry.PropertySetFn(instance, value); } catch (Exception ex) { var error = new RequestBindingError(); if (propertyName != null) error.PropertyName = propertyName; if (propertyTextValue != null) error.PropertyValueString = propertyTextValue; if (propertySerializerEntry != null && propertySerializerEntry.PropertyType != null) error.PropertyType = propertySerializerEntry.PropertyType; errors.Add(error); } } if (errors.Count > 0) { var serializationException = new SerializationException("Unable to bind to request '{0}'".Fmt(type.Name)); serializationException.Data.Add("errors", errors); throw serializationException; } return instance; }
private object PopulateFromKeyValue(object instance, string propertyName, string propertyTextValue, out PropertySerializerEntry propertySerializerEntry, List<RequestBindingError> errors, List<string> ignoredWarningsOnPropertyNames = null) { propertySerializerEntry = null; try { if (!propertySetterMap.TryGetValue(propertyName, out propertySerializerEntry)) { if (propertyName == "v") { int version; var hasVersion = instance as IHasVersion; if (hasVersion != null && int.TryParse(propertyTextValue, out version)) { hasVersion.Version = version; } return instance; } var ignoredProperty = propertyName.ToLowerInvariant(); if (ignoredWarningsOnPropertyNames == null || !ignoredWarningsOnPropertyNames.Contains(ignoredProperty)) { Log.WarnFormat("Property '{0}' does not exist on type '{1}'", ignoredProperty, type.FullName); } return instance; } if (propertySerializerEntry.PropertySetFn == null) { Log.WarnFormat("Could not set value of read-only property '{0}' on type '{1}'", propertyName, type.FullName); return instance; } if (propertySerializerEntry.PropertyType == typeof (bool)) { //InputExtensions.cs#530 MVC Checkbox helper emits extra hidden input field, generating 2 values, first is the real value propertyTextValue = propertyTextValue.LeftPart(','); } var value = propertySerializerEntry.PropertyParseStringFn(propertyTextValue); if (value == null) { Log.WarnFormat("Could not create instance on '{0}' for property '{1}' with text value '{2}'", instance, propertyName, propertyTextValue); return instance; } propertySerializerEntry.PropertySetFn(instance, value); } catch (Exception ex) { var error = new RequestBindingError { ErrorMessage = ex.Message }; if (propertyName != null) error.PropertyName = propertyName; if (propertyTextValue != null) error.PropertyValueString = propertyTextValue; if (propertySerializerEntry != null && propertySerializerEntry.PropertyType != null) error.PropertyType = propertySerializerEntry.PropertyType; errors.Add(error); } return instance; }