How do you handle []byte fields in Go when encoding and decoding JSON?

DDD
Release: 2024-11-06 15:07:02
Original
350 people have browsed it

How do you handle []byte fields in Go when encoding and decoding JSON?

Encoding and Decoding JSON with []byte Fields in Go

In Go, working with JSON data often involves encoding and decoding data structures to and from the JSON format. One common scenario is encountered when attempting to serialize strings represented as []byte fields into JSON.

Base64 Conversion by json.Marshal()

By default, the json.Marshal() method treats []byte fields specially. Instead of serializing them as raw bytes, it encodes them as base64-encoded strings. This conversion is necessary because JSON does not have a native representation for binary data.

Deviations from Expected Output

To illustrate this behavior, consider the following code snippet:

<code class="go">package main

import (
    "fmt"
    "encoding/json"
)

type Msg struct {
    Content []byte
}

func main() {
    helloStr := "Hello"
    helloSlc := []byte(helloStr)
    fmt.Println(helloStr, helloSlc)

    obj := Msg{helloSlc}
    json, _ := json.Marshal(obj)
    fmt.Println(string(json))
}</code>
Copy after login

Output:

Hello [72 101 108 108 111]
{"Content":"SGVsbG8="}
Copy after login

As you can see, the JSON string contains the base64-encoded version of the "Hello" string instead of the original string itself.

Understanding the Conversion

The reason for this behavior is rooted in the JSON specification, which lacks a native representation for raw bytes. By base64-encoding the []byte field, json.Marshal() ensures compatibility with the JSON format while preserving the integrity of the original data.

Handling Custom Encoding

If you prefer to preserve the raw bytes rather than base64-encode them, you can implement custom serialization and deserialization logic. This typically involves overriding the MarshalJSON() and UnmarshalJSON() methods of your struct.

Custom marshaling examples:

<code class="go">func (m *Msg) MarshalJSON() ([]byte, error) {
    type Alias Msg
    return json.Marshal((*Alias)(m))
}</code>
Copy after login
<code class="go">func (m *Msg) UnmarshalJSON(b []byte) error {
    type Alias Msg
    var a Alias
    if err := json.Unmarshal(b, &a); err != nil {
        return err
    }
    *m = Msg(a)
    return nil
}</code>
Copy after login

The above is the detailed content of How do you handle []byte fields in Go when encoding and decoding JSON?. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!