Support Nullable Types

Đang mở
#3 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

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

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của DevBetterCom/DevBetter.JsonExtensions

Tất cả issue của DevBetterCom/DevBetter.JsonExtensions

Issue tương tự

Thêm issue về C#

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.