Support Nullable Types
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 25/100
- Tipo di issue
- Funzionalità
- Chiarezza
- Da chiarire
- Stato di attività
- Ferma
- Stack tecnologico
- csharp
- Ambito
- backend-api-design
Direzione di ricerca
Inizia con DevBetter.JsonExtensions.Converters.MissingMemberErrorConverter e il codice sorgente che converte manualmente i valori. Traccia come Nullable viene gestito dal gestore generale Error e dagli altri gestori. Il lavoro è completato quando i valori nullable vengono deserializzati correttamente e la policy richiesta per i membri mancanti è definita per ogni gestore, incluso l'esempio Weather.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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!
- Lingua principale
- C#
- Stelle
- 4
- Fork
- 1
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Preparare l'ambiente
Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di DevBetterCom/DevBetter.JsonExtensions
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 35/100
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 45/100
Tutte le issue di DevBetterCom/DevBetter.JsonExtensions
Issue simili
-
[中] 更新器下载缓存从不清理Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
-
Remove duplicate Vimeo DI registration; use NimblePros.Vimeo AddVimeoServices and upgrade packageAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
DevBetterCom/DevBetterWeb#1486 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
NimblePros/eShopOnWeb#333 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
Natvs/Nt.Automaton#33 ·
-
enhancement
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100