google / google/json_serializable.dart

Provide some means to generate from/toJson for compatible types for other code generators

Open
#1,496 4 comments 0 reactions 0 assignees View on GitHub
Type: enhancement
Dominant language
Dart
Stars
1.6k
Forks
461
Avg merge
45m
Merged PRs (30d)
1

Description

json serializable is a powerful generator. Often, other generators will want to be able to generate to/fromJson functions for its own use.

This can include retrofit, riverpod's v3 experimental `@JsonPersist()`, and plenty more, which are effectively forced to re-implement json_serializable, which will never be as good, and will always be missing features.

I noticed this issue when i tried using `@JsonPersist()` with a type that uses generic argument factories, and it failed to generate correct code. the same was true of Slugid, which does not have a fromJson constructor, and it does not have a toJson method. with jsonSerializable, i could tell json_serializable to use `Slugid(json)` and `slugid.toString()` as is proper, but attempting to support that in riverpod would be highly unreasonable, forcing me to wrap the class to use json_serializable on, which is bad API

Often, this can involve programatically supporting generic argument factories, enums, and even classes that dont directly support serialization, which could benefit from somehow applying `@JsonKey` and Converters

I'm not sure what the best way to do this is, but it would be a benefit if we could keep all serialization code centralized _here_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.