drgmszo076956 2015-10-20 11:57
浏览 333
已采纳

重命名JSON字段

We want to rename a JSON field value to v in production. Till all our users use the new struct, we would continue to get old JSON structs into our code. So we want to handle this as well.

If you notice, First is the original structure, Second is the new structure. To handle both these structures, I have created a MyStruct and based on version, I copy the OldValue into Value

if m.Version <= 1 {
    m.Value = m.OldValue
}

Is there a better way to handle this, instead of my code.

Go Playground Link

package main

import "fmt"
import "encoding/json"
import "log"

type First struct {
    Version int `json:"version"`
    Value   int `json:"value"`
}

type Second struct {
    Version int `json:"version"`
    Value   int `json:"v"`
}

type MyStruct struct {
    Version  int `json:"version"`
    OldValue int `json:"value"`
    Value    int `json:"v"`
}

func main() {
    oldValue := []byte(`{"version":1, "value":5}`)
    newValue := []byte(`{"version":2, "v":7}`)

    var m MyStruct

    err := json.Unmarshal(newValue, &m)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("New Struct")
    fmt.Println(m.Value)

    err = json.Unmarshal(oldValue, &m)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("Old Struct")
    if m.Version <= 1 {
        m.Value = m.OldValue
    }
    fmt.Println(m.Value)

}
  • 写回答

2条回答 默认 最新

  • donglu9825 2015-10-20 12:48
    关注

    EDIT: You can actually do it with one unmarshaling, albeit you'll need another type:

    type Second struct {
        Version int `json:"version"`
        Value   int `json:"v"`
    }
    
    type SecondWithOldValue struct {
        OldValue int `json:"value"`
        Second
    }
    
    type MyStruct SecondWithOldValue
    
    func (v *MyStruct) UnmarshalJSON(b []byte) error {
        if err := json.Unmarshal(b, (*SecondWithOldValue)(v)); err != nil {
            return err
        }
    
        if v.Version <= 1 {
            v.Value = v.OldValue
        }
        return nil
    }
    

    Playground: https://play.golang.org/p/yII-ncxnU4.

    Old answer below.


    If you're OK with double unmarshaling, you can do it like this:

    type Second struct {
        Version int `json:"version"`
        Value   int `json:"v"`
    }
    
    type MyStruct struct {
        Second
    }
    
    func (v *MyStruct) UnmarshalJSON(b []byte) error {
        if err := json.Unmarshal(b, &v.Second); err != nil {
            return err
        }
    
        if v.Version <= 1 {
            var oldV struct{ Value int }
            if err := json.Unmarshal(b, &oldV); err != nil {
                return err
            }
            v.Value = oldV.Value
        }
        return nil
    }
    

    First, unmarshal into the inner struct, check version, and if it's an old one, get the old value.

    Playground: https://play.golang.org/p/AaULW6vJz_.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 机器学习训练相关模型
  • ¥15 Todesk 远程写代码 anaconda jupyter python3
  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制