using AyCode.Core.Tests.TestModels; using System.Runtime.CompilerServices; using System.Text.Json; namespace AyCode.Core.Serializers.Console.Benchmarks; /// /// System.Text.Json benchmark, String I/O mode. Reference comparison — uses reflection-based metadata /// (no source-generator opt-in here). Typically NOT in the active suite (commented out in /// BenchmarkLoop.CreateSerializers); ranks far behind binary serializers on µs/op but provides /// a familiar JSON baseline when needed. /// internal sealed class SystemTextJsonBenchmark : ISerializerBenchmark { private readonly TestOrder _order; private readonly JsonSerializerOptions _options; private readonly string _serialized; private readonly byte[] _serializedUtf8; public string Engine => Configuration.EngineSystemTextJson; public string IoMode => Configuration.IoString; public string DispatchMode => Configuration.ModeRuntime; // System.Text.Json default uses reflection-based metadata (no source generator opt-in here) public string OptionsPreset { get; } public int SerializedSize => _serializedUtf8.Length; public long SetupSerializeAllocBytes => 0; public long SetupDeserializeAllocBytes => 0; public SystemTextJsonBenchmark(TestOrder order, string optionsPreset) { _order = order; OptionsPreset = optionsPreset; _options = new JsonSerializerOptions { WriteIndented = false, DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull, ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.IgnoreCycles }; _serialized = JsonSerializer.Serialize(order, _options); _serializedUtf8 = Configuration.Utf8NoBom.GetBytes(_serialized); } [MethodImpl(MethodImplOptions.NoInlining)] public void Serialize() => JsonSerializer.Serialize(_order, _options); [MethodImpl(MethodImplOptions.NoInlining)] public void Deserialize() => JsonSerializer.Deserialize(_serialized, _options); public bool VerifyRoundTrip() { var json = JsonSerializer.Serialize(_order, _options); var roundTripped = JsonSerializer.Deserialize(json, _options); return BenchmarkLoop.DeepEqualsViaJson(_order, roundTripped); } }