Closed hiyangjx closed 2 years ago
@hiyangjx Hi 👋
To change the class property name for primitive values you don't need to annotate anything. Just change JSON
key name. In your case change 'e_id'
to 'history_id'
.
@hiyangjx Hi 👋 To change the class property name for primitive values you don't need to annotate anything. Just change
JSON
key name. In your case change'e_id'
to'history_id'
.
thanks. but the real json field is e_id, if i changed it in **.jsonc, i will change the following code manually after build every time. from: factory History.fromJson(Map<String, dynamic> json) => History( historyId: json['history_id'] as String?, );
to: factory History.fromJson(Map<String, dynamic> json) => History( historyId: json['e_id'] as String?, );
Dart to Json Model support converting from the local file **.jsonc
to make easily edit original json files. That if you want another property name you need change json key and generator will generate new updated JSON
object. Annotation used for anothers options where you can't display in original JSON
file. But for edit the name for primitive values is no idea to add annotation and change JSON
guide lines. Can you explain with more details what wrong if you change the original json key in the jsonc file?
@hiyangjx
@iamarnas Thanks for your reply. To put it simply, I just don't know how to use @jsonkey annotation. If I want to get history.swift:
import 'package:json_annotation/json_annotation.dart';
class History {
@JsonKey(name: 'e_id')
String? id;
History({this.id});
factory History.fromJson(Map<String, dynamic> json) => History(
id: json['e_id'] as String?,
);
Map<String, dynamic> toJson() => {
'e_id': id,
};
}
How can i write models.jsonc?
@hiyangjx Aaa, OK, I got it. This generator does not support changing name for primitive values. Only for class elements it possible to change type.
@iamarnas Thanks, the https://github.com/flutterchina/json_model support this feature, if you need.
@hiyangjx Thank you for the tips 👍
Is there an existing issue for this?
Current Behavior
models.jsonc file: [ { "__className": "history", "e_id": "9403" } ] history.dart file: class History { String? eId;
History({this.eId});
factory History.fromJson(Map<String, dynamic> json) => History( eId: json['e_id'] as String?, );
Map<String, dynamic> toJson() => { 'e_id': eId, }; }
Expected Behavior
how can i annotation the e_id in JSON, auto change the dart class's eId to historyId? like: d@ - Marks as default value. r@ - Marks as required value. hope add the n@field@propery - Marks custom propery name.
Steps To Reproduce
No response
Version
No response
Relevant JSON syntax
No response
Anything else?
No response