public TResult ParseContentDelegate <TResult>(JContainer contentJContainer, string contentString, Serialization.BindConvert bindConvert, ParameterInfo paramInfo, IApplication httpApp, IHttpRequest request, Func <object, TResult> onParsed, Func <string, TResult> onFailure) { if (contentJContainer is JObject) { var contentJObject = contentJContainer as JObject; var key = this.GetKey(paramInfo); return(ParseJsonContentDelegate(contentJObject, contentString, bindConvert, key, paramInfo, httpApp, request, onParsed, onFailure)); } if (contentJContainer is JArray) { var contentJArray = contentJContainer as JArray; var paramType = paramInfo.ParameterType; var key = this.GetKey(paramInfo); if (paramType.IsArray) { var elementType = paramType.GetElementType(); var arrayOfType = contentJArray .Select( contentJToken => { if (contentJToken is JObject) { var contentJObject = (JObject)contentJToken; if (!contentJObject.TryGetValue(key, out JToken valueToken)) { return(elementType.GetDefault()); } return(httpApp.Bind(valueToken, elementType, obj => obj, why => elementType.GetDefault())); } return(elementType.GetDefault()); }) .CastArray(elementType); return(onParsed(arrayOfType)); } } return(onFailure($"JSON Content is {contentJContainer.Type} and non-array properties can only be parsed from objects.")); }
public static TResult ParseJsonContentDelegate <TResult>(JObject contentJObject, string contentString, Serialization.BindConvert bindConvert, string key, ParameterInfo paramInfo, IApplication httpApp, IHttpRequest request, Func <object, TResult> onParsed, Func <string, TResult> onFailure) { if (!contentJObject.TryGetValue(key, out JToken valueToken)) { return(onFailure($"Key[{key}] was not found in JSON")); } try { //var tokenParser = new Serialization.JsonTokenParser(valueToken); return(httpApp.Bind(valueToken, paramInfo, obj => onParsed(obj), (why) => { // TODO: Get BindConvert to StandardJTokenBindingAttribute if (valueToken.Type == JTokenType.Object || valueToken.Type == JTokenType.Array) { try { var value = Newtonsoft.Json.JsonConvert.DeserializeObject( valueToken.ToString(), paramInfo.ParameterType, bindConvert); return onParsed(value); } catch (Newtonsoft.Json.JsonSerializationException) { throw; } } return onFailure(why); })); } catch (Exception ex) { return(onFailure(ex.Message)); } }