【问题标题】:How to serialize Uint8List to json with json_annotation in Dart?如何在 Dart 中使用 json_annotation 将 Uint8List 序列化为 json?
【发布时间】:2020-12-22 05:49:51
【问题描述】:

我创建了一个包含Uint8List 成员的简单类:

import "package:json_annotation/json_annotation.dart";
part "openvpn.g.dart";

@JsonSerializable()
class OpenVPN extends VPN {
  OpenVPN(Uint8List profile) {
    this.profile = profile;
  }
  /...
  Uint8List profile = null;

但是,当在其上运行构建运行程序以生成 json 序列化程序时,我得到:

Could not generate `fromJson` code for `profile`.
None of the provided `TypeHelper` instances support the defined type.
package:my_app/folder/openvpn.dart:19:13
   ╷
19 │   Uint8List profile = null;
   │             ^^^^^^^

有没有办法为这种类型编写我自己的序列化程序?或者有没有更简单的方法? 我不想在 json 文件上有一个字符串,我想有实际的字节。这是一个小文件,因此在 json 中存储为字节数组是有意义的。

【问题讨论】:

    标签: json flutter dart


    【解决方案1】:

    为 Uint8List 添加自定义 JSON 转换器

    import 'dart:typed_data';
    import 'package:json_annotation/json_annotation.dart';
    
    class Uint8ListConverter implements JsonConverter<Uint8List, List<int>> {
      const Uint8ListConverter();
    
      @override
      Uint8List fromJson(List<int> json) {
        if (json == null) {
          return null;
        }
    
        return Uint8List.fromList(json);
      }
    
      @override
      List<int> toJson(Uint8List object) {
        if (object == null) {
          return null;
        }
    
        return object.toList();
      }
    }
    

    在 Uint8List 属性上使用 Uint8ListConverter。 在你的情况下:

    import 'package:json_annotation/json_annotation.dart';
    import 'dart:typed_data';
    import 'package:.../uint8_list_converter.dart';
    
    part 'open_vpn.g.dart';
    
    @JsonSerializable(explicitToJson: true)
    class OpenVPN {
      OpenVPN({this.profile});
    
      @Uint8ListConverter()
      Uint8List profile = null;
    
      factory OpenVPN.fromJson(Map<String, dynamic> json) =>
          _$OpenVPNFromJson(json);
    
      Map<String, dynamic> toJson() => _$OpenVPNToJson(this);
    }
    

    在根项目路径下,从终端运行生成open_vpn.g.dart部分文件: flutter packages pub run build_runner build --delete-conflicting-outputs

    【讨论】:

    • 我在每个返回 null 行上都收到此代码的两个错误:“无法从方法 'fromJson' 返回类型为 'Null' 的值,因为它的返回类型为 ' Uint8List'”和“无法从方法'toJson'返回'Null'类型的值,因为它的返回类型为'List'”。
    【解决方案2】:

    在您的OpenVPN 序列化方法中,将Uint8List 转换为List&lt;int&gt;。根据您的实现,它可能类似于:

    class OpenVPN {
      factory OpenVPN.fromJson(dynamic map) {
        return OpenVPN(
            ...
            profile: Uint8List.fromList(map['profile'] ?? []),
        );
      }
    
      toJson() {
        return {
          ...
          'profile': profile as List<int>,
        };
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-03-16
      • 2020-08-25
      • 2019-05-30
      • 2021-09-05
      • 2021-12-20
      • 2019-12-28
      • 1970-01-01
      • 2020-08-25
      • 2020-11-13
      相关资源
      最近更新 更多