Skip to content

Commit

Permalink
introduce source generation in System.Text.Json to fix warning IL2026
Browse files Browse the repository at this point in the history
  • Loading branch information
smdn committed Apr 3, 2024
1 parent fdd13b7 commit 558db52
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ SPDX-License-Identifier: MIT

<!-- disables code style/code analysis warnings configured by Smdn.MSBuild.ProjectAssets.Library -->
<PropertyGroup>
<NoWarn>IL2026;SA1005;SA1028;SA1110;SA1306;SA1309;SA1313;SA1505;SA1508;SA1513;SA1514;SA1518;SA1616;SA1623;SA1629;SA1642;SA1649;$(NoWarn)</NoWarn>
<NoWarn>SA1005;SA1028;SA1110;SA1306;SA1309;SA1313;$(NoWarn)</NoWarn>
<NoWarn>SA1505;SA1508;SA1513;SA1514;SA1518;SA1616;SA1623;SA1629;SA1642;SA1649;$(NoWarn)</NoWarn>
</PropertyGroup>

<Target Name="GenerateReadmeFileContent">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// SPDX-FileCopyrightText: 2024 smdn <smdn@smdn.jp>
// SPDX-License-Identifier: MIT
using System.Text.Json.Serialization;

namespace Smdn.Net.EchonetLite.Appendix;

// use source generation in System.Text.Json
// ref: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/source-generation
[JsonSerializable(typeof(SpecificationMaster.SpecificationMasterJsonObject))]
[JsonSerializable(typeof(PropertyMaster))]
internal partial class JsonSerializerSourceGenerationContext : JsonSerializerContext { }
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal sealed class SpecificationMaster {
/// <summary>
/// JSONデシリアライズ用のオブジェクト
/// </summary>
private sealed class SpecificationMasterJsonObject {
internal sealed class SpecificationMasterJsonObject {
/// <summary>
/// ECHONET Lite SPECIFICATIONのバージョン
/// </summary>
Expand Down Expand Up @@ -126,7 +126,10 @@ public static SpecificationMaster GetInstance()
using var stream = GetSpecificationMasterDataStream(specificationMasterJsonFileName);

_Instance = new(
JsonSerializer.Deserialize<SpecificationMasterJsonObject>(stream) ?? throw new InvalidOperationException($"failed to deserialize {specificationMasterJsonFileName}")
JsonSerializer.Deserialize<SpecificationMasterJsonObject>(
stream,
JsonSerializerSourceGenerationContext.Default.SpecificationMasterJsonObject
) ?? throw new InvalidOperationException($"failed to deserialize {specificationMasterJsonFileName}")
);
}

Expand Down Expand Up @@ -173,7 +176,10 @@ byte classCode

// スーパークラスのプロパティを列挙
using (var stream = GetSpecificationMasterDataStream($"{classGroupSpec.SuperClassName}.json")) {
var superClassProperties = JsonSerializer.Deserialize<PropertyMaster>(stream) ?? throw new InvalidOperationException($"{nameof(PropertyMaster)} can not be null");
var superClassProperties = JsonSerializer.Deserialize<PropertyMaster>(
stream,
JsonSerializerSourceGenerationContext.Default.PropertyMaster
) ?? throw new InvalidOperationException($"{nameof(PropertyMaster)} can not be null");
properties.AddRange(superClassProperties.Properties);
}

Expand All @@ -187,7 +193,11 @@ byte classCode
// クラスのプロパティを列挙
using (var stream = GetSpecificationMasterDataStream(classGroupDirectoryName, classFileName)) {
if (stream is not null) {
var classProperties = JsonSerializer.Deserialize<PropertyMaster>(stream) ?? throw new InvalidOperationException($"{nameof(PropertyMaster)} can not be null");
var classProperties = JsonSerializer.Deserialize<PropertyMaster>(
stream,
JsonSerializerSourceGenerationContext.Default.PropertyMaster
) ?? throw new InvalidOperationException($"{nameof(PropertyMaster)} can not be null");

properties.AddRange(classProperties.Properties);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-FileCopyrightText: 2024 smdn <smdn@smdn.jp>
// SPDX-License-Identifier: MIT
using System.Text.Json.Serialization;

using Smdn.Net.EchonetLite.Protocol;

namespace Smdn.Net.EchonetLite.Serialization.Json;

// use source generation in System.Text.Json
// ref: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/source-generation
[JsonSerializable(typeof(Frame))]
internal partial class JsonSerializerSourceGenerationContext : JsonSerializerContext { }
5 changes: 4 additions & 1 deletion src/Smdn.Net.EchonetLite/Smdn.Net.EchonetLite.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ SPDX-License-Identifier: MIT

<!-- disables code style/code analysis warnings configured by Smdn.MSBuild.ProjectAssets.Library -->
<PropertyGroup>
<NoWarn>CS3001;CS3003;CS3005;IL2026;SA1000;SA1001;SA1004;SA1005;SA1024;SA1106;SA1110;SA1113;SA1122;SA1137;SA1208;SA1210;SA1309;SA1313;SA1316;SA1402;SA1413;SA1414;SA1505;SA1507;SA1508;SA1513;SA1514;SA1614;SA1623;SA1629;SA1642;$(NoWarn)</NoWarn>
<NoWarn>CS3001;CS3003;CS3005;$(NoWarn)</NoWarn>
<NoWarn>SA1000;SA1001;SA1004;SA1005;SA1024;SA1106;SA1110;SA1113;SA1122;SA1137;SA1208;SA1210;$(NoWarn)</NoWarn>
<NoWarn>SA1309;SA1313;SA1316;SA1402;SA1413;SA1414;$(NoWarn)</NoWarn>
<NoWarn>SA1505;SA1507;SA1508;SA1513;SA1514;SA1614;SA1623;SA1629;SA1642;$(NoWarn)</NoWarn>
</PropertyGroup>

<Target Name="GenerateReadmeFileContent">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.Extensions.Logging;

using Smdn.Net.EchonetLite.Protocol;
using Smdn.Net.EchonetLite.Serialization.Json;

namespace Smdn.Net.EchonetLite;

Expand Down Expand Up @@ -65,7 +66,7 @@ private void EchonetDataReceived(object? sender, (IPAddress address, ReadOnlyMem
// ECHONETLiteフレームではないため無視
return;

_logger?.LogTrace($"Echonet Lite Frame受信: address:{value.address}\r\n,{JsonSerializer.Serialize(frame)}");
_logger?.LogTrace($"Echonet Lite Frame受信: address:{value.address}\r\n,{JsonSerializer.Serialize(frame, JsonSerializerSourceGenerationContext.Default.Frame)}");

FrameReceived?.Invoke(this, (value.address, frame));
}
Expand All @@ -92,7 +93,7 @@ private async ValueTask SendFrameAsync(IPAddress? address, Action<IBufferWriter<

if (_logger is not null && _logger.IsEnabled(LogLevel.Trace)) {
if (FrameSerializer.TryDeserialize(_requestFrameBuffer.WrittenSpan, out var frame)) {
_logger.LogTrace($"Echonet Lite Frame送信: address:{address}\r\n,{JsonSerializer.Serialize(frame)}");
_logger.LogTrace($"Echonet Lite Frame送信: address:{address}\r\n,{JsonSerializer.Serialize(frame, JsonSerializerSourceGenerationContext.Default.Frame)}");
}
#if DEBUG
else {
Expand Down

0 comments on commit 558db52

Please sign in to comment.