搜索插件、代码仓与 APP 分类

插件详情v1.2.0+2

dson_adapter

使用更少代码生成(build_runner)将 JSON 转换为 Dart 类

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
11
喜欢数
31
Pub 分数
150

安装插件

flutter pub add dson_adapter

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

DsonAdapter 是一个用于 Flutter 的代码生成包,可简化 JSON 到 Dart 类的转换,减少样板代码。通过使用 build_runner,它会自动生成序列化和反序列化逻辑,从而减少手动编码。专为希望实现高效、类型安全的 JSON 处理而无需编写重复代码的开发者设计。它可无缝集成到所有主要平台的 Flutter 项目中,提升开发速度和可维护性。

适合场景

  • 自动生成 JSON 序列化器
  • 减少数据模型中的样板代码
  • 简化 API 响应解析
  • 与 RESTful 服务集成
  • 启用类型安全的 JSON 处理
  • 支持跨平台应用

主要特点

  • 通过 build_runner 实现自动代码生成
  • 所需设置最少
  • 类型安全的 JSON 序列化
  • 支持嵌套对象
  • 高效的运行时性能
  • 跨平台兼容性

适合对象

  • 使用 JSON API 的 Flutter 开发人员
  • 注重代码可维护性的团队
  • 需要快速原型设计的项目
  • 具有复杂 JSON 结构的应用程序
  • 开发者避免手动序列化
  • 使用 build_runner 工作流的用户

使用建议

  • 需要设置 build_runner
  • 仅构建时依赖
  • 不适用于仅运行时的 JSON 解析
  • 无需额外配置即可进行有限的自定义
  • Dart 类需要适当的注解
  • 可能会略微增加构建时间

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

DSON

Convert JSON to Dart Class withless code generate(build_runner).

A simple Object

class Person {
  final int id;
  final String name;
  final int age;

  Person({
    required this.id,
    required this.name,
    required this.age,
  });
}

Convert json to Object:

main(){
     final jsonMap = {
      'id': 1,
      'name': 'Joshua Clak',
      'age': 3,
    };

    Person person = dson.fromJson(jsonMap, Person.new);

    print(person.id);
    print(person.name);
    print(person.age);
}


A complex object:

For complex objects it is necessary to declare the constructor in the inner property;

main(){
    final jsonMap = {
      'id': 1,
      'name': 'MyHome',
      'owner': {
        'id': 1,
        'name': 'Joshua Clak',
        'age': 3,
      },
    };

    Person person = dson.fromJson(
      jsonMap,
      Person.new,
      inner: {
        'owner':  Person.new,
      }
    );

    print(person);
}

A complex object with List:

For work with a list, it is necessary to declare the constructor in the inner property and declare the list resolver in the resolvers property.

main(){
    final jsonMap = {
      'id': 1,
      'name': 'MyHome',
      'owner': {
        'id': 1,
        'name': 'Joshua Clak',
        'age': 3,
      },
      'parents': [
        {
          'id': 2,
          'name': 'Kepper Vidal',
          'age': 25,
        },
        {
          'id': 3,
          'name': 'Douglas Bisserra',
          'age': 23,
        },
      ],
    };

    Home home = dson.fromJson(
      // json Map or List
      jsonMap,
      // Main constructor
      Home.new,
      // external types
      inner: {
        'owner': Person.new,
        'parents': ListParam<Person>(Person.new),
      },
    );

    print(home);
}

DSON Have ListParam and SetParam for collection.

When API replace Param Name (Set aliases):

You need to declare within the aliases map the object type that has changed in the key, and in the value, a map with the old key as the key and the new key as the value.

main(){
    final jsonMap = {
      'id': 1,
      'name': 'MyHome',
      'master': {
        'key': 1,
        'name': 'Joshua Clak',
        'age': 3,
      },
      'parents': [
        {
          'key': 2,
          'name': 'Kepper Vidal',
          'age': 25,
        },
        {
          'key': 3,
          'name': 'Douglas Bisserra',
          'age': 23,
        },
      ],
    };

    Home home = dson.fromJson(
      // json Map or List
      jsonMap,
      // Main constructor
      Home.new,
      // external types
      inner: {
        'owner': Person.new,
        'parents': ListParam<Person>(Person.new),
      },
      // Param names Object <-> Param name in API
      aliases: {
        Home: {'owner': 'master'},
        Person: {'id': 'key'}
      }
    );

    print(home);
}

所属分类

源码仓库

使用此插件的项目

暂无相关数据。