Converting UTF-8 Strings to Byte Arrays
Unmarshalling JSON requires a byte slice input, while strings are stored as UTF-8 in Go. This article explores the efficient conversion of UTF-8 strings to byte arrays.
Direct Conversion
Go allows converting strings to byte slices, creating a copy of the string's bytes:
s := "some text"
b := []byte(s)
However, for large strings, this approach is inefficient due to the copying.
Using io.Reader
An efficient alternative is using strings.NewReader() to create an io.Reader that reads from the string without copying:
s := `{ "somekey": "somevalue" }`
var result interface{}
err := json.NewDecoder(strings.NewReader(s)).Decode(&result)
This approach avoids copying the string.
Small JSON Texts
For small JSON texts, direct conversion remains a fast option:
s := `{ "somekey": "somevalue" }`
var result interface{}
err := json.Unmarshal([]byte(s), &result)
Note: When reading JSON from an io.Reader (e.g., file or network), pass the io.Reader directly to json.NewDecoder() without intermediate string reads.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3