public object PopulateFromMap(object instance, IDictionary <string, string> keyValuePairs, List <string> ignoredWarningsOnPropertyNames = null) { var errors = new List <RequestBindingError>(); PropertySerializerEntry propertySerializerEntry = null; if (instance == null) { instance = type.CreateInstance(); } foreach (var pair in keyValuePairs) { if (!string.IsNullOrEmpty(pair.Value)) { instance = PopulateFromKeyValue(instance, pair.Key, pair.Value, out propertySerializerEntry, errors, ignoredWarningsOnPropertyNames); } } if (errors.Count > 0) { var serializationException = new SerializationException($"Unable to bind to request '{type.Name}'"); serializationException.Data.Add("errors", errors); throw serializationException; } return(instance); }
public StringMapTypeDeserializer(Type type) { this.type = type; foreach (var propertyInfo in type.GetSerializableProperties()) { var propertySetFn = JsvDeserializeType.GetSetPropertyMethod(type, propertyInfo); var propertyType = propertyInfo.PropertyType; var propertyParseStringFn = GetParseFn(propertyType); var propertySerializer = new PropertySerializerEntry(propertySetFn, propertyParseStringFn) { PropertyType = propertyType }; var attr = propertyInfo.FirstAttribute<DataMemberAttribute>(); if (attr != null && attr.Name != null) { propertySetterMap[attr.Name] = propertySerializer; } propertySetterMap[propertyInfo.Name] = propertySerializer; } if (JsConfig.IncludePublicFields) { foreach (var fieldInfo in type.GetSerializableFields()) { var fieldSetFn = JsvDeserializeType.GetSetFieldMethod(type, fieldInfo); var fieldType = fieldInfo.FieldType; var fieldParseStringFn = JsvReader.GetParseFn(fieldType); var fieldSerializer = new PropertySerializerEntry(fieldSetFn, fieldParseStringFn) {PropertyType = fieldType}; propertySetterMap[fieldInfo.Name] = fieldSerializer; } } }
public StringMapTypeDeserializer(Type type) { this.type = type; if (type.IsOrHasGenericInterfaceTypeOf(typeof(IEnumerable <>))) { return; } foreach (var propertyInfo in type.GetProperties()) { var propertySetFn = JsvDeserializeType.GetSetPropertyMethod(type, propertyInfo); var propertyType = propertyInfo.PropertyType; var propertyParseStringFn = JsvReader.GetParseFn(propertyType); var propertySerializer = new PropertySerializerEntry(propertySetFn, propertyParseStringFn) { PropertyType = propertyType }; var attr = propertyInfo.FirstAttribute <DataMemberAttribute>(); if (attr != null && attr.Name != null) { propertySetterMap[attr.Name] = propertySerializer; } propertySetterMap[propertyInfo.Name] = propertySerializer; } }
public object PopulateFromMap(object instance, INameValueCollection nameValues, List <string> ignoredWarningsOnPropertyNames = null) { var errors = new List <RequestBindingError>(); PropertySerializerEntry propertySerializerEntry = null; if (instance == null) { instance = type.CreateInstance(); } foreach (var key in nameValues.AllKeys) { string value = nameValues[key]; if (!string.IsNullOrEmpty(value)) { instance = PopulateFromKeyValue(instance, key, value, out propertySerializerEntry, errors, ignoredWarningsOnPropertyNames); } } if (errors.Count > 0) { var serializationException = new SerializationException($"Unable to bind to request '{type.Name}'"); serializationException.Data.Add("errors", errors); throw serializationException; } return(instance); }
public object PopulateFromMap(object instance, IDictionary <string, string> keyValuePairs) { string propertyName = null; string propertyTextValue = null; PropertySerializerEntry propertySerializerEntry = null; try { if (instance == null) { instance = type.CreateInstance(); } foreach (var pair in keyValuePairs) { propertyName = pair.Key; propertyTextValue = pair.Value; if (!propertySetterMap.TryGetValue(propertyName, out propertySerializerEntry)) { if (propertyName != "format" && propertyName != "callback" && propertyName != "debug") { Log.WarnFormat("Property '{0}' does not exist on type '{1}'", propertyName, type.FullName); } continue; } 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); } return(instance); } catch (Exception ex) { var serializationException = new SerializationException("KeyValueDataContractDeserializer: Error converting to type: " + ex.Message, ex); if (propertyName != null) { serializationException.Data.Add("propertyName", propertyName); } if (propertyTextValue != null) { serializationException.Data.Add("propertyValueString", propertyTextValue); } if (propertySerializerEntry != null && propertySerializerEntry.PropertyType != null) { serializationException.Data.Add("propertyType", propertySerializerEntry.PropertyType); } throw serializationException; } }
public StringMapTypeDeserializer(Type type) { this.type = type; foreach (var propertyInfo in type.GetProperties()) { var propertySetFn = JsvDeserializeType.GetSetPropertyMethod(type, propertyInfo); var propertyParseStringFn = JsvReader.GetParseFn(propertyInfo.PropertyType); var propertySerializer = new PropertySerializerEntry(propertySetFn, propertyParseStringFn); propertySetterMap.Add(propertyInfo.Name, propertySerializer); } }
public object PopulateFromMap(object instance, IDictionary <string, string> keyValuePairs) { string propertyName = null; string propertyTextValue = null; PropertySerializerEntry propertySerializerEntry = null; if (instance == null) { instance = _CreateInstanceFn(type); } foreach (var pair in keyValuePairs) { propertyName = pair.Key; propertyTextValue = pair.Value; if (string.IsNullOrEmpty(propertyTextValue)) { continue; } if (!propertySetterMap.TryGetValue(propertyName, out propertySerializerEntry)) { if (propertyName == "v") { continue; } continue; } if (propertySerializerEntry.PropertySetFn == null) { 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 = LeftPart(propertyTextValue, ','); } var value = propertySerializerEntry.PropertyParseStringFn(propertyTextValue); if (value == null) { continue; } propertySerializerEntry.PropertySetFn(instance, value); } return(instance); }
public StringMapTypeDeserializer(Func <Type, object> createInstanceFn, Func <Type, Func <string, object> > getParseFn, Type type) { _CreateInstanceFn = createInstanceFn; _GetParseFn = getParseFn; this.type = type; foreach (var propertyInfo in RestPath.GetSerializableProperties(type)) { var propertySetFn = TypeAccessor.GetSetPropertyMethod(type, propertyInfo); var propertyType = propertyInfo.PropertyType; var propertyParseStringFn = GetParseFn(propertyType); var propertySerializer = new PropertySerializerEntry(propertySetFn, propertyParseStringFn, propertyType); propertySetterMap[propertyInfo.Name] = propertySerializer; } }
public StringMapTypeDeserializer(Type type) { this.type = type; if (type.IsOrHasGenericInterfaceTypeOf(typeof(IEnumerable <>))) { return; } foreach (var propertyInfo in type.GetProperties()) { var propertySetFn = JsvDeserializeType.GetSetPropertyMethod(type, propertyInfo); var propertyParseStringFn = JsvReader.GetParseFn(propertyInfo.PropertyType); var propertySerializer = new PropertySerializerEntry(propertySetFn, propertyParseStringFn); propertySetterMap.Add(propertyInfo.Name, propertySerializer); } }
public StringMapTypeDeserializer(Type type) { this.type = type; foreach (var propertyInfo in type.GetSerializableProperties()) { var propertySetFn = JsvDeserializeType.GetSetPropertyMethod(type, propertyInfo); var propertyType = propertyInfo.PropertyType; var propertyParseStringFn = GetParseFn(propertyType); var propertySerializer = new PropertySerializerEntry(propertySetFn, propertyParseStringFn) { PropertyType = propertyType }; var attr = propertyInfo.FirstAttribute <DataMemberAttribute>(); if (attr != null && attr.Name != null) { propertySetterMap[attr.Name] = propertySerializer; } // Enhanced by William var attr2 = propertyInfo.FirstAttribute <XmlElementAttribute>(); if (attr2 != null && attr2.ElementName != null) { propertySetterMap[attr2.ElementName] = propertySerializer; } propertySetterMap[propertyInfo.Name] = propertySerializer; } if (JsConfig.IncludePublicFields) { foreach (var fieldInfo in type.GetSerializableFields()) { var fieldSetFn = JsvDeserializeType.GetSetFieldMethod(type, fieldInfo); var fieldType = fieldInfo.FieldType; var fieldParseStringFn = JsvReader.GetParseFn(fieldType); var fieldSerializer = new PropertySerializerEntry(fieldSetFn, fieldParseStringFn) { PropertyType = fieldType }; propertySetterMap[fieldInfo.Name] = fieldSerializer; } } }
public StringMapTypeDeserializer(Type type) { this.type = type; if (type.IsOrHasGenericInterfaceTypeOf(typeof(IEnumerable<>))) return; foreach (var propertyInfo in type.GetProperties()) { var propertySetFn = JsvDeserializeType.GetSetPropertyMethod(type, propertyInfo); var propertyParseStringFn = JsvReader.GetParseFn(propertyInfo.PropertyType); var propertySerializer = new PropertySerializerEntry(propertySetFn, propertyParseStringFn); var attr = propertyInfo.FirstAttribute<DataMemberAttribute>(); if (attr != null && attr.Name != null) { propertySetterMap[attr.Name] = propertySerializer; } propertySetterMap[propertyInfo.Name] = propertySerializer; } }
public StringMapTypeDeserializer(Type type) { this.type = type; foreach (var propertyInfo in type.GetSerializableProperties()) { var propertySetFn = TypeAccessor.GetSetPropertyMethod(type, propertyInfo); var propertyType = propertyInfo.PropertyType; var propertyParseStringFn = GetParseFn(propertyType); var propertySerializer = new PropertySerializerEntry(propertySetFn, propertyParseStringFn) { PropertyType = propertyType }; var attr = propertyInfo.AllAttributes <DataMemberAttribute>().FirstOrDefault(); if (attr != null && attr.Name != null) { propertySetterMap[attr.Name] = propertySerializer; } propertySetterMap[propertyInfo.Name] = propertySerializer; } }
public StringMapTypeDeserializer(Type type) { this.type = type; foreach (var propertyInfo in type.GetSerializableProperties()) { var propertySetFn = propertyInfo.CreateSetter(); var propertyType = propertyInfo.PropertyType; var propertyParseStringFn = GetParseFn(propertyType); var propertySerializer = new PropertySerializerEntry(propertySetFn, propertyParseStringFn) { PropertyType = propertyType }; var attr = propertyInfo.FirstAttribute <DataMemberAttribute>(); if (attr?.Name != null) { propertySetterMap[attr.Name] = propertySerializer; } propertySetterMap[propertyInfo.Name] = propertySerializer; } if (JsConfig.IncludePublicFields) { foreach (var fieldInfo in type.GetSerializableFields()) { var fieldSetFn = fieldInfo.CreateSetter(); var fieldType = fieldInfo.FieldType; var fieldParseStringFn = JsvReader.GetParseFn(fieldType); var fieldSerializer = new PropertySerializerEntry(fieldSetFn, fieldParseStringFn) { PropertyType = fieldType }; propertySetterMap[fieldInfo.Name] = fieldSerializer; } } }
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) #endif { string propertyName = null; string propertyTextValue = null; PropertySerializerEntry propertySerializerEntry = null; try { if (instance == null) { instance = type.CreateInstance(); } foreach (var pair in keyValuePairs.Where(x => !string.IsNullOrEmpty(x.Value))) { propertyName = pair.Key; propertyTextValue = pair.Value; if (!propertySetterMap.TryGetValue(propertyName, out propertySerializerEntry)) { 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 (Type.GetTypeCode(propertySerializerEntry.PropertyType) == TypeCode.Boolean) { //InputExtensions.cs#530 MVC Checkbox helper emits extra hidden input field, generating 2 values, first is the real value propertyTextValue = propertyTextValue.SplitOnFirst(',').First(); } 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); } return(instance); } catch (Exception ex) { var serializationException = new SerializationException("KeyValueDataContractDeserializer: Error converting to type: " + ex.Message, ex); #if !NETCF if (propertyName != null) { serializationException.Data.Add("propertyName", propertyName); } if (propertyTextValue != null) { serializationException.Data.Add("propertyValueString", propertyTextValue); } if (propertySerializerEntry != null && propertySerializerEntry.PropertyType != null) { serializationException.Data.Add("propertyType", propertySerializerEntry.PropertyType); } #endif throw serializationException; } }
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); }
public object PopulateFromMap(object instance, IDictionary <string, string> keyValuePairs, List <string> ignoredWarningsOnPropertyNames = null) { string propertyName = null; string propertyTextValue = null; PropertySerializerEntry propertySerializerEntry = null; try { if (instance == null) { instance = type.CreateInstance(); } foreach (var pair in keyValuePairs.Where(x => !string.IsNullOrEmpty(x.Value))) { propertyName = pair.Key; propertyTextValue = pair.Value; if (!propertySetterMap.TryGetValue(propertyName, out propertySerializerEntry)) { 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; } 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); } return(instance); } catch (Exception ex) { var serializationException = new SerializationException("KeyValueDataContractDeserializer: Error converting to type: " + ex.Message, ex); if (propertyName != null) { serializationException.Data.Add("propertyName", propertyName); } if (propertyTextValue != null) { serializationException.Data.Add("propertyValueString", propertyTextValue); } if (propertySerializerEntry != null && propertySerializerEntry.PropertyType != null) { serializationException.Data.Add("propertyType", propertySerializerEntry.PropertyType); } throw serializationException; } }
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); }