【玩转Golang】 自定义json序列化对象时,非法字符错误原因
由于前台web页面传来的日期对象是这样的格式“2010-11-03 15:23:22”,所以我安装网上查来的办法,自定义包装了time.Time对象,实现自己的Marshal和UnMarshal方法
type DateTime struct {
time.Time
}
const ctLayout = "2006-01-02 15:04:05"
const ctLayout_nosec = "2006-01-02 15:04"
const ctLayout_date = "2006-01-02"
func (this *DateTime) UnmarshalJSON(b []byte) (err error) {
if b[0] == '"' && b[len(b)-1] == '"' {
b = b[1 : len(b)-1]
}
loc, err := time.LoadLocation("Asia/Shanghai")
if err != nil {
panic(err)
}
sv := string(b)
if len(sv) == 10 {
sv += " 00:00:00"
} else if len(sv) == 16 {
sv += ":00"
}
this.Time, err = time.ParseInLocation(ctLayout, string(b), loc)
if err != nil {
if this.Time, err = time.ParseInLocation(ctLayout_nosec, string(b), loc); err != nil {
this.Time, err = time.ParseInLocation(ctLayout_date, string(b), loc)
}
}
return
}
func (this *DateTime) MarshalJSON() ([]byte, error) {
rs := []byte(this.Time.Format(ctLayout))
return rs, nil
}
var nilTime = (time.Time{}).UnixNano()
func (this *DateTime) IsSet() bool {
return this.UnixNano() != nilTime
}
然后,把结构中声明为time.Time的都修改为自定义的类型DateTime,试了一下,发现已经可以正确解析网页发来的时间,但是在输出时,总是不对,好像并没有调用自定义的Marshal方法。编写测试方法发现,原来json.Marshal方法调用DateTime.Marshal时出错了!
invalid character '-' after top-level value
开始没有认真看,以为“-”号不合法,就换了一个,结果错误一样:
invalid character '/' after top-level value
看来,根本不是分割符的问题,仔细分析错误,发现“top-level”字样,我这返回的就是一个字符串,怎么可能top-level呢!想到这儿突然醒悟,是不是返回字符串应该自己加引号呢,急忙修改代码一试,果然!~_~
最终代码如下:
type DateTime struct {
time.Time
}
const ctLayout = "2006-01-02 15:04:05"
const ctLayout_nosec = "2006-01-02 15:04"
const ctLayout_date = "2006-01-02"
func (this *DateTime) UnmarshalJSON(b []byte) (err error) {
if b[0] == '"' && b[len(b)-1] == '"' {
b = b[1 : len(b)-1]
}
loc, err := time.LoadLocation("Asia/Shanghai")
if err != nil {
panic(err)
}
sv := string(b)
if len(sv) == 10 {
sv += " 00:00:00"
} else if len(sv) == 16 {
sv += ":00"
}
this.Time, err = time.ParseInLocation(ctLayout, string(b), loc)
if err != nil {
if this.Time, err = time.ParseInLocation(ctLayout_nosec, string(b), loc); err != nil {
this.Time, err = time.ParseInLocation(ctLayout_date, string(b), loc)
}
}
return
}
func (this *DateTime) MarshalJSON() ([]byte, error) {
rs := []byte(fmt.Sprintf(`"%s"`, this.Time.Format(ctLayout)))
return rs, nil
}
var nilTime = (time.Time{}).UnixNano()
func (this *DateTime) IsSet() bool {
return this.UnixNano() != nilTime
}
【玩转Golang】 自定义json序列化对象时,非法字符错误原因的更多相关文章
- EF中Json序列化对象时检测到循环引用的解决办法
MVC4 EF中将数据表外键引用的是自身,转换成Json时,总是提示错误:“序列化类型为....的对象时检测到循环引用.”: 解决办法: 把要序列化的对象转为匿名对象去掉导航属性,如下 :本来是var ...
- 使用DataContractJsonSerializer发序列化对象时出现的异常
最近服务器上的某个程序的错误日志中频繁出现以下异常: Deserialising: There was an error deserializing the object of type {type} ...
- .NET 自定义Json序列化时间格式
.NET 自定义Json序列化时间格式 Intro 和 JAVA 项目组对接,他们的接口返回的数据是一个json字符串,里面的时间有的是Unix时间戳,有的是string类型,有的还是空,默认序列化规 ...
- JSON.Net 自定义Json序列化时间格式
JSON.Net 自定义Json序列化时间格式 Intro 和 JAVA 项目组对接,他们的接口返回的数据是一个json字符串,里面的时间有的是Unix时间戳,有的是string类型,有的还是空,默认 ...
- Error serializing object:序列化对象时出错
序列化对象时出错 :Error serializing object. Error serializing object. Cause: java.io.NotSerializableExceptio ...
- 【翻译自mos文章】使用aum( Automatic Undo Management) 时遇到 ORA-01555错误--- 原因和解决方式。
使用aum( Automatic Undo Management) 时遇到 ORA-01555错误--- 原因和解决方式. 參考原文: ORA-01555 Using Automatic Undo M ...
- SpringBoot之解决一对一、多对一、多对多等关联实体在JSON序列化/输出时产生的无限递归死循环问题(infinite recursion)
前言 这问题着实让人苦不堪言,有必要把它记下了. @JsonBackReference [亲测有效] 1.使用注解@JsonBackReference标记在有关联关系的实体属性上 2.仅导入此注解类有 ...
- Json序列化对象
之前都是用的二进制的序列化方法,是.net自带的,但是最常用到的还是Json序列化 (1)只需要调用 Newtonsoft.Json.dll 即可 public class JsonTools { / ...
- Gson序列化对象时排除字段
import com.google.gson.ExclusionStrategy; import com.google.gson.FieldAttributes; /** *Gson序列化对象排除属性 ...
随机推荐
- select 操作选中添加、删除操作Javascript
//添加选中项 function addItem() { var myMember = document.getElementById("myMember"); var other ...
- [加密]展讯secureboot方案
Secure Boot方案介绍及实施流程 转自网络 1. Secure boot概述 本文档主要是secure boot方案的介绍和说明,其内容会涵盖以下方面:secure boot的目的和介绍.技术 ...
- if 语句练习 身高体重问题
public class d { /** * @param args */ public static void main(String[] args) { // TODO 自动生成的方法存根 int ...
- CPP_异常处理
错误处理:C分散的就近处理:C++集中处理. C++错误处理机制:函数实现中若出现错误想处理,用throw抛出异常.应用程序中,用try...catch...捕获异常处理. 异常按类型捕获:int, ...
- setfacl命令
setfacl命令是用来在命令行里设置ACL(访问控制列表).在命令行里,一系列的命令跟随以一系列的文件名. 选项 -b,--remove-all:删除所有扩展的acl规则,基本的acl规则(所有者, ...
- 编译安装LAMP之安装Apache+php与管理(十五)
[教程主题]:编译安装LAMP之安装Apache+php与管理 [课程录制]: 创E [主要内容] [1]编译安装Apache+PHP 1.安装程序依赖库和开发环境 为了省事把所需要的库文件全都安装上 ...
- Hive Tuning(五) 标准调优清单
Hive的标准调优清单,我们可以对照着来做我们的查询优化!
- dapper支持操作函数和事物
dapper除了支持基础的CURD.存储过程以外,还支持操作函数和事物. dapper操作函数的代码如下: using Dapper; using System; using System.Colle ...
- MongoDB 运行状态、性能监控,分析
这篇文章的目的是让你知道怎么了解你正在运行的Mongdb是否健康.转载自http://tech.lezi.com/archives/290 mongostat详解 启动mongodb监控,通过下面命令 ...
- 1 php protocolbuffers安装
安装工具 yum install autoconf yum install libtool 安装protoc编译器 # cd /root/soft/protobuf- autogen.sh : if ...