31 lines
1.2 KiB
C#
31 lines
1.2 KiB
C#
using Newtonsoft.Json;
|
|
|
|
namespace Idoklad.Infrastructure;
|
|
|
|
/// <summary>
|
|
/// Wraps an iDoklad SDK converter that only supports reading. Reading is delegated to the SDK
|
|
/// converter, while <see cref="CanWrite"/> is false, so Newtonsoft writes the value with the
|
|
/// standard member-by-member serialization instead of calling the SDK's <c>WriteJson</c>.
|
|
/// </summary>
|
|
public sealed class SdkWriteBypassJsonConverter : JsonConverter
|
|
{
|
|
private readonly JsonConverter _sdkConverter;
|
|
|
|
public SdkWriteBypassJsonConverter(JsonConverter sdkConverter)
|
|
{
|
|
_sdkConverter = sdkConverter;
|
|
}
|
|
|
|
public override bool CanRead => _sdkConverter.CanRead;
|
|
|
|
public override bool CanWrite => false;
|
|
|
|
public override bool CanConvert(Type objectType) => _sdkConverter.CanConvert(objectType);
|
|
|
|
public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
|
|
=> _sdkConverter.ReadJson(reader, objectType, existingValue, serializer);
|
|
|
|
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
|
|
=> throw new InvalidOperationException("CanWrite is false; Newtonsoft must not call this converter for writing.");
|
|
}
|