Removing fields from struct or hiding them in JSON Response

后端 未结 13 1419
孤街浪徒
孤街浪徒 2020-12-12 09:37

I\'ve created an API in Go that, upon being called, performs a query, creates an instance of a struct, and then encodes that struct as JSON before sending back to the caller

13条回答
  •  渐次进展
    2020-12-12 10:21

    I also faced this problem, at first I just wanted to specialize the responses in my http handler. My first approach was creating a package that copies the information of a struct to another struct and then marshal that second struct. I did that package using reflection, so, never liked that approach and also I wasn't dynamically.

    So I decided to modify the encoding/json package to do this. The functions Marshal, MarshalIndent and (Encoder) Encode additionally receives a

    type F map[string]F

    I wanted to simulate a JSON of the fields that are needed to marshal, so it only marshals the fields that are in the map.

    https://github.com/JuanTorr/jsont

    package main
    
    import (
        "fmt"
        "log"
        "net/http"
    
        "github.com/JuanTorr/jsont"
    )
    
    type SearchResult struct {
        Date        string      `json:"date"`
        IdCompany   int         `json:"idCompany"`
        Company     string      `json:"company"`
        IdIndustry  interface{} `json:"idIndustry"`
        Industry    string      `json:"industry"`
        IdContinent interface{} `json:"idContinent"`
        Continent   string      `json:"continent"`
        IdCountry   interface{} `json:"idCountry"`
        Country     string      `json:"country"`
        IdState     interface{} `json:"idState"`
        State       string      `json:"state"`
        IdCity      interface{} `json:"idCity"`
        City        string      `json:"city"`
    } //SearchResult
    
    type SearchResults struct {
        NumberResults int            `json:"numberResults"`
        Results       []SearchResult `json:"results"`
    } //type SearchResults
    func main() {
        msg := SearchResults{
            NumberResults: 2,
            Results: []SearchResult{
                {
                    Date:        "12-12-12",
                    IdCompany:   1,
                    Company:     "alfa",
                    IdIndustry:  1,
                    Industry:    "IT",
                    IdContinent: 1,
                    Continent:   "america",
                    IdCountry:   1,
                    Country:     "México",
                    IdState:     1,
                    State:       "CDMX",
                    IdCity:      1,
                    City:        "Atz",
                },
                {
                    Date:        "12-12-12",
                    IdCompany:   2,
                    Company:     "beta",
                    IdIndustry:  1,
                    Industry:    "IT",
                    IdContinent: 1,
                    Continent:   "america",
                    IdCountry:   2,
                    Country:     "USA",
                    IdState:     2,
                    State:       "TX",
                    IdCity:      2,
                    City:        "XYZ",
                },
            },
        }
        fmt.Println(msg)
        http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    
            //{"numberResults":2,"results":[{"date":"12-12-12","idCompany":1,"idIndustry":1,"country":"México"},{"date":"12-12-12","idCompany":2,"idIndustry":1,"country":"USA"}]}
            err := jsont.NewEncoder(w).Encode(msg, jsont.F{
                "numberResults": nil,
                "results": jsont.F{
                    "date":       nil,
                    "idCompany":  nil,
                    "idIndustry": nil,
                    "country":    nil,
                },
            })
            if err != nil {
                log.Fatal(err)
            }
        })
    
        http.ListenAndServe(":3009", nil)
    }
    

提交回复
热议问题