| | | 1 | | using System.Net.Mime; |
| | | 2 | | using System.Text; |
| | | 3 | | using Newtonsoft.Json; |
| | | 4 | | |
| | | 5 | | namespace ArturRios.Util.Http; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Provides HTTP-related extension helpers for serializing objects to HTTP content. |
| | | 9 | | /// </summary> |
| | | 10 | | public static class HttpExtensions |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Adds JSON serialization helpers to <see cref="object"/> instances for HTTP requests. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <param name="object">The target object to extend.</param> |
| | | 16 | | extension(object @object) |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// Serializes the current object instance to JSON and wraps it in a <see cref="StringContent"/> |
| | | 20 | | /// with UTF-8 encoding and <see cref="MediaTypeNames.Application.Json"/> media type. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <returns> |
| | | 23 | | /// A <see cref="StringContent"/> instance containing the JSON representation of the object. |
| | | 24 | | /// </returns> |
| | | 25 | | public StringContent ToJsonStringContent() |
| | 3 | 26 | | { |
| | 3 | 27 | | var json = JsonConvert.SerializeObject(@object); |
| | | 28 | | |
| | 3 | 29 | | return new StringContent(json, Encoding.UTF8, MediaTypeNames.Application.Json); |
| | | 30 | | } |
| | | 31 | | } |
| | | 32 | | } |