Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

ParseBytes no longer copies data + added result.Bytes()#383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
Yoone wants to merge1 commit intotidwall:master
base:master
Choose a base branch
Loading
fromYoone:master

Conversation

@Yoone
Copy link

Summary

  • ParseBytes(json []byte) Result now uses thebytesString helper function (unsafe cast) overstring(...) conversion to avoid copying the byte slice.
  • A newresult.Bytes() helper was added to accessresult.Raw as[]byte without copying data.

Motivation

This change aims at allowing the use of[]byte overstring as input when usinggjson without having to copy data or perform extra memory allocations, since one of the selling points of this library is its ability to parse and process JSON with no copy / zero allocations.

mirecl reacted with thumbs up emoji
@mirecl
Copy link

@tidwall , what do you think about this change?

@tidwall
Copy link
Owner

This PR breaks the rules of immutability for strings in Go.

Using the change in this PR, run this code:

json:= []byte(`{ "first": "Janet", "last": "Prichard" }`)first:=gjson.ParseBytes(json).Get("first").String()println(first)copy(json[11:18],`"Carol"`)println(first)// Output:// Janet// Carol

It should not be possible to change the backed memory of a string, but in the example above the stringfirst was changed from "Janet" to "Carol".

This is because thejson byte slice was unsafely cast as a string in ParseBytes and returned as a string field in theResult type. Then further calls toResult expects that string to be immutable.

A safer workaround would be to cast before parse, then immediately copy the result (which contains a mutable string), back into an immutable string.

json:= []byte(`{ "first": "Janet", "last": "Prichard" }`)jsonStr:=*(*string)(unsafe.Pointer(&json))// cast []bytes to string, zero-copyfirst:=Parse(jsonStr).Get("first").String()// use Parse instead of ParseBytesfirst=string([]byte(first))// copy result back into stringprintln(first)copy(json[11:18],`"Carol"`)println(first)// Output:// Janet// Janet

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

3 participants

@Yoone@mirecl@tidwall

[8]ページ先頭

©2009-2025 Movatter.jp