在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(Map<String, dynamic> json) => _$MyObjFromJson(json);
Map<String, dynamic> 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(map<string, dynamic> json) => _$myobjfromjson(json);
map<string, dynamic> 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<customdatetime, string> {
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(map<string, dynamic> json) => _$myobjfromjson(json);
map<string, dynamic> tojson() => _$myobjtojson(this);
}
这满足了代码生成器的要求,瞧!我们可以使用来自 golang time.time 的 rfc3339 时间戳读取 json。
我也遇到了同样的问题。我找到了一个非常简单的解决方案。我们可以将自定义转换器与 jsonconverter 一起使用。如需更多说明,您可以使用我的article。
import 'package:json_annotation/json_annotation.dart';
class customdatetimeconverter implements jsonconverter<datetime, string> {
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(Map<String, dynamic> json) =>
_$PublicationDocumentFromJson(json);
Map<String, dynamic> toJson() => _$PublicationDocumentToJson(this);
}以上就是《Flutter 使用 Golang RFC3339 中的 DateTime 解析 json:FormatException:无效的日期格式》的详细内容,更多关于的资料请关注golang学习网公众号!
本文内容来源于互联网,如有侵权请联系删除。
