< Summary

Information
Class: Cff.Effect.Json.Json
Assembly: Cff.Effect.Json
File(s): /home/runner/work/Cff.Effect/Cff.Effect/src/Cff.Effect.Json/Json.cs
Tag: 43_3213223096
Line coverage
56%
Covered lines: 13
Uncovered lines: 10
Coverable lines: 23
Total lines: 42
Line coverage: 56.5%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Method coverage is only available for sponsors.

Upgrade to PRO version

Coverage History

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
get_JsonSerializerOptions()100%1100%
Serialize(...)100%1100%
DeserializeAsync(...)100%10%
Deserialize(...)100%1100%

File(s)

/home/runner/work/Cff.Effect/Cff.Effect/src/Cff.Effect.Json/Json.cs

#LineLine coverage
 1using System.Reflection;
 2using System.Text.Json;
 3using Cff.Effect.Abstractions;
 4
 5namespace Cff.Effect.Json;
 6
 147internal readonly record struct Json(JsonSerializerOptions JsonSerializerOptions) : IJson
 8{
 9    public string Serialize<T>(T msg) where T : notnull
 510    {
 511        var assembly = msg.GetType().Assembly.GetName().Name;
 512        var type = msg.GetType().FullName!;
 13
 514        return @$"{{""A"":""{assembly}"",""T"":""{type}"",""V"":{JsonSerializer.Serialize(msg, JsonSerializerOptions)}}}
 515    }
 16
 17    public Task<object?> DeserializeAsync(Stream stream, CancellationToken ct)
 018    {
 019        var q = (JsonSerializerOptions option) =>
 020            from doc in JsonDocument.ParseAsync(stream, cancellationToken: ct)
 021            let root = doc.RootElement
 022            let assembly = root.GetProperty("A").GetString()
 023            let typeName = root.GetProperty("T").GetString()
 024            let type = Assembly.Load(assembly).GetType(typeName)
 025            select root.GetProperty("V").Deserialize(type, option);
 26
 027        return q(JsonSerializerOptions);
 028    }
 29
 30    public object? Deserialize(string jsonString)
 231    {
 232        var root = JsonDocument.Parse(jsonString).RootElement;
 33
 234        var assembly = root.GetProperty("A").GetString();
 235        var typeName = root.GetProperty("T").GetString();
 36
 237        var type = Assembly.Load(assembly!).GetType(typeName!);
 38
 239        return root.GetProperty("V").Deserialize(type!, JsonSerializerOptions);
 240    }
 41}
 42