1234567891011121314151617181920212223242526 |
- import 'dart:convert';
- import 'package:json_annotation/json_annotation.dart';
- /// Json解析num类型
- class StringConverter extends JsonConverter<String?, dynamic> {
- const StringConverter();
- @override
- String? fromJson(json) {
- if (json == null) return null;
- // String
- if (json is String) return json;
- // num boolean
- if (json is num || json is bool) return json.toString();
- // List Map Object 转换Json字符串
- if (json is List || json is Map || json is Object)
- return JsonEncoder().convert(json);
- // 其他类型
- return json.toString();
- }
- @override
- toJson(String? object) => object;
- }
|