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

[Question/Feature] how set converter globally and override builtin

Open gemunet opened this issue 1 year ago • 4 comments

How can I set a converter globally to replace the builtin datetime for example?

this does not appear in the documentation

https://pub.dev/packages/json_serializable

replace the datetime converter with a custom one globally for all classes

gemunet avatar Jan 26 '23 00:01 gemunet

i also need this feature to convert all datetime to local time

Ali1Ammar avatar Feb 06 '23 10:02 Ali1Ammar

there is not an easy way to do this, sadly. At least not yet. Will flag this as a feature request.

kevmoo avatar Feb 06 '23 21:02 kevmoo

there is not an easy way to do this, sadly. At least not yet. Will flag this as a feature request.

Don't converters solve this problem already? For example:

class MyJsonSerializable extends JsonSerializable {
	const MyJsonSerializable({
		super.fieldRename,
		// etc., forward all props
	}) : super(converters: [MyGlobalConverter()]);
}

Then just always use @MyJsonSerializable instead of @JsonSerializable

shilangyu avatar May 16 '23 18:05 shilangyu

there is not an easy way to do this, sadly. At least not yet. Will flag this as a feature request.

Don't converters solve this problem already? For example:

class MyJsonSerializable extends JsonSerializable {
	const MyJsonSerializable({
		super.fieldRename,
		// etc., forward all props
	}) : super(converters: [MyGlobalConverter()]);
}

Then just always use @MyJsonSerializable instead of @JsonSerializable

This is all right, but it would be even better if it was just like the in-built types like DateTime : you have nothing to specify. So for custom class on a project, you would just have to specify converter once globally, and nowhere else. That would be very handy !

Nico04 avatar Aug 04 '23 08:08 Nico04