パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細v1.2.0+2

dson_adapter

少ないコードで JSON を Dart クラスに変換する(build_runner)

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);
}

カテゴリー

ソースリポジトリ

このパッケージを使用しているプロジェクト

データはまだありません。