搜尋套件、儲存庫與 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);
}

所屬分類

原始碼儲存庫

使用此套件的專案

暫無相關資料。