json_serializable.dart icon indicating copy to clipboard operation
json_serializable.dart copied to clipboard

How to exclude field from toJson serialization?

Open AlexanderBykin opened this issue 4 years ago • 1 comments

Hi

Currently JsonKey annotation have ignore parameter, but it's generalized for both fromJson and toJson. The idea is to have a control over which fields can be serialized via toJson method and then sent over internet.

Could there be implemented additional parameter bool? excludeToJson ?

AlexanderBykin avatar Feb 17 '22 14:02 AlexanderBykin

I'd actually like the above and one step further.. I have a backend SQL database that on Inserts has some Not Null with default database fields. So whilst I'm using this solution, toJson converts to a null value as it should when it's a nullable field, but if it's null on the data side I'd like to exclude the field in the toJson.. So I need the option above for things like the id field that's automatically generated on insert, but also an option to dynamically exclude fields based on their value.

JulianSwales avatar Mar 16 '22 14:03 JulianSwales

Duplicate of #569

Trying to align all of these requests into one place!

kevmoo avatar Nov 17 '22 23:11 kevmoo

I have a (draft) PR out on this. I'd LOVE (early) feedback about if this works for what folks need here: https://github.com/google/json_serializable.dart/pull/1256

I still need to do some documentation and testing...

kevmoo avatar Nov 22 '22 01:11 kevmoo

@JulianSwales – please open a separate issue for your request!

kevmoo avatar Nov 22 '22 01:11 kevmoo