public Task <ITriggerData> BindAsync(CloudQueueMessage value, ValueBindingContext context) { if (value == null) { throw new ArgumentNullException("value"); } object convertedValue; try { convertedValue = JsonConvert.DeserializeObject(value.AsString, ValueType, JsonSerialization.Settings); } catch (JsonException e) { // Easy to have the queue payload not deserialize properly. So give a useful error. string msg = String.Format(CultureInfo.CurrentCulture, @"Binding parameters to complex objects (such as '{0}') uses Json.NET serialization. 1. Bind the parameter type as 'string' instead of '{0}' to get the raw values and avoid JSON deserialization, or 2. Change the queue payload to be valid json. The JSON parser failed: {1} ", _valueType.Name, e.Message); throw new InvalidOperationException(msg); } IValueProvider provider = new QueueMessageValueProvider(value, convertedValue, ValueType); IReadOnlyDictionary <string, object> bindingData = (_bindingDataProvider != null) ? _bindingDataProvider.GetBindingData(convertedValue) : null; return(Task.FromResult <ITriggerData>(new TriggerData(provider, bindingData))); }
public void GetBindingData_IfComplexDataType_ReturnsBindingDataWithAllTypes() { // Arrange IBindingDataProvider provider = BindingDataProvider.FromType(typeof(ComplexDataType)); // When JSON is a structured object, we can extract the fields as route parameters. string json = @"{ ""a"":1, ""b"":[1,2,3], ""c"":{} }"; object value = JsonConvert.DeserializeObject(json, typeof(ComplexDataType)); // Act var bindingData = provider.GetBindingData(value); // Assert Assert.NotNull(bindingData); // Only take simple types Assert.Equal(3, bindingData.Count); Assert.Equal(1, bindingData["a"]); Assert.Equal(new int[] { 1, 2, 3 }, bindingData["b"]); Assert.Equal(new Dictionary <string, object>(), bindingData["c"]); }
internal override object Convert(TMessage value, Dictionary <string, object> bindingData) { string json = this.ConvertToString(value); object obj; try { obj = JsonConvert.DeserializeObject(json, this._elementType); } catch (JsonException e) { // Easy to have the queue payload not deserialize properly. So give a useful error. string msg = string.Format(CultureInfo.CurrentCulture, @"Binding parameters to complex objects (such as '{0}') uses Json.NET serialization. 1. Bind the parameter type as 'string' instead of '{0}' to get the raw values and avoid JSON deserialization, or 2. Change the queue payload to be valid json. The JSON parser failed: {1} ", this._elementType.Name, e.Message); throw new InvalidOperationException(msg); } if (bindingData != null && _provider != null) { var pocoData = _provider.GetBindingData(obj); foreach (var kv in pocoData) { string propName = kv.Key; object propVal = kv.Value; bindingData[propName] = propVal; } } return(obj); }
public void GetBindingData_IfSimpleDataType_ReturnsValidBindingData() { // Arrange IBindingDataProvider provider = BindingDataProvider.FromType(typeof(SimpleDataType)); // When JSON is a structured object, we can extract the fields as route parameters. string json = @"{ ""Name"" : 12, ""other"" : 13 }"; object value = JsonConvert.DeserializeObject(json, typeof(SimpleDataType)); // Act var bindingData = provider.GetBindingData(value); // Assert Assert.NotNull(bindingData); Assert.Equal(1, bindingData.Count); Assert.Equal(12, bindingData["Name"]); }
public Task <ITriggerData> BindAsync(object value, ValueBindingContext context) { IReadOnlyList <Document> triggerValue; try { triggerValue = value as IReadOnlyList <Document>; } catch (Exception ex) { throw new InvalidOperationException("Unable to convert trigger to DocumentDBTrigger:" + ex.Message); } var valueBinder = new CosmosDBTriggerValueBinder(_parameter.ParameterType, triggerValue); return(Task.FromResult <ITriggerData>(new TriggerData(valueBinder, _bindingDataProvider.GetBindingData(valueBinder.GetValue())))); }
public void GetBindingData_WithDerivedValue_ReturnsValidBindingData() { // Arrange IBindingDataProvider provider = BindingDataProvider.FromType(typeof(Base)); Derived value = new Derived { A = 1, B = 2 }; // Act var bindingData = provider.GetBindingData(value); // Assert Assert.NotNull(bindingData); // Get binding data for the type used when creating the provider Assert.Equal(1, bindingData.Count); Assert.Equal(1, bindingData["a"]); }
public void GetBindingData_IfDateProperty_ReturnsValidBindingData() { // Arrange IBindingDataProvider provider = BindingDataProvider.FromType(typeof(DataTypeWithDateProperty)); // Dates with JSON can be tricky. Test Date serialization. DateTime date = new DateTime(1950, 6, 1, 2, 3, 30); var json = JsonConvert.SerializeObject(new { date = date }); object value = JsonConvert.DeserializeObject(json, typeof(DataTypeWithDateProperty)); // Act var bindingData = provider.GetBindingData(value); // Assert Assert.NotNull(bindingData); Assert.Equal(1, bindingData.Count); Assert.Equal(date, bindingData["date"]); }
public async Task <ITriggerData> BindAsync(object value, ValueBindingContext context) { IValueProvider valueProvider = null; IReadOnlyDictionary <string, object> bindingData = null; string invokeString = value?.ToString(); if (_isUserTypeBinding) { if (value != null && value.GetType() != _parameter.ParameterType) { if (value is string) { value = JsonConvert.DeserializeObject((string)value, _parameter.ParameterType); } else { throw new InvalidOperationException($"Unable to bind value to type {_parameter.ParameterType}"); } } valueProvider = new SimpleValueProvider(_parameter.ParameterType, value, invokeString); if (_bindingDataProvider != null) { // binding data is defined by the user type // the provider might be null if the Type is invalid, or if the Type // has no public properties to bind to bindingData = _bindingDataProvider.GetBindingData(await valueProvider.GetValueAsync()); } } else { valueProvider = new SimpleValueProvider(_parameter.ParameterType, value, invokeString); var bindingDataTemp = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase); Utility.ApplyBindingData(value, bindingDataTemp); bindingData = bindingDataTemp; } return(new TriggerData(valueProvider, bindingData)); }
public async Task <ITriggerData> BindAsync(BrokeredMessage value, ValueBindingContext context) { IValueProvider provider; BrokeredMessage clone = value.Clone(); TInput contents = await GetBody(value, context); if (contents == null) { provider = await BrokeredMessageValueProvider.CreateAsync(clone, null, ValueType, context.CancellationToken); return(new TriggerData(provider, null)); } provider = await BrokeredMessageValueProvider.CreateAsync(clone, contents, ValueType, context.CancellationToken); IReadOnlyDictionary <string, object> bindingData = (_bindingDataProvider != null) ? _bindingDataProvider.GetBindingData(contents) : null; return(new TriggerData(provider, bindingData)); }