skycosmic_parser 0.0.2
skycosmic_parser: ^0.0.2 copied to clipboard
A CLI tool to parse and generate code for SkyCosmic projects.
Skycosmic Parser #
A powerful CLI tool to generate robust, "anti-null-safety" Dart models from API JSON responses. It automatically handles null values, nested objects, and lists, ensuring your application never crashes due to unexpected nulls.
Features #
- Anti-Null Safety: Generates models where fields are nullable but
fromJsonmethods safely handle nulls and missing keys. - Automatic Type Inference: Detects types (int, double, bool, String, List, Map) from your JSON data.
- Nested Class Generation: Automatically detects nested objects and generates separate classes for them.
- Token Support: Easily pass authentication tokens for protected API endpoints.
- CLI & Programmatic Usage: Use it from the command line or import the library in your Dart scripts.
Installation #
Activate the package globally using:
dart pub global activate skycosmic_parser
Usage #
CLI Command #
The primary way to use Skycosmic Parser is via the command line.
Syntax
skycosmic create model <filename> <API_URL> [token='<YOUR_TOKEN>']
Arguments
<filename>: The name of the file (and class) to generate (e.g.,user).<API_URL>: The full URL of the API endpoint you want to parse.[token='...']: (Optional) Your Bearer token if the API requires authentication.
Examples
1. Public API:
skycosmic create model todo https://jsonplaceholder.typicode.com/todos/1
2. Protected API with Token:
skycosmic create model user https://api.myservice.com/v1/user/profile token='abcdef-123456-secret'
Steps to Generate a Model #
- Identify your API Endpoint: Ensure you have the URL that returns the JSON structure you want to model.
- Run the Command: Execute the command in your terminal.
- Check the Output: The tool will generate a file in
lib/models/<filename>.dart. - Use the Model: Import the generated file in your project and use the class (e.g.,
Todo.fromJson(json)).
Programmatic Usage #
You can also use the parser in your own Dart code:
import 'package:skycosmic_parser/skycosmic_parser.dart';
void main() {
final jsonData = {'id': 1, 'name': 'Test'};
final generator = ModelGenerator('MyModel');
final code = generator.generate(jsonData);
print(code);
}
Generated Code Example #
The tool generates easy-to-use models like this:
class ApiResponse {
final int? userId;
final int? id;
final String? title;
final bool? completed;
ApiResponse({
this.userId,
this.id,
this.title,
this.completed,
});
factory ApiResponse.fromJson(Map<String, dynamic> json) {
return ApiResponse(
userId: json['userId'],
id: json['id'],
title: json['title'],
completed: json['completed'],
);
}
Map<String, dynamic> toJson() {
return {
'userId': userId,
'id': id,
'title': title,
'completed': completed,
};
}
}
License #
MIT