How can I implement UnmarshalJSON for a type derived from a scalar in Go?

前端 未结 2 1751
执笔经年
执笔经年 2021-01-15 04:25

I have a simple type that implements conversion of subtyped integer consts to strings and vice versa in Go. I want to be able to automatically unmarshal strings in JSON to v

2条回答
  •  滥情空心
    2021-01-15 04:39

    Here's an answer based on my comment. I'm not sure this is exactly what you want to do as some of your questions wording confuses me however the basic idea is to separate unmarshalling and transformation into two different steps. First unmarshal raw data into a compatible type, after do a transformation to another type or enrich the type you already have like in the example below. You're welcome to hide this behavior in a custom implementation of UnmarshalJSON if you'd like but I would personally advise against it. Here's my two reasons; 1) it's just not consistent with Go's explicit verbose coding style 2) I despise highly obfuscated packages/libraries/languages that do stuff like this for you because sooner or later it bites you in the ass an costs you a lot more than adding that 1 line of extra code in a few places (like hours trying to debug something that makes no sense to you).

    type MyType struct {
        Id   PersonID
        Name     string   `json: "name"` 
        Count    int      `json: "count"`
        Greeting string   `json: "greeting"`
    }
    
    
    func main() {
        var m MyType
        if err := json.Unmarshal([]byte(`{"name": "Ralph", "count": 4, "greeting": "Hello"}`), &m); err != nil {
            fmt.Println(err)
        } else {
            m.Id = Lookup(m.Name) // see this isn't unmarshalling
            // better to take the data as is and do transformation separate
            for i := 0; i < m.Count; i++ {
                fmt.Println(m.Greeting, m.Person.Name())
            }
        }
    }
    

提交回复
热议问题