< Summary

Information
Class: InferenceEngine.Core.Worker.SystemTextJsonWorkerSerializer<T1, T2>
Assembly: InferenceEngine.Core
File(s): /home/runner/work/inference-engine-lib/inference-engine-lib/InferenceEngine.Core/Worker/WorkerSerializer.cs
Line coverage
100%
Covered lines: 10
Uncovered lines: 0
Coverable lines: 10
Total lines: 42
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
SerializeInput(...)100%11100%
DeserializeInput(...)100%22100%
SerializeOutput(...)100%11100%
DeserializeOutput(...)100%22100%

File(s)

/home/runner/work/inference-engine-lib/inference-engine-lib/InferenceEngine.Core/Worker/WorkerSerializer.cs

#LineLine coverage
 1using System;
 2using System.Text.Json;
 3
 4namespace InferenceEngine.Core.Worker;
 5
 6/// <summary>
 7/// Serializes <typeparamref name="TInput"/>/<typeparamref name="TOutput"/> across the
 8/// out-of-process IPC boundary. The default <see cref="SystemTextJsonWorkerSerializer{TInput,TOutput}"/>
 9/// handles <see cref="byte"/>[] as base64 (System.Text.Json's built-in) and POCOs as JSON.
 10/// </summary>
 11public interface IWorkerSerializer<TInput, TOutput>
 12{
 13    string SerializeInput(TInput input);
 14    TInput DeserializeInput(string serialized);
 15    string SerializeOutput(TOutput output);
 16    TOutput DeserializeOutput(string serialized);
 17}
 18
 19/// <summary>
 20/// Default <see cref="IWorkerSerializer{TInput,TOutput}"/> using <see cref="JsonSerializer"/>.
 21/// <see cref="byte"/>[] is serialized as a base64 string (System.Text.Json's built-in behavior),
 22/// so <c>byte[]</c>→<c>List&lt;T&gt;</c> engines (e.g. ONNX detection) work out of the box.
 23/// </summary>
 24public sealed class SystemTextJsonWorkerSerializer<TInput, TOutput> : IWorkerSerializer<TInput, TOutput>
 25{
 226    private static readonly JsonSerializerOptions Options = new(JsonSerializerDefaults.Web)
 227    {
 228        IncludeFields = true,
 229    };
 30
 1031    public string SerializeInput(TInput input) => JsonSerializer.Serialize(input, Options);
 32
 33    public TInput DeserializeInput(string serialized) =>
 634        JsonSerializer.Deserialize<TInput>(serialized, Options)
 635            ?? throw new InvalidOperationException("Deserialized worker input was null.");
 36
 437    public string SerializeOutput(TOutput output) => JsonSerializer.Serialize(output, Options);
 38
 39    public TOutput DeserializeOutput(string serialized) =>
 640        JsonSerializer.Deserialize<TOutput>(serialized, Options)
 641            ?? throw new InvalidOperationException("Deserialized worker output was null.");
 42}