Home > Backend Development > Golang > How Can json.RawMessage Prevent Double Unmarshaling in Go When Handling Heterogeneous JSON Structures?

How Can json.RawMessage Prevent Double Unmarshaling in Go When Handling Heterogeneous JSON Structures?

Mary-Kate Olsen
Release: 2024-12-27 21:34:10
Original
974 people have browsed it

How Can json.RawMessage Prevent Double Unmarshaling in Go When Handling Heterogeneous JSON Structures?

Using RawMessage to Avoid Double Unmarshaling in Go

In Go, when working with JSON data, it is sometimes necessary to partially unmarshal the data in order to access unknown fields or handle heterogeneous JSON structures. However, this process can lead to double unmarshaling, which can be inefficient. This article presents a solution to avoid this issue using json.RawMessage.

Consider a scenario where a web socket connection sends different types of JSON messages, and you wish to unmarshal them into known structs. The traditional approach involves unmarshaling the JSON into a generic map[string]interface{}, searching for the desired key, and then attempting to cast the value to the desired type. However, this approach can fail when working with nested JSON structures.

To resolve this issue, instead of unmarshaling the values into interface{}, we can unmarshal them into json.RawMessage. This allows us to access the raw JSON data without losing the type information. Later, in the switch statement, we can directly unmarshal the raw JSON data into the desired struct without the need to re-marshal it.

import (
    "encoding/json"
    "fmt"
)

type Ping struct {
    Ping string `json:"ping"`
}

type Ack struct {
    Messages []Message `json:"messages"`
}

type Message string

func main() {
    testJSON := []byte(`{"ack":{"messages":["Hi there","Hi again"]}}`)
    var myAck = Ack{}
    var myMap map[string]json.RawMessage
    err := json.Unmarshal(testJSON, &myMap)
    if err != nil {
        fmt.Println("error unmarshalling: ", err)
    }

    for k, v := range myMap {
        fmt.Printf("key: %s, value: %s \n", k, v)

        switch k {
        case "ping":
            fmt.Println(k, " is a ping", v)
        case "ack":
            fmt.Println(k, " is an ack containing a message list")
            err = json.Unmarshal(v, &myAck)
            if err != nil {
                fmt.Println("unmarshal error", err)
            } else {
                fmt.Println("New ack object: ", myAck)
            }
        default:
            fmt.Printf("%s is of a type (%T) I don't know how to handle", k, v)
        }
    }
}
Copy after login

With this approach, we avoid the redundant step of marshalling and unmarshalling the JSON data, making it more efficient and straightforward to handle JSON data with heterogeneous structures.

The above is the detailed content of How Can json.RawMessage Prevent Double Unmarshaling in Go When Handling Heterogeneous JSON Structures?. 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