Flutter Dart Macro 宏简化 JSON 序列化

简介: 今天我们将会体验 dart 语言新特性 macro 宏,来实现对 json 的序列化,用到的包是官方实验室写的 json 包。本文将会一步步的带你实现这个功能,那我们开始吧。

Flutter Dart Macro 宏简化 JSON 序列化

视频

https://youtu.be/gBZYjBmoOcU

https://www.bilibili.com/video/BV1tT421a7ev/

前言

原文 https://ducafecat.com/blog/using-dart-macros-to-simplify-json-serialization

今天我们将会体验 dart 语言新特性 macro 宏,来实现对 json 的序列化,用到的包是官方实验室写的 json 包。

本文将会一步步的带你实现这个功能,那我们开始吧。

Dart, 宏, JSON 序列化, 代码生成, 效率优化, 语言特性

参考

https://dart.dev/language/macros#view-the-generated-code

https://github.com/dart-lang/language/blob/main/working/macros/feature-specification.md

https://pub.dev/packages/json

macro 宏

好处

  • 直接语言集成。由于宏直接内置于语言中,它们比外部库提供更多的一致性并兼容语言的本机功能。
  • 减少外部依赖。通过使用宏来减少对外部库的依赖,您可以减小应用程序的大小并简化依赖管理。
  • 编译时代码生成。与运行时生成代码的解决方案相比,宏在编译时生成代码可能会提供更快的结果。
  • 增强定制化和自由度。开发者可以借助宏更好地根据项目独特需求定制代码生成,提供大量的定制自由度。

json_serializable 比较

为了在 Dart 中生成 JSON 序列化代码,这个库经常被使用,但它需要更多的依赖和设置。无需额外的工具,宏可以在源代码中自动生成 fromJson 和 toJson 方法。

freezed 比较

使用这个库,创建类型安全、不可变的 Dart 类变得非常简单。虽然 freezed 功能强大,但宏可能以更少的依赖和更直接的集成来完成类似的任务。

实现步骤

第一步:切到 dart 3.5 版本

https://docs.flutter.dev/release/archive?tab=macos

下载 beta channel , dart 3.5 版本。

我本机用的 fvm 管理多版本。

检查环境

❯ flutter --version

Flutter 3.23.0-0.1.pre • channel beta • https://github.com/flutter/flutter.git
Framework • revision 2feea7a407 (13 天前) • 2024-06-06 10:19:10 +0700
Engine • revision bb10c54666
Tools • Dart 3.5.0 (build 3.5.0-180.3.beta) • DevTools 2.36.0
AI 代码解读

第二步:配置 pubspec.yaml

pubspec.yaml

environment:
  sdk: ">=3.5.0-180.3.beta <4.0.0"

dependencies:
  json: ^0.20.2
AI 代码解读

官方实验室 json 包
https://pub.dev/packages/json

analysis_options.yaml

analyzer:
  enable-experiment:
    - macros
AI 代码解读

在编写代码时,除非你告诉分析器你正在试验此功能,否则它会发出警告。

第三步:编写测试代码

编写官方示例代码

lib/macros/user.dart

import 'package:json/json.dart';

@JsonCodable() // Macro annotation.
class User {
   
   
  final int? age;
  final String name;
  final String username;
}
AI 代码解读

可以点击 Go to Augmentation 查看详细

augment library 'package:flutter_application_macro/macros/user.dart';

import 'dart:core' as prefix0;

augment class User {
   
   
  external User.fromJson(prefix0.Map<prefix0.String, prefix0.Object?> json);
  external prefix0.Map<prefix0.String, prefix0.Object?> toJson();
  augment User.fromJson(prefix0.Map<prefix0.String, prefix0.Object?> json, )
      : this.age = json[r'age'] as prefix0.int?,
        this.name = json[r'name'] as prefix0.String,
        this.username = json[r'username'] as prefix0.String;
  augment prefix0.Map<prefix0.String, prefix0.Object?> toJson() {
   
   
    final json = <prefix0.String, prefix0.Object?>{
   
   };
    if (this.age != null) {
   
   
      json[r'age'] = this.age!;
    }
    json[r'name'] = this.name;
    json[r'username'] = this.username;
    return json;
  }
}
AI 代码解读

这段代码自动的,不需要你编写。

启动时加入测试 lib/main.dart

class _MyHomePageState extends State<MyHomePage> {
   
   

  @override
  void initState() {
   
   
    super.initState();

    // Given some arbitrary JSON:
    final userJson = {
   
   
      'age': 5,
      'name': 'Roger',
      'username': 'roger1337',
    };

    // Use the generated members:
    final user = User.fromJson(userJson);
    print(user);
    print(user.toJson());
  }
  ...
AI 代码解读

如果代码成功运行将会打印 json 字符串

最后:运行测试

加入参数 --enable-experiment=macros 执行

我是 vsc 所以加在了 .vscode/launch.json

  "configurations": [
    {
   
   
      "name": "flutter_application_macro",
      "request": "launch",
      "type": "dart",
      "flutterMode": "debug",
      "program": "lib/main.dart",
      "args": ["--enable-experiment=macros"]
    },
AI 代码解读

主要是在 args 中加入 --enable-experiment=macros 参数

最后我们启动,查看输出信息。

Launching lib/main.dart on iPhone 15 in debug mode...
Xcode build done.                                           11.3s
Connecting to VM Service at ws://127.0.0.1:55696/aP-7aqbeIA4=/ws
Connected to the VM Service.
flutter: Instance of 'User'
flutter: {
   
   age: 5, name: Roger, username: roger1337}
AI 代码解读

官方 json 实现

https://pub.dev/packages/json

代码

https://github.com/dart-lang/sdk/blob/main/pkg/json/lib/json.dart

macro class JsonCodable
    with _Shared, _FromJson, _ToJson
    implements ClassDeclarationsMacro, ClassDefinitionMacro {
   
   
  const JsonCodable();

  /// Declares the `fromJson` constructor and `toJson` method, but does not
  /// implement them.
  @override
  Future<void> buildDeclarationsForClass(
      ClassDeclaration clazz, MemberDeclarationBuilder builder) async {
   
   
    final mapStringObject = await _setup(clazz, builder);

    await (
      _declareFromJson(clazz, builder, mapStringObject),
      _declareToJson(clazz, builder, mapStringObject),
    ).wait;
  }

  /// Provides the actual definitions of the `fromJson` constructor and `toJson`
  /// method, which were declared in the previous phase.
  @override
  Future<void> buildDefinitionForClass(
      ClassDeclaration clazz, TypeDefinitionBuilder builder) async {
   
   
    final introspectionData =
        await _SharedIntrospectionData.build(builder, clazz);

    await (
      _buildFromJson(clazz, builder, introspectionData),
      _buildToJson(clazz, builder, introspectionData),
    ).wait;
  }
}
AI 代码解读
  1. buildDeclarationsForClass 方法:
    • 在类声明阶段,为该类生成 fromJson 构造函数和 toJson 方法的声明。
  2. buildDefinitionForClass 方法:
    • 在类定义阶段,为 fromJsontoJson 提供具体的实现逻辑。
    • 该实现逻辑是根据类的属性信息自动生成的,通过反射获取类的结构信息。

代码

https://github.com/ducafecat/flutter_develop_tips/tree/main/flutter_application_macro_json

小结

这个 macro 实现的 json 目前也是试验产品,不建议你在项目中使用,本文的目的只是让你体验下宏的优势,和未来代码编写的一个趋势方向。

感谢阅读本文

如果有什么建议,请在评论中让我知道。我很乐意改进。


flutter 学习路径


© 猫哥
ducafecat.com

end

目录
打赏
0
2
2
0
36
分享
相关文章
|
6月前
|
Twaver-HTML5基础学习(19)数据容器(2)_数据序列化_XML、Json
本文介绍了Twaver HTML5中的数据序列化,包括XML和JSON格式的序列化与反序列化方法。文章通过示例代码展示了如何将DataBox中的数据序列化为XML和JSON字符串,以及如何从这些字符串中反序列化数据,重建DataBox中的对象。此外,还提到了用户自定义属性的序列化注册方法。
77 1
Flutter 是谷歌推出的一款高效跨平台移动应用开发框架,使用 Dart 语言,具备快速开发、跨平台支持、高性能、热重载及美观界面等特点。
Flutter 是谷歌推出的一款高效跨平台移动应用开发框架,使用 Dart 语言,具备快速开发、跨平台支持、高性能、热重载及美观界面等特点。本文从 Flutter 简介、特点、开发环境搭建、应用架构、组件详解、路由管理、状态管理、与原生代码交互、性能优化、应用发布与部署及未来趋势等方面,全面解析 Flutter 技术,助你掌握这一前沿开发工具。
258 8
Go语言中json序列化的一个小坑,建议多留意一下
在Go语言开发中,JSON因其简洁和广泛的兼容性而常用于数据交换,但其在处理数字类型时存在精度问题。本文探讨了JSON序列化的一些局限性,并介绍了两种替代方案:Go特有的gob二进制协议,以及msgpack,两者都能有效解决类型保持和性能优化的问题。
103 7
|
6月前
|
如何在 Flutter 项目中使用 Dart 语言?
如何在 Flutter 项目中使用 Dart 语言?
164 58
聊聊 Go 语言中的 JSON 序列化与 js 前端交互类型失真问题
在Web开发中,后端与前端的数据交换常使用JSON格式,但JavaScript的数字类型仅能安全处理-2^53到2^53间的整数,超出此范围会导致精度丢失。本文通过Go语言的`encoding/json`包,介绍如何通过将大整数以字符串形式序列化和反序列化,有效解决这一问题,确保前后端数据交换的准确性。
92 4
对比JSON和Hessian2的序列化格式
通过以上对比分析,希望能够帮助开发者在不同场景下选择最适合的序列化格式,提高系统的整体性能和可维护性。
150 3
Python中序列化/反序列化JSON格式的数据
【11月更文挑战第4天】本文介绍了 Python 中使用 `json` 模块进行序列化和反序列化的操作。序列化是指将 Python 对象(如字典、列表)转换为 JSON 字符串,主要使用 `json.dumps` 方法。示例包括基本的字典和列表序列化,以及自定义类的序列化。反序列化则是将 JSON 字符串转换回 Python 对象,使用 `json.loads` 方法。文中还提供了具体的代码示例,展示了如何处理不同类型的 Python 对象。
110 1
flutter dart mixin 姿势
flutter dart mixin 姿势
flutter:dart的学习
本文介绍了Dart语言的下载方法及基本使用,包括在Windows系统上和VSCode中的安装步骤,并展示了如何运行Dart代码。此外,还详细说明了Dart的基础语法、构造函数、泛型以及库的使用方法。文中通过示例代码解释了闭包、运算符等概念,并介绍了Dart的新特性如非空断言操作符和延迟初始化变量。最后,提供了添加第三方库依赖的方法。
68 12

热门文章

最新文章