hiranthaR / Json-to-Dart-Model

Json to Dart Model extension can convert JSON objects into Dart data classes. It supports pure Dart class conversion, Flutter-recommended JSON serialization using annotations, Freezed support, Effective Dart:Style, and many more features. Currently, it has more than 135,000 installs.
https://marketplace.visualstudio.com/items?itemName=hirantha.json-to-dart
MIT License
93 stars 18 forks source link

how can i custom the class's property name? #94

Closed hiyangjx closed 2 years ago

hiyangjx commented 2 years ago

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

iamarnas commented 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 commented 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'.

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?, );

iamarnas commented 2 years ago

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

hiyangjx commented 2 years ago

@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?

iamarnas commented 2 years ago

@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.

hiyangjx commented 2 years ago

@iamarnas Thanks, the https://github.com/flutterchina/json_model support this feature, if you need.

iamarnas commented 2 years ago

@hiyangjx Thank you for the tips 👍