douyong8801 2017-10-26 19:33
浏览 977

sql.NullString || sql.NullInt64转换为普通格式json是否为null? [关闭]

Often happens in relational database, that when selecting records from the database, the field is returned. Go does not process strings and numbers as nil, in this case there is an auxiliary type of sql.Null {type} But if you return data in json format, then use of this type becomes unnecessary since, in javascript, there is null.

Now I sql.NullString convert to manual. Also there is a variant in sql itself to return, not null, and an empty line (if it is a line), but for some reasons in the future it will be difficult to debug.

How do you solve this problem?

  • 写回答

1条回答 默认 最新

  • douqiang7976 2017-10-26 20:16
    关注

    Your question is a little confusing but I think I understand what you are asking. Inspired by another post that I can no longer find, I wrote wrappers around the sql.Null types to encode JSON as primitive types. See example below:

    import (
        "database/sql"
        "encoding/json"
    )
    
    type JsonNullInt64 struct {
        sql.NullInt64
    }
    
    type JsonNullFloat64 struct {
        sql.NullFloat64
    }
    
    type JsonNullString struct {
        sql.NullString
    }
    
    type JsonNullBool struct {
        sql.NullBool
    }
    
    func (v *JsonNullBool) MarshalJSON() ([]byte, error) {
        if v.Valid {
            return json.Marshal(v.Bool)
        } else {
            return json.Marshal(nil)
        }
    }
    
    func (v *JsonNullBool) UnmarshalJSON(data []byte) error {
        var b *bool
        if err := json.Unmarshal(data, &b); err != nil {
            return err
        }
    
        if b != nil {
            v.Valid = true
            v.Bool = *b
        } else {
            v.Valid = false
        }
    
        return nil
    }
    
    func (v *JsonNullString) MarshalJSON() ([]byte, error) {
        if v.Valid {
            return json.Marshal(v.String)
        } else {
            return json.Marshal(nil)
        }
    }
    
    func (v *JsonNullString) UnmarshalJSON(data []byte) error {
        var str *string
        if err := json.Unmarshal(data, &str); err != nil {
            return err
        }
    
        if str != nil {
            v.Valid = true
            v.String = *str
        } else {
            v.Valid = false
        }
    
        return nil
    }
    
    func (v JsonNullFloat64) MarshalJSON() ([]byte, error) {
        if v.Valid {
            return json.Marshal(v.Float64)
        } else {
            return json.Marshal(0)
        }
    }
    
    func (v *JsonNullFloat64) UnmarshalJSON(data []byte) error {
        // Unmarshalling into a pointer will let us detect null
    
        var x *float64
        err := json.Unmarshal(data, &x)
        if err != nil {
            return err
        }
        if x != nil {
            v.Valid = true
            v.Float64 = *x
        } else {
            v.Valid = false
        }
        return nil
    }
    
    func (v JsonNullInt64) MarshalJSON() ([]byte, error) {
        if v.Valid {
            return json.Marshal(v.Int64)
        } else {
            return json.Marshal(0)
        }
    }
    
    func (v JsonNullInt64) UnmarshalJSON(data []byte) error {
        // Unmarshalling into a pointer will let us detect null
        var x *int64
        if err := json.Unmarshal(data, &x); err != nil {
            return err
        }
        if x != nil {
            v.Valid = true
            v.Int64 = *x
        } else {
            v.Valid = false
        }
        return nil
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题