Support Nullable Types
まだ誰も着手していません。
評価
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 初心者へのやさしさ
- 25/100
- issue の種類
- 機能追加
- 明瞭さ
- 説明が足りない
- 活発さ
- 停滞
- 技術スタック
- csharp
調査の方向性
DevBetter.JsonExtensions.Converters.MissingMemberErrorConverter と、値を手動で変換するソースコードから始めます。Nullable が一般的な Error ハンドラーと他のハンドラーによってどのように処理されるかを追跡します。Nullable 値が正しくデシリアライズされ、要求された missing-member policy が Weather の例を含む各ハンドラーについて定義されていれば完了です。
索引モデルが issue の本文から書いたものです。
説明
Ths is a nice extension for solving the problem, where missing values are silently ignored by default. However, I have some cases where I know beforehand that a value might be missing from the JSON. I represent these in the class I deserialise to as nullable fields.
When I try to deserialise like this:
public class Weather{
public string description { get; set; }
public double windSpeed { get; set; }
public int? temperature {get;set;}
}
var deserializeOptions = new JsonSerializerOptions()
.SetMissingMemberHandling(DevBetter.JsonExtensions.MissingMemberHandling.Error);
Weather? result = System.Text.Json.JsonSerializer.Deserialize<Weather>(text, deserializeOptions);
with this JSON:
{
"temperature": 32,
"description": "windy. So very windy",
"windSpeed": 60.05
}
I get
{System.Text.Json.JsonException: The JSON value could not be converted to System.Nullable`1[System.Int32]. Path: $ | .....
---> System.InvalidOperationException: The requested operation requires an element of type 'Object', but the target element has type 'Number'.
at System.Text.Json.JsonElement.EnumerateObject()
at DevBetter.JsonExtensions.Converters.MissingMemberErrorConverter.Read(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options)
at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
--- End of inner exception stack trace ---
Looking at the sources, it seems that you're converting types manually, and that the Nullable generic isn't eexplicitly handled yet, so this would be expected, the exception is because it can't construct a system.Nullable from a numeric value yet.
Suggested implementation:
- If not added to the general Error handler, add a new handler, that behaves like error, except that values that are explicitly nullable in the class do not cause an exception to be thrown if they are missing from the JSON.
- Add support for nullable types to all handlers, with a policy on how these should be deserialised for that handler.
Thanks!
- 主要言語
- C#
- スター
- 4
- フォーク
- 1
- PR マージ指標
- 30日以内にマージされた PR はありません
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
DevBetterCom/DevBetter.JsonExtensions のほかの issue
-
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
-
難易度 3/5 1〜2日 初心者へのやさしさ 45/100
DevBetterCom/DevBetter.JsonExtensions の issue をすべて見る
似ている issue
-
[中] 更新器下载缓存从不清理オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
-
Remove duplicate Vimeo DI registration; use NimblePros.Vimeo AddVimeoServices and upgrade packageオープン
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
DevBetterCom/DevBetterWeb#1486 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
NimblePros/eShopOnWeb#333 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
Natvs/Nt.Automaton#33 ·
-
enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100