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

패키지 상세v3.2.4

json_to_model

JSON 파일에서 모델 클래스 생성하기.

30일 다운로드
274
좋아요
46
Pub 점수
40

패키지 설치

flutter pub add json_to_model

패키지 개요

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

JSON 파일에서 Dart 모델 클래스를 생성하는 Flutter/Dart 패키지로, 데이터 직렬화 및 역직렬화를 간소화합니다. JSON 파싱을 위한 부품 코드 생성을 자동화하여 수작업과 잠재적 오류를 줄입니다. API 또는 로컬 JSON 데이터를 다루는 개발자에게 이상적이며, 사용자 정의 이름 규칙과 선택적 필드를 지원합니다. 이 도구는 가볍고 pub.dev를 통해 Flutter 프로젝트에 원활하게 통합됩니다.

사용 사례

  • API 응답에서 모델 생성
  • 로컬 JSON 파일을 Dart 클래스로 변환하기
  • 앱에서 JSON 직렬화 자동화
  • 데이터 레이어 개발 간소화
  • 수동 보일러플레이트 코딩 감소

주요 기능

  • 자동으로 Dart 모델 클래스 생성
  • 사용자 정의 필드 이름 지정을 지원
  • 중첩된 JSON 구조를 처리합니다
  • Flutter pub와의 통합
  • 깨끗하고 가독성 높은 출력을 제공합니다

적합한 대상

  • Flutter 개발자
  • API 통합자
  • 데이터 모델링 초보자
  • 코드 일관성을 우선시하는 팀
  • 자주 JSON 변경이 발생하는 프로젝트

고려 사항

  • 수동 JSON 입력이 필요합니다
  • 런타임 검증 없음
  • 정적 JSON 템플릿에 한정
  • 동적 JSON 구조를 처리하지 않습니다
  • 사전 정의된 JSON 스키마와 함께 사용하는 것이 가장 좋습니다

원본 README

연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.

json_to_model Pub Version

Command line tool for generating Dart models (json_serializable) from Json file.

inspired by json_model.

based of the json_to_model

Contents

Features

Feature Status
Null safety ✅
toJson/fromJson ✅
immutable classes ✅
copyWith generation ✅
clone and deepclone ✅
nested json classes ✅
enum support ✅

Installation

on pubspec.yaml

dev_dependencies:
  json_to_model: ^3.0.1
  quiver: ^3.0.1+1

install using pub get command or if you using dart vscode/android studio, you can use install option.

What does this library do

Command line tool to convert .json files into immutable .dart models.

Get started

The command will run through your json files and find possible type, variable name, import uri, decorator and class name, and will write it into the templates.

Create/copy .json files into ./jsons/(default) on root of your project, and run flutter pub run json_to_model.

Examples

Input Consider this files named product.json and employee.json

product.json

{
  "id": "123",
  "caseId?": "123",
  "startDate?": "2020-08-08",
  "endDate?": "2020-10-10",
  "placementDescription?": "Description string"
}

eployee.json

{
  "id": "123",
  "displayName?": "Jan Jansen",
  "@ignore products?": "$[]product"
}

Output This will generate this product.dart and employee.dart

product.dart

import 'package:flutter/foundation.dart';

@immutable
class Product {

  const Product({
    required this.id,
    this.caseId,
    this.startDate,
    this.endDate,
    this.placementDescription,
  });

  final String id;
  final String? caseId;
  final String? startDate;
  final String? endDate;
  final String? placementDescription;

  factory Product.fromJson(Map<String,dynamic> json) => Product(
    id: json['id'] as String,
    caseId: json['caseId'] != null ? json['caseId'] as String : null,
    startDate: json['startDate'] != null ? json['startDate'] as String : null,
    endDate: json['endDate'] != null ? json['endDate'] as String : null,
    placementDescription: json['placementDescription'] != null ? json['placementDescription'] as String : null
  );

  Map<String, dynamic> toJson() => {
    'id': id,
    'caseId': caseId,
    'startDate': startDate,
    'endDate': endDate,
    'placementDescription': placementDescription
  };

  Product clone() => Product(
    id: id,
    caseId: caseId,
    startDate: startDate,
    endDate: endDate,
    placementDescription: placementDescription
  );


  Product copyWith({
    String? id,
    String? caseId,
    String? startDate,
    String? endDate,
    String? placementDescription
  }) => Product(
    id: id ?? this.id,
    caseId: caseId ?? this.caseId,
    startDate: startDate ?? this.startDate,
    endDate: endDate ?? this.endDate,
    placementDescription: placementDescription ?? this.placementDescription,
  );

  @override
  bool operator ==(Object other) => identical(this, other)
    || other is Product && id == other.id && caseId == other.caseId && startDate == other.startDate && endDate == other.endDate && placementDescription == other.placementDescription;

  @override
  int get hashCode => id.hashCode ^ caseId.hashCode ^ startDate.hashCode ^ endDate.hashCode ^ placementDescription.hashCode;
}

eployee.dart

import 'package:flutter/foundation.dart';
import 'product.dart';

@immutable
class Employee {

  const Employee({
    required this.id,
    this.displayName,
    this.products,
  });

  final String id;
  final String? displayName;
  final List<Product>? products;

  factory Employee.fromJson(Map<String,dynamic> json) => Employee(
    id: json['id'] as String,
    displayName: json['displayName'] != null ? json['displayName'] as String : null
  );

  Map<String, dynamic> toJson() => {
    'id': id,
    'displayName': displayName
  };

  Employee clone() => Employee(
    id: id,
    displayName: displayName,
    products: products?.map((e) => e.clone()).toList()
  );


  Employee copyWith({
    String? id,
    String? displayName,
    List<Product>? products
  }) => Employee(
    id: id ?? this.id,
    displayName: displayName ?? this.displayName,
    products: products ?? this.products,
  );

  @override
  bool operator ==(Object other) => identical(this, other)
    || other is Employee && id == other.id
    && displayName == other.displayName
    && products == other.products;

  @override
  int get hashCode => id.hashCode ^
    displayName.hashCode ^
    products.hashCode;
}

Input Consider this file named location.json

{
    "locationId?": 93,
    "locationTypeId?": "1234",
    "updatedAt": "@datetime",
    "name?": "Lunet 10a, Veenendaal",
    "confidential?": false,
    "locationType?": "@enum:INSIDE,OUTSIDE,CLIENT,HOME,ROOM,UNKNOWN",
    "point?": {
        "longitude": 58.1234,
        "latitude": 12.123
    }
}

Output This will generate this location.dart

import 'package:flutter/foundation.dart';

@immutable
class Location {

  const Location({
    this.locationId,
    this.locationTypeId,
    required this.updatedAt,
    this.name,
    this.confidential,
    this.locationType,
    this.point,
  });

  final int? locationId;
  final String? locationTypeId;
  final DateTime updatedAt;
  final String? name;
  final bool? confidential;
  LocationLocationTypeEnum
    get locationLocationTypeEnum => _locationLocationTypeEnumValues.map[locationType]!;
  final String? locationType;
  final Point? point;

  factory Location.fromJson(Map<String,dynamic> json) => Location(
    locationId: json['locationId'] != null ? json['locationId'] as int : null,
    locationTypeId: json['locationTypeId'] != null ? json['locationTypeId'] as String : null,
    updatedAt: DateTime.parse(json['updatedAt'] as String),
    name: json['name'] != null ? json['name'] as String : null,
    confidential: json['confidential'] != null ? json['confidential'] as bool : null,
    locationType: json['locationType'] != null ? json['locationType'] as String : null,
    point: json['point'] != null ? Point.fromJson(json['point'] as Map<String, dynamic>) : null
  );

  Map<String, dynamic> toJson() => {
    'locationId': locationId,
    'locationTypeId': locationTypeId,
    'updatedAt': updatedAt.toIso8601String(),
    'name': name,
    'confidential': confidential,
    'locationType': _locationLocationTypeEnumValues.reverse[locationType],
    'point': point?.toJson()
  };

  Location clone() => Location(
    locationId: locationId,
    locationTypeId: locationTypeId,
    updatedAt: updatedAt,
    name: name,
    confidential: confidential,
    locationType: locationType,
    point: point?.clone()
  );

  Location copyWith({
    int? locationId,
    String? locationTypeId,
    DateTime? updatedAt,
    String? name,
    bool? confidential,
    String? locationType,
    Point? point
  }) => Location(
    locationId: locationId ?? this.locationId,
    locationTypeId: locationTypeId ?? this.locationTypeId,
    updatedAt: updatedAt ?? this.updatedAt,
    name: name ?? this.name,
    confidential: confidential ?? this.confidential,
    locationType: locationType ?? this.locationType,
    point: point ?? this.point,
  );

  @override
  bool operator ==(Object other) => identical(this, other)
    || other is Location && locationId == other.locationId && locationTypeId == other.locationTypeId && updatedAt == other.updatedAt && name == other.name && confidential == other.confidential && locationType == other.locationType && point == other.point;

  @override
  int get hashCode => locationId.hashCode ^ locationTypeId.hashCode ^ updatedAt.hashCode ^ name.hashCode ^ confidential.hashCode ^ locationType.hashCode ^ point.hashCode;
}

enum LocationLocationTypeEnum { INSIDE, OUTSIDE, CLIENT, HOME, ROOM, UNKNOWN }

extension LocationLocationTypeEnumEx on LocationLocationTypeEnum{
  String? get value => _locationLocationTypeEnumValues.reverse[this];
}

final _locationLocationTypeEnumValues = _LocationLocationTypeEnumConverter({
  'INSIDE': LocationLocationTypeEnum.INSIDE,
  'OUTSIDE': LocationLocationTypeEnum.OUTSIDE,
  'CLIENT': LocationLocationTypeEnum.CLIENT,
  'HOME': LocationLocationTypeEnum.HOME,
  'ROOM': LocationLocationTypeEnum.ROOM,
  'UNKNOWN': LocationLocationTypeEnum.UNKNOWN,
});

class _LocationLocationTypeEnumConverter<String, O> {
  final Map<String, O> map;
  Map<O, String>? reverseMap;

  _LocationLocationTypeEnumConverter(this.map);

  Map<O, String> get reverse => reverseMap ??= map.map((k, v) => MapEntry(v, k));
}

@immutable
class Point {

  const Point({
    required this.longitude,
    required this.latitude,
  });

  final double longitude;
  final double latitude;

  factory Point.fromJson(Map<String,dynamic> json) => Point(
    longitude: json['longitude'] as double,
    latitude: json['latitude'] as double
  );

  Map<String, dynamic> toJson() => {
    'longitude': longitude,
    'latitude': latitude
  };

  Point clone() => Point(
    longitude: longitude,
    latitude: latitude
  );


  Point copyWith({
    double? longitude,
    double? latitude
  }) => Point(
    longitude: longitude ?? this.longitude,
    latitude: latitude ?? this.latitude,
  );

  @override
  bool operator ==(Object other) => identical(this, other)
    || other is Point && longitude == other.longitude && latitude == other.latitude;

  @override
  int get hashCode => longitude.hashCode ^ latitude.hashCode;
}

Getting started

  1. Create a directory jsons(default) at root of your project
  2. Put all or Create json files inside jsons directory
  3. run pub run json_to_model. or flutter packages pub run json_to_model flutter project

Usage

this package will read .json file, and generate .dart file, asign the type of the value as variable type and key as the variable name.

Description Expression Input (Example) Output(declaration) Output(import)
declare type depends on the json value {...:any type} {"id": 1, "message":"hello world"}, int id;
String message;
import model and asign type {...:"$value"} {"auth":"$user"} User auth; import 'user.dart'
import from path {...:"$pathto/value"} {"price":"$product/price"} Price price; import '../product/price.dart'
asign list of type and import (can also be recursive) {...:"$[]value"} {"addreses":"$[]address"} List<Address> addreses; import 'address.dart'
import other library(input value can be array) {"@import":...} {"@import":"package:otherlibrary/otherlibrary.dart"} import 'package:otherlibrary/otherlibrary.dart'
Datetime type {...:"@datetime"} {"createdAt": "@datetime:2020-02-15T15:47:51.742Z"} DateTime createdAt;
Enum type {...:"@enum:(folowed by enum separated by ',')"} {"@import":"@enum:admin,app_user,normal"} enum UserTypeEnum { Admin, AppUser, Normal }
Enum type with values {...:"@enum:(folowed by enum separated by ',')"} {"@import":"@enum:admin(0),app_user(1),normal(2)"} enum UserTypeEnum { Admin, AppUser, Normal }

카테고리

의존성

의존성 목록 4 개

소스 저장소

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

아직 데이터가 없습니다.