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

Not clear how to apply custom json serializable converter #240

Closed
SandroMaglione opened this issue Aug 3, 2020 · 3 comments
Closed

Not clear how to apply custom json serializable converter #240

SandroMaglione opened this issue Aug 3, 2020 · 3 comments

Comments

@SandroMaglione
Copy link
Contributor

I am trying to apply my custom json converter. The README states that it is possible to add a custom json serializable converter to a model created with freezed, but it does not seem to provide an example on how to actually apply it to a model.

I tried different strategies, but all seem unsuccessful.

// 1. Outside the model

@freezed
@ModelConverter()
abstract class Model with _$Model {
  const factory Model({
    String name,
    String surname,
  }) = _Model;

  factory Model.fromJson(Map<String, dynamic> json) => _$ModelFromJson(json);
}
// 2. On the factory constructor

@freezed
abstract class Model with _$Model {
  @ModelConverter()
  const factory Model({
    String name,
    String surname,
  }) = _Model;

  factory Model.fromJson(Map<String, dynamic> json) => _$ModelFromJson(json);
}
// 3. On the factory fromJson method

@freezed
abstract class Model with _$Model {
  const factory Model({
    String name,
    String surname,
  }) = _Model;

  @ModelConverter()
  factory Model.fromJson(Map<String, dynamic> json) => _$ModelFromJson(json);
}

As stated, none of this solutions seem to work.

How can I apply my custom converter such that toJson and fromJson generated with json_serializable utilize my custom converter?

@rrousselGit
Copy link
Owner

rrousselGit commented Aug 3, 2020

The decorator isn't placed on the Model class, but the places where Model is used:

@freezed
abstract class Another with _$Another {
  const factory Another({
    @ModelConverter Model model,
  }) = _Another;

  factory Anotherl.fromJson(Map<String, dynamic> json) => _$AnotherFromJson(json);
}

@SandroMaglione
Copy link
Contributor Author

I see, now it works. Thanks.

I was having doubts because the Model inside Another class was contained in a List, so I thought it would not work.

@freezed
abstract class Another with _$Another {
  const factory Another({
    // Adding the decorator with a list works as well!
    @ModelConverter List<Model> modelList,
  }) = _Another;

  factory Another.fromJson(Map<String, dynamic> json) => _$AnotherFromJson(json);
}

Do you think it would be useful to add this example to the docs (both for the plain Model and List examples)? Should I open a PR?

@rrousselGit
Copy link
Owner

Sure, feel free to make a PR

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

2 participants