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

Add #[frb(json_key)] annotation to configure/control JSON keys for dart code. #2505

Open
ashim-kr-saha opened this issue Jan 18, 2025 · 4 comments
Labels
awaiting Waiting for responses, PR, further discussions, upstream release, etc enhancement New feature or request

Comments

@ashim-kr-saha
Copy link

Is your feature request related to a problem? Please describe.
We generated a dart structure that will have toJosn/fromJson methods. But we can't control the generated key of the JSON. eg

#[frb(dart_metadata=("freezed"))]
pub struct Tag {
    pub id: Option<String>,
    pub created_at: Option<i64>,
    pub created_by: Option<String>,
    pub updated_at: Option<i64>,
    pub updated_by: Option<String>,
    pub name: String,
}

Will generate something similar to

import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart';
import 'package:freezed_annotation/freezed_annotation.dart' hide protected;

import '../frb_generated.dart';

part 'tags.freezed.dart';
part 'tags.g.dart';

@freezed
class Tag with _$Tag {
  const factory Tag({
    String? id,
    int? createdAt,
    String? createdBy,
    int? updatedAt,
    String? updatedBy,
    required String name,
  }) = _Tag;

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

But if we need something similar to

import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart';
import 'package:freezed_annotation/freezed_annotation.dart' hide protected;

import '../frb_generated.dart';

part 'tags.freezed.dart';
part 'tags.g.dart';

@freezed
class Tag with _$Tag {
  const factory Tag({
    String? id,
    @JsonKey(name: 'created_at') int? createdAt,
    @JsonKey(name: 'created_by') String? createdBy,
    @JsonKey(name: 'updated_at') int? updatedAt,
    @JsonKey(name: 'updated_by') String? updatedBy,
    required String name,
  }) = _Tag;

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

There is no option for that.

Describe the solution you'd like
We can have a #[frb(json_key=("created_at"))] annotation to configure/control JSON keys for dart code.

#[frb(dart_metadata=("freezed"))]
pub struct Tag {
    pub id: Option<String>,

    #[frb(json_key=("created_at"))]
    pub created_at: Option<i64>,

    #[frb(json_key=("created_by"))]
    pub created_by: Option<String>,

    #[frb(json_key=("updated_at"))]
    pub updated_at: Option<i64>,

    #[frb(json_key=("updated_by"))]
    pub updated_by: Option<String>,

    pub name: String,
}
@ashim-kr-saha ashim-kr-saha added the enhancement New feature or request label Jan 18, 2025
@fzyzcjy
Copy link
Owner

fzyzcjy commented Jan 18, 2025

Looks pretty reasonable and feel free to PR for this!

Btw, in your case maybe you want a more automatic solution - https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonSerializable/fieldRename.html?

Another potential workaround: do the json serialization on the rust side using e.g. serde.

@fzyzcjy fzyzcjy added the awaiting Waiting for responses, PR, further discussions, upstream release, etc label Jan 18, 2025
@ashim-kr-saha
Copy link
Author

@fzyzcjy Should I try to create a PR based on #[frb(json_key=("created_at"))] approach?

@fzyzcjy
Copy link
Owner

fzyzcjy commented Jan 18, 2025

I personally think the fieldRename approach is better for your case, because the json_key approach requires to write a lot of annotations. But feel free to choose the one you like!

@ashim-kr-saha
Copy link
Author

I found a better option here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting Waiting for responses, PR, further discussions, upstream release, etc enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants