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

Use JsonConverter converters? #2

Open
tgrushka opened this issue Dec 11, 2023 · 1 comment
Open

Use JsonConverter converters? #2

tgrushka opened this issue Dec 11, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@tgrushka
Copy link

I was interested in this package until I discovered that the use of JsonConverter classes/annotations on custom fields does not work. JsonSerializable requires specifying converters for fields that have types that are not built into json_serializable. For example, using a type like UuidValue in a field, I'd like to convert it to/from String.

I can do this with freezed, but freezed also does not allow specifying this globally for types or on classes (one has to add the converter to every field). Would give your package an advantage to be able to specify custom converters at the class level and also in build.yaml (which neither freezed nor json_serializable offers).

Example:

class UuidValueConverter extends JsonConverter<UuidValue, String> {
  const UuidValueConverter();

  @override
  UuidValue fromJson(String json) => UuidValue.fromString(json);

  @override
  String toJson(UuidValue object) => object.uuid;
}

@Morphy(converters: [UuidValueConverter()])
abstract class $Pet {
  UuidValue? get id;
  String get name;
}

or build.yaml:

targets:
  $default:
    builders:
      morphy:
        options:
          converters:
            - UuidValueConverter
@atreeon
Copy link
Owner

atreeon commented Dec 12, 2023

Hi @tgrushka, many thanks for trying out the package. You are right this would be beneficial. I'll take a look at this sometime this week and update it. I only recently added the json package and functionality (it is the main reason why I sat on the package for so long) so that part is the area where functionality is likely missing. I think this should be fairly straight forward though.

@atreeon atreeon added the enhancement New feature or request label Dec 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants