Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't custom JsonConverter with constructor arguments. Error "Generators with constructor arguments are not supported" #1373

Open
phuongvt opened this issue Nov 18, 2023 · 8 comments

Comments

@phuongvt
Copy link

When I write DateTimeConverter extends from JsonConverter with constructor arguments. Then use

  @JsonKey(name: 'time')
  @DateTimeConverter(pattern: 'YYYY/MM/dd')
  final DateTime time;

run build_runner build print error "Generators with constructor arguments are not supported"

phuongvt added a commit to phuongvt/json_serializable.dart that referenced this issue Nov 18, 2023
Support Issue: Can't custom JsonConverter with constructor arguments google#1373
phuongvt added a commit to phuongvt/json_serializable.dart that referenced this issue Nov 18, 2023
Issue: Can't custom JsonConverter with constructor arguments [google#1373]
@faisalansari0367
Copy link

@phuongvt Thanks for this awesome PR.
When can we expect this into release?

@faisalansari0367
Copy link

For now If anyone wants to use a same JsonConverter for two purpose You can use a named constructor.
For example:

Screenshot 2023-11-27 at 8 26 52 PM

And you can it use it as:
Screenshot 2023-11-27 at 8 27 03 PM

@phuongvt
Copy link
Author

@faisalansari0367 : Thank you for your support!

First of all, sorry for the late reply.
In my case, I need to define many parameters in JsonConverter structure, and the parameters have too many different values because they are of type String => there can be many "named constructors" that need to be created without rarely reused. For example:

pattern= 'YYYY/MM/dd'
pattern= 'YYYY/MM/dd HH:mm:ss'
pattern= 'YYYY/MM/dd HH:mm'
pattern= 'YYYY-MM-dd'
...

so I still find "Generators with constructor arguments" necessary. I need MR as soon as possible to support my projects

Thank you!

@kevmoo
Copy link
Collaborator

kevmoo commented Jan 16, 2024

Have you considered adding named constructors with the values pre-populated? That should work.

@phuongvt
Copy link
Author

@kevmoo yes, I was considering creating 'named constructors' that are often reused. However, I still think supporting 'Generators with constructor arguments' is necessary

@kevmoo
Copy link
Collaborator

kevmoo commented Jan 16, 2024

@kevmoo yes, I was considering creating 'named constructors' that are often reused. However, I still think supporting 'Generators with constructor arguments' is necessary

This will add non-trivial complexity to this package, sadly.

@phuongvt
Copy link
Author

phuongvt commented Jan 17, 2024

@kevmoo What do you think if I create a new class ArgumentsJsonConverter. JsonConverter I will throw an exception "Use ArgumentsJsonConverter to support constructor arguments or define named constructors (#1373)" instead of "Generators with constructor arguments are not supported"

@MoralCode
Copy link

i believe I have found a convenient enough workaround.

in your Converter:

  const DecimalIntConverter.tenths() : this(places: -1);
  const DecimalIntConverter({this.places = 0, this.base = 10});

in your type:

   @DecimalIntConverter.tenths() double someValue;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants