/// <summary> /// Parses the JSON as a given type from the request body string. /// </summary> /// <typeparam name="T">The type of specified object type.</typeparam> /// <param name="requestBody">The request body.</param> /// <returns> /// A string that represents the json as a given type from the request body string. /// </returns> public static T ParseJson <T>(this string requestBody) where T : class { return(requestBody == null ? null : Json.Deserialize <T>(requestBody)); }
/// <summary> /// Post a object as JSON with optional authorization token. /// </summary> /// <typeparam name="T">The type of response object.</typeparam> /// <param name="url">The URL.</param> /// <param name="payload">The payload.</param> /// <param name="authorization">The authorization.</param> /// <param name="ct">The cancellation token.</param> /// <returns>A task with a result of the requested type.</returns> public static async Task <T> Post <T>(string url, object payload, string authorization = null, CancellationToken ct = default) { var jsonString = await PostString(url, payload, authorization, ct); return(!string.IsNullOrEmpty(jsonString) ? Json.Deserialize <T>(jsonString) : default);
/// <summary> /// Outputs async a Json Response given a data object. /// </summary> /// <param name="context">The context.</param> /// <param name="data">The data.</param> /// <returns>A <c>true</c> value of type ref=JsonResponseAsync".</returns> public static Task <bool> JsonResponseAsync(this HttpListenerContext context, object data) { return(context.JsonResponseAsync(Json.Serialize(data))); }