패키지, 저장소 및 앱 카테고리 검색

패키지 상세v1.2.0+2

dson_adapter

더 적은 코드로 JSON을 Dart 클래스로 변환하기 (build_runner)

AndroidiOSWebmacOSWindowsLinux
30일 다운로드
11
좋아요
31
Pub 점수
150

패키지 설치

flutter pub add dson_adapter

패키지 개요

공개 정보를 바탕으로 생성한 선택 가이드입니다.

DsonAdapter는 최소한의 보일러플레이트로 JSON에서 Dart 클래스로의 변환을 간편하게 해주는 Flutter용 코드 생성 패키지입니다. 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);
}

카테고리

소스 저장소

이 패키지를 사용하는 프로젝트

아직 데이터가 없습니다.