Home > Article > Backend Development > 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>
Output:
Hello [72 101 108 108 111] {"Content":"SGVsbG8="}
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>
<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>
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!