Support Nullable Types
還沒有人認領這個 Issue。
評估
- 難度
- 5/5
- 預估耗時
- 一週以上
- 新手友好度
- 25/100
- Issue 類型
- 功能
- 描述清晰度
- 需要釐清
- 活躍度
- 停滯
- 技術堆疊
- csharp
研究方向
從 DevBetter.JsonExtensions.Converters.MissingMemberErrorConverter 和手動轉換值的原始碼開始。追蹤 Nullable 如何由一般的 Error 處理常式和其他處理常式處理。完成的標準是 nullable 值能正確反序列化,並且已為每個處理常式定義所要求的缺少成員原則,包括 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 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 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 小時 新手友好度 68/100
維護者通常 1 天內回覆
-
難度 2/5 半天 新手友好度 76/100
維護者通常 1 天內回覆
-
[中] 更新器下载缓存从不清理未關閉
難度 2/5 1-3 小時 新手友好度 72/100
-
area: settings priority: p1 type: bug
難度 2/5 1-3 小時 新手友好度 72/100
AmineChr54/umbra#408 ·
維護者通常 1 天內回覆
-
Remove duplicate Vimeo DI registration; use NimblePros.Vimeo AddVimeoServices and upgrade package未關閉
難度 2/5 1-3 小時 新手友好度 72/100
DevBetterCom/DevBetterWeb#1486 · 1 則留言 ·
維護者通常 1 天內回覆