搜尋套件、儲存庫與 APP 分類

GitHub 儲存庫iv-re/dart-json-codable

dart-json-codable

JSON 解析、驗證與模式產生。

dart-json-codable 專案封面
星標
0
分支
0
最近推送(UTC)
2026年9月15日
專案狀態
未封存

原始 README

以下為英文專案原文快照,最新內容請造訪 GitHub。

json_codable

JSON parsing, validation, and schema generation.

Usage

Parsing & Validation (JsonObject)

JsonObject supports two parsing modes: Accumulated Validation (json.parse) and Fail-Fast (direct constructor).

import 'dart:convert';
import 'package:json_codable/json_codable.dart';

class CreateUserDto {
  CreateUserDto.fromJson(JsonObject json)
      : name = json.string('name', rules: [.length(min: 2)]),
        email = json.string('email', rules: [.email()]),
        age = json.integerOrNull('age', rules: [.range(min: 18)]);

  final String name;
  final String email;
  final int? age;
}

final jsonString = '{"name": "A", "email": "invalid"}';
final jsonMap = jsonDecode(jsonString) as Map<String, Object?>;
final json = JsonObject(jsonMap);

// Accumulated Validation: collects ALL field errors before throwing
try {
  final user = json.parse(CreateUserDto.fromJson);
} on ValidationErrors catch (error) {
  print(error.toJson()); // Contains errors for both 'name' and 'email'
}

// Fail-Fast: throws immediately on the first encountered error
try {
  final user = CreateUserDto.fromJson(json);
} on ValidationErrors catch (error) {
  print(error.toJson()); // Contains only the first error ('name')
}
Serialization (ToJson)

Implement ToJson for DTOs and models that export to JSON maps:

class UserDto implements ToJson {
  UserDto({required this.id, required this.name});

  final String id;
  final String name;

  @override
  Map<String, Object?> toJson() => {
    'id': id,
    'name': name,
  };
}
Schema Generation

Generate JSON Schema documents directly from fromJson mappers or builder callbacks:

class CreateUserDto {
  // ...
  static final Schema schema = JsonObject.schema(
    CreateUserDto.fromJson,
    title: 'CreateUserDto',
  );
}
Ivan Salvatore GitHub avatar
GitHub 作者

Ivan Salvatore ↗

語言Dart

分類

使用的依賴

依賴清單 6 項

  • meta^1.12.0
  • equatable^2.1.0
  • json_schema_builder^0.1.6
  • checks開發依賴^0.3.1
  • pusk_lints開發依賴^1.0.0
  • test開發依賴^1.25.6

如何選擇適合專案的儲存庫?

先依使用情境篩選,再比較專案活躍情況,最後到原始儲存庫確認使用要求。

  1. 透過儲存庫名稱、作者或描述搜尋,再依技術分類縮小範圍。
  2. 綜合星標、分支與最近推送比較專案;星標反映社群關注,不保證品質。
  3. 使用前在 GitHub 確認 README、授權條款與 Issues。

來源與維護

“A repository is the most basic element of GitHub.”

GitHub Docs — About repositories ↗