public Boolean AsJSONObject(String Key, out JSONWrapper JSONWrapper) { Object Value = null; if (TryGetValue(Key, out Value)) { var JSON = Value as JObject; if (JSON != null) { JSONWrapper = new JSONWrapper(JSON); } else { JSONWrapper = null; } return(true); } JSONWrapper = null; return(false); }
public static Boolean TryParseJObjectRequestBody(this HTTPRequest Request, out JSONWrapper JSON, out HTTPResponse HTTPResponse, Boolean AllowEmptyHTTPBody = false, String JSONLDContext = null) { #region AllowEmptyHTTPBody JSON = null; HTTPResponse = null; if (Request.ContentLength == 0 && AllowEmptyHTTPBody) { HTTPResponse = new HTTPResponseBuilder(Request) { HTTPStatusCode = HTTPStatusCode.OK, }; return(false); } #endregion #region Get text body var RequestBodyString = Request.GetRequestBodyAsUTF8String(HTTPContentType.JSON_UTF8, AllowEmptyHTTPBody); if (RequestBodyString.HasErrors) { HTTPResponse = RequestBodyString.Error; return(false); } #endregion #region Try to parse the JSON try { JSON = new JSONWrapper(RequestBodyString.Data); } catch (Exception e) { HTTPResponse = new HTTPResponseBuilder(Request) { HTTPStatusCode = HTTPStatusCode.BadRequest, ContentType = HTTPContentType.JSON_UTF8, Content = JSONObject.Create( JSONLDContext.IsNotNullOrEmpty() ? new JProperty("context", JSONLDContext) : null, new JProperty("description", "Invalid JSON request body!"), new JProperty("hint", e.Message) ).ToUTF8Bytes() }; return(false); } return(true); #endregion }