Search packages, repositories and app categories

Package detailsv1.2.0+2

dson_adapter

Convert JSON to Dart Class withless code generate(build_runner)

AndroidiOSWebmacOSWindowsLinux
30-day downloads
11
Likes
31
Pub points
150

Install package

flutter pub add dson_adapter

Package Overview

Package selection guidance compiled from public package information, community data, and project usage.

DsonAdapter is a code generation package for Flutter that simplifies JSON-to-Dart class conversion with minimal boilerplate. Using build_runner, it automatically generates serialization and deserialization logic, reducing manual coding. Designed for developers seeking efficient, type-safe JSON handling without writing repetitive code. It integrates seamlessly into Flutter projects across all major platforms, enhancing development speed and maintainability.

Use cases

  • Auto-generating JSON serializers
  • Reducing boilerplate in data models
  • Streamlining API response parsing
  • Integrating with RESTful services
  • Enabling type-safe JSON handling
  • Supporting cross-platform apps

Key features

  • Automatic code generation via build_runner
  • Minimal setup required
  • Type-safe JSON serialization
  • Supports nested objects
  • Efficient runtime performance
  • Cross-platform compatibility

Suitable for

  • Flutter developers using JSON APIs
  • Teams prioritizing code maintainability
  • Projects requiring rapid prototyping
  • Apps with complex JSON structures
  • Developers avoiding manual serialization
  • Those using build_runner workflows

Considerations

  • Requires build_runner to be set up
  • Build-time dependency only
  • Not suitable for runtime-only JSON parsing
  • Limited customization without additional configuration
  • Needs proper annotations on Dart classes
  • May increase build time slightly

Original README

Original README snapshot from the linked source repository. Visit the repository for the latest content.

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

Categories

Source repository

Projects using this package

No data available yet.