从单个 JSON 输入生成 dart 代码
我正在尝试构建一个 dart 包,它从 OData JSON 表示文件生成 API 源代码。我尝试使用 source_gen
,但它似乎是根据注释或现有源代码生成代码(例如,JSON 序列化程序为现有类生成代码)。
目标:根据 JSON 数据生成多个 dart 代码。例如:假设我的 JSON 是:
{
"user": {
"income": "Decimal", //Decimal is the type, which I translate it into dart type
}
}
,我生成的代码将是:
user.dart
:
import "package:decimal/decimal.dart";
class User {
final Decimal age;
User(this.age);
}
I'm trying to build a dart package which generates API source code from OData JSON representation file. I tried using source_gen
, but it appears to generate code based on annotations or existing source code (e.g. JSON serializer generated code for existing class).
Goal: Generate multiple dart codes based on JSON data. E.g.: Say my JSON is:
{
"user": {
"income": "Decimal", //Decimal is the type, which I translate it into dart type
}
}
and my generated code would be:
user.dart
:
import "package:decimal/decimal.dart";
class User {
final Decimal age;
User(this.age);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当涉及到 API 描述时,你想要做的事情已经实现了。
您可以使用
.proto
或 swagger.yaml
/.json
文件中的类生成代码。原型文件示例:
看:

What are you trying to do is already implemented, when it comes to API description.
You can generate code with classes from
.proto
or swagger.yaml
/.json
files.Proto file example:
Look:
