Skip to content

Added createJsonKeys#1401

Merged
kevmoo merged 13 commits intogoogle:masterfrom
ChreSyr:create_json_keys
Mar 3, 2024
Merged

Added createJsonKeys#1401
kevmoo merged 13 commits intogoogle:masterfrom
ChreSyr:create_json_keys

Conversation

@ChreSyr
Copy link
Copy Markdown
Contributor

@ChreSyr ChreSyr commented Feb 20, 2024

See #1400

@google-cla
Copy link
Copy Markdown

google-cla bot commented Feb 20, 2024

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Copy link
Copy Markdown
Collaborator

@kevmoo kevmoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@kevmoo
Copy link
Copy Markdown
Collaborator

kevmoo commented Feb 28, 2024

looks like you're missing a dependency override in json_serializable

@kevmoo
Copy link
Copy Markdown
Collaborator

kevmoo commented Mar 2, 2024

@ChreSyr – I landed some fixes – take a look!

## 4.8.2-wip

- Require Dart 3.0
- Added `JsonSerializable(createJsonKeys: true)`.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be 4.9.0 since we're adding new features!

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto with json_serial. I'll do it. Just FYI for future.

@kevmoo kevmoo merged commit 5b46ef9 into google:master Mar 3, 2024
static const jsonKeys = ModelJsonKeys;
}

typedef ModelJsonKeys = _$ModelJsonKeys;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Smart, i like the idea

Copy link
Copy Markdown

@Juliotati Juliotati Apr 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ChreSyr would be really nice to have this auto generated inside json_keys_example.g.dart above the generated ModelJsonKeys class

@ChreSyr
Copy link
Copy Markdown
Contributor Author

ChreSyr commented Mar 4, 2024

Thanks @kevmoo for your help and work ! It was my first experience in open-source contribution, I will definitely continue.


Map<String, dynamic> _$ModelToJson(Model instance) => <String, dynamic>{
'first-name': instance.firstName,
'LAST_NAME': instance.lastName,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should these keys use the same above constants?


Model _$ModelFromJson(Map<String, dynamic> json) => Model(
firstName: json['first-name'] as String,
lastName: json['LAST_NAME'] as String,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should these keys use constants from the _$ModelJsonKeys?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe? I thought about it. Just makes things a bit more complex to implement.

Copy link
Copy Markdown

@ekuleshov ekuleshov Jun 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe? I thought about it. Just makes things a bit more complex to implement.

@kevmoo My main driver is to see usages/references of these field names constants. Without it, these two places fall off the grid.


abstract final class _$ModelJsonKeys {
static const String firstName = 'first-name';
static const String lastName = 'LAST_NAME';
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious why not using enum here? Like enum _$ModelJsonKeys { firstName('first-name'), ...

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

Successfully merging this pull request may close these issues.

5 participants