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

loresoft / FluentRest / 9388947971

05 Jun 2024 05:56PM UTC coverage: 57.76% (+0.2%) from 57.587%
9388947971

push

github

pwelter34
fix flaky tests

277 of 614 branches covered (45.11%)

Branch coverage included in aggregate %.

847 of 1332 relevant lines covered (63.59%)

73.6 hits per line

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

92.31
/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"></param>
17
    public JsonContentSerializer(JsonSerializerOptions options = null)
42✔
18
    {
19
        Options = options ?? new JsonSerializerOptions(JsonSerializerDefaults.Web)
42✔
20
        {
42✔
21
            DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault
42✔
22
        };
42✔
23
    }
42✔
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; }
153✔
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";
216✔
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 == null)
6!
49
            return Task.FromResult<HttpContent>(null);
×
50

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

55
        return Task.FromResult<HttpContent>(httpContent);
6✔
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
        using var stream = await content.ReadAsStreamAsync().ConfigureAwait(false);
147✔
67
        var result = await JsonSerializer
147✔
68
            .DeserializeAsync<TData>(stream, Options)
147✔
69
            .ConfigureAwait(false);
147✔
70

71
        return result;
147✔
72
    }
147✔
73

74
    #region Singleton
75

76
    private static readonly Lazy<JsonContentSerializer> _current = new Lazy<JsonContentSerializer>(() => new JsonContentSerializer());
12✔
77

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

89
    #endregion
90
}
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