Home > Backend Development > Golang > How to Deserialize JSON Websocket Messages as Union Types in Go?

How to Deserialize JSON Websocket Messages as Union Types in Go?

Linda Hamilton
Release: 2024-11-20 11:18:02
Original
582 people have browsed it

How to Deserialize JSON Websocket Messages as Union Types in Go?

Deserializing JSON Websocket Messages as Union Types in Go

In Go, the gorilla websocket library is commonly used for handling websocket connections. However, when using JSON for serialization and deserialization, handling incoming messages of varying types presents a challenge.

Consider the following example where you have structs for message types "Foo" and "Bar":

type Foo struct {
    A string `json:"a"`
    B string `json:"b"`
}

type Bar struct {
    C string `json:"c"`
    D string `json:"d"`
}
Copy after login

Gorilla's conn.ReadJSON function allows you to deserialize incoming JSON messages into specific structs. However, you would need to use separate conn.ReadJSON(Foo) and conn.ReadJSON(Bar) calls to handle messages of different types, which is inefficient and cumbersome.

To address this, you can use an intermediary struct that contains a control field and a field to hold the actual message data:

type Messages struct {
    Control string `json:"control"`
    X json.RawMessage
}
Copy after login

The Control field indicates the payload's type, and X holds the raw JSON data. To deserialize incoming messages using this approach:

var m Messages
err := c.ReadJSON(&m)
if err != nil {
    // Handle error
}

switch m.Control {
case "Foo":
    var foo Foo
    if err := json.Unmarshal([]byte(m.X), &foo); err != nil {
        // Handle error
    }
    // Do something with foo

case "Bar":
    // Follow the same pattern for handling Bar
}
Copy after login

This solution allows you to deserialize incoming messages regardless of their type, using the RawMessage interface type in json.RawMessage. The switch statement checks the control field to determine the actual message type and deserialize accordingly.

The above is the detailed content of How to Deserialize JSON Websocket Messages as Union Types in Go?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template