Buscar pacotes, repositórios e categorias de apps

Repositório GitHubiv-re/dart-json-codable

dart-json-codable

Análise, validação e geração de esquema JSON.

Capa do projeto dart-json-codable
Estrelas
0
Forks
0
Último push (UTC)
15 de set. de 2026
Status do projeto
Ativo

README original

Texto original em inglês. Visite o GitHub para a versão atual.

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
Autor no GitHub

Ivan Salvatore ↗

LinguagensDart

Categorias

Dependências usadas

Lista de dependências 6 itens

  • meta^1.12.0
  • equatable^2.1.0
  • json_schema_builder^0.1.6
  • checksDesenvolvimento^0.3.1
  • pusk_lintsDesenvolvimento^1.0.0
  • testDesenvolvimento^1.25.6

Como escolher um repositório para seu projeto?

Filtre pelo caso de uso, compare a atividade e confira o projeto original antes de adotá-lo.

  1. Busque por nome, autor ou descrição e filtre por tema.
  2. Compare estrelas, forks e envios recentes. Estrelas indicam interesse, mas não garantem qualidade.
  3. Confira o README, a licença e as issues no GitHub antes de usar o projeto.

Fontes e manutenção

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

GitHub Docs — About repositories ↗