google / json_serializable.dart

Generates utilities to aid in serializing to/from JSON.
https://pub.dev/packages/json_serializable
BSD 3-Clause "New" or "Revised" License
1.54k stars 393 forks source link

Ability to pinpoint where fromJson throws an error #1381

Closed martinralfreindl closed 7 months ago

martinralfreindl commented 7 months ago

Suppose I have a large and very nested json that represents a user. The json has tons of attributes (over 200 of them), but deeply nested in that json under user->settings->general there is a field called age, where the user's age is defined as an integer. If my backend somehow forgets to fill in this age field, json_serializable will now throw an error along the lines of:

Expected a value of type 'num', but got one of type 'Null'

It's nice that I get the error, but now I have to go find which of those 200 fields the error is from, and that takes forever. Would it be possible to improve json_serializable error message along the lines of:

Field "user->settings->general->age" expected a value of type 'num', but got one of type 'Null'

This would make it way easier for us to detect errors in large files

swayambhoo-manu commented 7 months ago

I also think this feature would be really helpful.

kevmoo commented 7 months ago

Already have it. Look at the "checked" property - https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonSerializable/checked.html

On Fri, Dec 8, 2023 at 1:49 AM Swayambhoo-Manu @.***> wrote:

I also think this feature would be really helpful.

— Reply to this email directly, view it on GitHub https://github.com/google/json_serializable.dart/issues/1381#issuecomment-1846875171, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAEFCRVPIBVAUMJS65C2BTYILPCDAVCNFSM6AAAAABAML2UCGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQNBWHA3TKMJXGE . You are receiving this because you are subscribed to this thread.Message ID: @.***>

martinralfreindl commented 7 months ago

Amazing. Thank you so much. Worked like a charm!