Skip to content

Support for Records #1222

@TimWhiting

Description

@TimWhiting

With the upcoming records feature, it would be good to figure out how to serialize those with this package.

I'd propose:

  • Make it an error to mix positional and named fields in an inline type
  • Allow records with only named fields - serializes to a normal JSON map with keys as the field names
  • Allow records with only positional fields - serializes to a normal JSON list (note that the list might not be homogenous)

Allow records with mixed fields with the following qualifications:

  • only via a typedef - so that we don't have to canonicalize structural types.
  • is converted to a JSON map
  • positional fields require a JsonKey annotation with a name

Potential other requirements to consider:

  • Only support JsonKey annotation with typedefs - to make it clear we don't canonicalize structural types

Note that we could probably support not requiring to go through a typedef, it just makes it more error prone for users, because they need to understand that the type is not canonicalized and the annotation would have to be added to all records.

Finally, we would need to consider what annotating a record type by itself (not embedded in an object) with JsonSerializable would look like. I think creating a toJson extension method would be a good approach. Where to put the fromJson method / how to name it is a bit more tricky.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions