'Generating dart code from single JSON input

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

}


Solution 1:[1]

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.

  1. Proto file generation for gRPC API

Proto file example:

message SearchRequest {
  string query = 1;
  int32 page_number = 2;
  int32 result_per_page = 3;
}

message SearchResponse {
 ...
}
  1. Swagger rest api code generation from .yml/json files

Look: img

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Danila Fominyh