save progress
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace StellaOps.Scanner.CallGraph.Serialization;
|
||||
|
||||
/// <summary>
|
||||
/// System.Text.Json converter for <see cref="ImmutableArray{T}"/> to ensure default serializer options
|
||||
/// can round-trip call graph models without requiring per-call JsonSerializerOptions registration.
|
||||
/// </summary>
|
||||
public sealed class ImmutableArrayJsonConverter<T> : JsonConverter<ImmutableArray<T>>
|
||||
{
|
||||
public override ImmutableArray<T> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
if (reader.TokenType == JsonTokenType.Null)
|
||||
{
|
||||
return ImmutableArray<T>.Empty;
|
||||
}
|
||||
|
||||
var values = JsonSerializer.Deserialize<List<T>>(ref reader, options);
|
||||
if (values is null || values.Count == 0)
|
||||
{
|
||||
return ImmutableArray<T>.Empty;
|
||||
}
|
||||
|
||||
return ImmutableArray.CreateRange(values);
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, ImmutableArray<T> value, JsonSerializerOptions options)
|
||||
{
|
||||
writer.WriteStartArray();
|
||||
|
||||
var normalized = value.IsDefault ? ImmutableArray<T>.Empty : value;
|
||||
foreach (var item in normalized)
|
||||
{
|
||||
JsonSerializer.Serialize(writer, item, options);
|
||||
}
|
||||
|
||||
writer.WriteEndArray();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user