1234567891011121314151617181920212223242526272829303132 |
- import 'package:json_annotation/json_annotation.dart';
- import '../main.dart';
- /// Json解析num类型
- class NumConverter extends JsonConverter<num?, dynamic> {
- const NumConverter();
- @override
- num? fromJson(json) {
- if (json == null) return null;
- if (json is num) return json;
- // String
- if (json is String) {
- try {
- return num.parse(json);
- } catch (e) {
- logger.e(e);
- return null;
- }
- }
- // boolean false=0 true=1
- if (json is bool) {
- return json ? 1 : 0;
- }
- return null;
- }
- @override
- toJson(num? object) => object?.toString();
- }
|