• 【玩转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
    }
  • 相关阅读:
    java安全——BASE64
    Spring Aop 梳理
    Spring Aop中,获取被代理类的工具
    关于eclipse运行TestNG出现: CreateProcess error=206, ÎļþÃû»ò)չÃû的解决办法
    java内存设置
    用python开发调试器——起始篇
    5分钟破解全峰快递
    重拾黑客
    ThreadPoolExecutor的运转机制
    20190627_解决ADB的device offline问题的两种方法
  • 原文地址:https://www.cnblogs.com/dajianshi/p/4192810.html
Copyright © 2020-2023  润新知