Flutter 使用 Golang RFC3339 中的 DateTime 解析 json:FormatException:无效的日期格式
来源:stackoverflow
时间:2024-04-13 08:06:35 113浏览 收藏
在Golang实战开发的过程中,我们经常会遇到一些这样那样的问题,然后要卡好半天,等问题解决了才发现原来一些细节知识点还是没有掌握好。今天golang学习网就整理分享《Flutter 使用 Golang RFC3339 中的 DateTime 解析 json:FormatException:无效的日期格式》,聊聊,希望可以帮助到正在努力赚钱的你。
当尝试在 dart / flutter 中读取使用 golangs json 包生成的 json 文件时,我注意到解析日期会产生错误:
formatexception: invalid date format
示例是在 go 服务器上生成的以下 json:
333718689354我正在使用 json(反)序列化的代码生成方法来避免编写所有样板代码。 json_serialized 包是可用于此目的的标准包。所以我的代码如下所示:
@JsonSerializable() class MyObj { DateTime dateCreated; MyObj( this.dateCreated); factory MyObj.fromJson(Mapjson) => _$MyObjFromJson(json); Map toJson() => _$MyObjToJson(this); }
解决方案
因为文档没有充分涵盖这个问题,所以我花了一天的时间研究颤振源并尝试和错误不同的东西来解决它。所以不妨分享一下。
当序列化为 json 时,golang 默认对 rfc3339 中的 time.time 进行编码(如给定的示例)。 flutter明确支持rfc3339,那么为什么它不起作用呢?答案是秒小数部分的支持方式略有不同。虽然 golang 产生 7 位数字的精度,但 dart 最多只支持 6 位数字,并且不能优雅地处理违规。因此,如果示例被更正为只有 6 位精度,它将在 dart 中正常解析:
{ ... "datecreated": "2018-09-29t19:51:57.413978-07:00", ... }
为了以通用方式解决此问题,您有两个选择:1. 截断字符串中的额外精度,或 2. 实现您自己的解析。假设我们扩展了 datetime
类并创建您自己的 customdatetime
。新类重写了 parse
方法,以在将其传递给父类的解析方法之前删除 6 位数字后的所有多余内容。
现在我们可以在 dart 类中使用 customdatetime
了。例如:
@jsonserializable() class myobj { customdatetime datecreated; myobj( this.datecreated); factory myobj.fromjson(mapjson) => _$myobjfromjson(json); map tojson() => _$myobjtojson(this); }
但是现在代码生成当然被破坏了,我们收到以下错误:
error running jsonserializablegenerator could not generate 'tojson' code for 'datecreated'. none of the provided 'typehelper' instances support the defined type.
幸运的是,json_annotation
包现在为我们提供了一个简单的解决方案 - jsonconverter
。以下是如何在我们的示例中使用它:
首先定义一个转换器,向代码生成器解释如何转换我们的 customdatetime
类型:
class customdatetimeconverter implements jsonconverter{ const customdatetimeconverter(); @override customdatetime fromjson(string json) => json == null ? null : customdatetime.parse(json); @override string tojson(customdatetime object) => object.toiso8601string(); }
其次,我们只需将此转换器注释到使用 customdatetime 数据类型的每个类:
@jsonserializable() @customdatetimeconverter() class myobj { customdatetime datecreated; myobj( this.datecreated); factory myobj.fromjson(mapjson) => _$myobjfromjson(json); map tojson() => _$myobjtojson(this); }
这满足了代码生成器的要求,瞧!我们可以使用来自 golang time.time 的 rfc3339 时间戳读取 json。
我也遇到了同样的问题。我找到了一个非常简单的解决方案。我们可以将自定义转换器与 jsonconverter 一起使用。如需更多说明,您可以使用我的article。
import 'package:json_annotation/json_annotation.dart'; class customdatetimeconverter implements jsonconverter{ const customdatetimeconverter(); @override datetime fromjson(string json) { if (json.contains(".")) { json = json.substring(0, json.length - 1); } return datetime.parse(json); } @override string tojson(datetime json) => json.toiso8601string(); }
import 'package:json_annotation/json_annotation.dart'; import 'package:my_app/shared/helpers/custom_datetime.dart'; part 'publication_document.g.dart'; @JsonSerializable() @CustomDateTimeConverter() class PublicationDocument { final int id; final int publicationId; final DateTime publicationDate; final DateTime createTime; final DateTime updateTime; final bool isFree; PublicationDocument({ this.id, this.publicationId, this.publicationDate, this.createTime, this.updateTime, this.isFree, }); factory PublicationDocument.fromJson(Mapjson) => _$PublicationDocumentFromJson(json); Map toJson() => _$PublicationDocumentToJson(this); }
以上就是《Flutter 使用 Golang RFC3339 中的 DateTime 解析 json:FormatException:无效的日期格式》的详细内容,更多关于的资料请关注golang学习网公众号!
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习