GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - HJJJJJQ/json_serializable_plus: Generates utilities to aid in serializing from json

BranchesTags
Open more actions menu

Latest commit

 

History

16 Commits
16 Commits

Folders and files

NameName
Last commit message
Last commit date
example
example
 
 
lib
lib
 
 
test
test
 
 
.gitignore
.gitignore
 
 
.metadata
.metadata
 
 
CHANGELOG.md
CHANGELOG.md
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
build.yaml
build.yaml
 
 
pubspec.lock
pubspec.lock
 
 
pubspec.yaml
pubspec.yaml
 
 

Repository files navigation

json_serializable_plus

使用build_runner,对官方json_serializable功能的增加,提供了统一的json解析方法。 支持List解析。

构建器会找到 package:json_annotation注释的 类,生成统一的解析方法。

查看json_serializable的使用方法

Getting Started

  • 当前最新版本 json_serializable_plus: ^0.0.3

在已经添加json_serializable 的基础上,只需要在dev_dependencies中添加json_serializable_plus:^x.x.x 。 最终的pubspec.yaml 文件类似下面这样:

dependencies:
  flutter:
    sdk: flutter

  json_annotation: '>=3.1.0 <3.2.0'


dev_dependencies:
  flutter_test:
    sdk: flutter

  build_runner: ^1.0.0
  json_serializable: ^3.5.1
  json_serializable_plus: ^0.0.3


Example

  1. 给Teacher 和 Student设置 @JsonSerializable()注解
import 'package:json_annotation/json_annotation.dart';

part 'user.g.dart';
@JsonSerializable()
class Teacher {
  String teacherName;
  int id;
  List<Student> students;
  Teacher(this.teacherName,this.id,this.students);
  factory Teacher.fromJson(Map<String,dynamic> json)=>_$TeacherFromJson(json);
}


@JsonSerializable()
class Student {
  String studentName;
  int id;
  Student(this.studentName,this.id);
  factory Student.fromJson(Map<String,dynamic> json)=>_$StudentFromJson(json);
}


  1. 运行flutter pub run build_runner build会在根目录会生成一个json_serializable.dart 文件

  2. 调用方法解析json

Teacher teacher = JsonSerializablePlus.fromJson(
        json.decode("{\"teacherName\":\"hjq\",\"id\":32,\"students\":[{\"studentName\":\"s1\",\"id\":12},{\"studentName\":\"s2\",\"id\":12}]}"));

 List<Student> student = JsonSerializablePlus.fromJson(json.decode(
        "[{\"studentName\":\"s1\",\"id\":12},{\"studentName\":\"s2\",\"id\":12}]"));

TODO

  • 增加对json_annotation 字段的解析

QA

  • 运行flutter pub run build_runner build时出现
Failed to precompile build_runner:build_runner:
../../../../../.pub-cache/hosted/pub.flutter-io.cn/build_resolvers-x.x.x/lib/src/resolver.dart:343:10: Error: Method not found: 'SummaryBuilder'.
return SummaryBuilder(sdkSources, sdk.context)

dev_dependencies指定build_resolvers版本 >= ^1.5.1

  • 运行flutter pub run build_runner build时出现[SEVERE] Conflicting outputs were detected and the build is unable to prompt for permission to remove them.

代码提交时会把生成的*.g.dart提交上去,但是dart_tool/build 不会提交,所以在缺少dart_tool/build的文件情况下,flutter 无法自己删除重建。 运行以下代码

flutter packages pub run build_runner clean
flutter packages pub run build_runner build --delete-conflicting-outputs

About

Generates utilities to aid in serializing from json

Topics

dartdart-build-systemdartlangflutterjsonjson-annotationjson-serializablesource-gen

Resources

Readme
BSD-3-Clause license
Activity

Stars

34 stars

Watchers

1 watching

Forks

7 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.