• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

loresoft / FluentRest / 13399786835

18 Feb 2025 08:48PM UTC coverage: 57.756% (-0.4%) from 58.107%
13399786835

push

github

pwelter34
add support for nullable, minor cleanup, improve UrlBuilder

293 of 646 branches covered (45.36%)

Branch coverage included in aggregate %.

238 of 380 new or added lines in 20 files covered. (62.63%)

3 existing lines in 3 files now uncovered.

824 of 1288 relevant lines covered (63.98%)

56.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.67
/src/FluentRest/JsonContentSerializer.cs
1
using System.Text;
2
using System.Text.Json;
3
using System.Text.Json.Serialization;
4

5
namespace FluentRest;
6

7
/// <summary>
8
/// A JSON content serializer.
9
/// </summary>
10
public class JsonContentSerializer : IContentSerializer
11
{
12

13
    /// <summary>
14
    /// Create a new JSON content serializer with the specified options.
15
    /// </summary>
16
    /// <param name="options">The <see cref="JsonSerializerOptions"/> instance to use.</param>
17
    public JsonContentSerializer(JsonSerializerOptions? options = null)
28✔
18
    {
19
        Options = options ?? new JsonSerializerOptions(JsonSerializerDefaults.Web)
28✔
20
        {
28✔
21
            DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault
28✔
22
        };
28✔
23
    }
28✔
24

25
    /// <summary>
26
    /// Gets or sets the JSON serializer options.
27
    /// </summary>
28
    /// <value>
29
    /// The JSON serializer options.
30
    /// </value>
31
    public JsonSerializerOptions Options { get; }
102✔
32

33
    /// <summary>
34
    /// Gets the content-type the serializer supports.
35
    /// </summary>
36
    /// <value>
37
    /// The content-type the serializer supports.
38
    /// </value>
39
    public string ContentType { get; } = "application/json";
144✔
40

41
    /// <summary>
42
    /// Serializes the specified <paramref name="data"/> object asynchronous.
43
    /// </summary>
44
    /// <param name="data">The data object to serialize.</param>
45
    /// <returns>The <see cref="HttpContent"/> that the data object serialized to.</returns>
46
    public Task<HttpContent?> SerializeAsync(object? data)
47
    {
48
        if (data is null)
4!
NEW
49
            return Task.FromResult<HttpContent?>(null);
×
50

51
        var objectType = data.GetType();
4✔
52
        var json = JsonSerializer.Serialize(data, objectType, Options);
4✔
53
        var httpContent = new StringContent(json, Encoding.UTF8, ContentType);
4✔
54

55
        return Task.FromResult<HttpContent?>(httpContent);
4✔
56
    }
57

58
    /// <summary>
59
    /// Deserialize the <see cref="HttpContent"/> asynchronously.
60
    /// </summary>
61
    /// <typeparam name="TData">The type of the data.</typeparam>
62
    /// <param name="content">The content to deserialize.</param>
63
    /// <returns>The data object deserialized from the HttpContent.</returns>
64
    public async Task<TData?> DeserializeAsync<TData>(HttpContent? content)
65
    {
66
        if (content is null)
98!
NEW
67
            return default;
×
68

69
        using var stream = await content.ReadAsStreamAsync().ConfigureAwait(false);
98✔
70
        var result = await JsonSerializer
98✔
71
            .DeserializeAsync<TData>(stream, Options)
98✔
72
            .ConfigureAwait(false);
98✔
73

74
        return result;
98✔
75
    }
98✔
76

77
    #region Singleton
78

79
    private static readonly Lazy<JsonContentSerializer> _current = new Lazy<JsonContentSerializer>(() => new JsonContentSerializer());
8✔
80

81
    /// <summary>
82
    /// Gets the current singleton instance of JsonContentSerializer.
83
    /// </summary>
84
    /// <value>The current singleton instance.</value>
85
    /// <remarks>
86
    /// An instance of JsonContentSerializer wont be created until the very first
87
    /// call to the sealed class. This is a CLR optimization that
88
    /// provides a properly lazy-loading singleton.
89
    /// </remarks>
90
    public static JsonContentSerializer Default => _current.Value;
4✔
91

92
    #endregion
93
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc