Support Nullable Types
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 25/100
- Loại issue
- Tính năng
- Độ rõ ràng
- Cần làm rõ
- Mức độ hoạt động
- Đình trệ
- Công nghệ
- csharp
- Lĩnh vực
- backend-api-design
Hướng nghiên cứu
Bắt đầu với DevBetter.JsonExtensions.Converters.MissingMemberErrorConverter và mã nguồn chuyển đổi các giá trị theo cách thủ công. Theo dõi cách Nullable được Error handler tổng quát và các handler khác xử lý. Được xem là hoàn tất khi các giá trị nullable được deserialize chính xác và chính sách missing-member được yêu cầu đã được định nghĩa cho từng handler, bao gồm cả ví dụ Weather.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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!
- Ngôn ngữ chính
- C#
- Star
- 4
- Fork
- 1
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của DevBetterCom/DevBetter.JsonExtensions
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 45/100
Tất cả issue của DevBetterCom/DevBetter.JsonExtensions
Issue tương tự
-
[中] 更新器下载缓存从不清理Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
-
Remove duplicate Vimeo DI registration; use NimblePros.Vimeo AddVimeoServices and upgrade packageĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
DevBetterCom/DevBetterWeb#1486 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
NimblePros/eShopOnWeb#333 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Natvs/Nt.Automaton#33 ·
-
enhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100