您如何编组sql.NullString以便将输出展平以仅给出go中的值? [英] How do you marshal a sql.NullString such that the output is flattened to give just the value in go?

查看:67
本文介绍了您如何编组sql.NullString以便将输出展平以仅给出go中的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个go结构

type Company struct {
    ID   int             `json:"id"`              
    Abn  sql.NullString  `json:"abn,string"`
}

被编组成这样的东西

company := &Company{}
company.ID = 68
company.Abn = "SomeABN"
result, err := json.Marshal(company)

结果是

{
    "id": "68",
    "abn": {
        "String": "SomeABN",
        "Valid": true
    }
}

所需结果是

{
    "id": "68",
    "abn": "SomeABN"
}

我已经尝试过明确指出Abn是一个字符串.

I've tried explicitly stating that Abn is a string.

Abn  sql.NullString  `json:"abn,string"`

没有改变结果.

如何编组sql.NullString以便将输出展平以仅给出go中的值?

How do you marshal a sql.NullString such that the output is flattened to give just the value in go?

编辑

阅读 https://stackoverflow.com/users/8256506/nilsocket和 https://stackoverflow.com/users/965900/mkopriva

package main

import (
    "database/sql"
    "encoding/json"
    "reflect"
    //"github.com/lib/pq"
)

/*
    https://medium.com/aubergine-solutions/how-i-handled-null-possible-values-from-database-rows-in-golang-521fb0ee267
*/

type NullString sql.NullString

func (x *NullString) MarshalJSON() ([]byte, error) {
    if !x.Valid {
        x.Valid = true
        x.String = ""
        //return []byte("null"), nil
    }
    return json.Marshal(x.String)
}

// Scan implements the Scanner interface for NullString
func (ns *NullString) Scan(value interface{}) error {
    var s sql.NullString
    if err := s.Scan(value); err != nil {
        return err
    }

    // if nil then make Valid false
    if reflect.TypeOf(value) == nil {
        *ns = NullString{s.String, false}
    } else {
        *ns = NullString{s.String, true}
    }

    return nil
}

type Company struct {
    ID                     int             `json:"id"`    
    Abn                    NullString      `json:"abn"`         
}

推荐答案

下面是代码,

package main

import (
    "database/sql"
    "encoding/json"
    "fmt"
    "log"
)

//Company details
type Company struct {
    ID  int        `json:"id"`
    Abn NullString `json:"abn"`
}

//NullString is a wrapper around sql.NullString
type NullString sql.NullString

//MarshalJSON method is called by json.Marshal,
//whenever it is of type NullString
func (x *NullString) MarshalJSON() ([]byte, error) {
    if !x.Valid {
        return []byte("null"), nil
    }
    return json.Marshal(x.String)
}

func main() {
    company := &Company{}
    company.ID = 68
    //create new NullString value
    nStr := sql.NullString{String: "hello", Valid: true}
    //cast it
    company.Abn = NullString(nStr)
    result, err := json.Marshal(company)
    if err != nil {
        log.Println(err)
    }
    fmt.Println(string(result))
}

这里是一篇博客文章,对其进行了详细说明.

Here is the blog post which explains it in detail.

这篇关于您如何编组sql.NullString以便将输出展平以仅给出go中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆