dongrang2186 2016-05-26 10:44
浏览 55
已采纳

Golang-通过接口分两步解组/重建对象

I'm working with json and golang. I've made a TCP server and, I need to Unmarshal the message to know which type of service is asked, before Unmarshal the contained data. It's a bit hard to explain so this is my code:

package main

import (
    "fmt"
    "encoding/json"
)

type Container struct {
    Type string
    Object interface{}
}

type Selling struct {
    Surname string
    Firstname string
    //......
    Price int
}

type Buying struct {
    ID int
    Surname string
    Firstname string
    //..........
}

/*
type Editing struct {
    ID int
    ...............
}
Informations, etc etc
*/

func main() {

    tmp_message_json1 := Selling{Surname: "X", Firstname: "Mister", Price: 10}
    //tmp_message_json1 := Buying{ID: 1, Surname: "X", Firstname: "Mister"}
    tmp_container_json1 := Container{Type: "Selling", Object: tmp_message_json1}
    json_tmp, _ := json.Marshal(tmp_container_json1)

/*...........
We init tcp etc etc and then a message comes up !
...........*/

    c := Container{}
    json.Unmarshal(json_tmp, &c)
    //I unmarshal a first time to know the type of service asked

    //  first question: Does Unmarshal need to be used only one time?
    // does I need to pass c.Object as a string to unmarshal it in the called functions?
    if c.Type == "Buying" {
        takeInterfaceBuying(c.Object)
    } else if c.Type == "client" {
        takeInterfaceSelling(c.Object)
    } else {
        fmt.Println("bad entry")
    }
}

func takeInterfaceBuying(Object interface{}) {

    bu := Object

    fmt.Println(bu.Firstname, bu.Surname, "wants to buy the following product:", bu.ID)
}

func takeInterfaceSelling(Object interface{}) {

    se := Object

    fmt.Println(se.Firstname, se.Surname, "wants to sell something for:", se.Price)
}

I need to handle messages which comes up like it, but I don't know if it's possible? does it possible?

Thank for help !

  • 写回答

2条回答 默认 最新

  • dongye9228 2016-05-26 11:29
    关注

    There is json.RawMessage for this purpose.

    package main
    
    import (
        "encoding/json"
        "fmt"
    )
    
    type Container struct {
        Type   string
        Object json.RawMessage
    }
    
    type Selling struct {
        Surname   string
        Firstname string
        Price     int
    }
    
    type Buying struct {
        ID        int
        Surname   string
        Firstname string
    }
    
    func main() {
        rawJson1 := []byte(`{"Type":"Selling","Object":{"Surname":"X","Firstname":"Mister","Price":10}}`)
        rawJson2 := []byte(`{"Type":"Buying","Object":{"ID":1,"Surname":"X","Firstname":"Mister"}}`)
    
        processMessage(rawJson1)
        processMessage(rawJson2)
    }
    
    func processMessage(data []byte) {
        var c Container
        json.Unmarshal(data, &c)
    
        switch {
        case c.Type == "Buying":
            processBuying(c)
        case c.Type == "Selling":
            processSelling(c)
        default:
            fmt.Println("bad entry")
        }
    }
    
    func processBuying(c Container) {
        var bu Buying
        json.Unmarshal(c.Object, &bu)
        fmt.Println(bu.Firstname, bu.Surname, "wants to buy the following product:", bu.ID)
    }
    
    func processSelling(c Container) {
        var se Selling
        json.Unmarshal(c.Object, &se)
        fmt.Println(se.Firstname, se.Surname, "wants to sell something for:", se.Price)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 表达式必须是可修改的左值
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题