-
Notifications
You must be signed in to change notification settings - Fork 53
Expose Unescape + AppendUnescape helper functions #62
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -161,6 +161,25 @@ func AppendEscape(b []byte, s string, flags AppendFlags) []byte { | |
return b | ||
} | ||
|
||
// Unescape is a convenience helper to unescape a JSON value. | ||
// For more control over the unescape behavior and | ||
// to write to a pre-allocated buffer, use AppendUnescape. | ||
func Unescape(s []byte) []byte { | ||
b := make([]byte, 0, len(s)) | ||
return AppendUnescape(b, s, ParseFlags(0)) | ||
} | ||
|
||
// AppendUnescape appends s to b with the string unescaped as a JSON value. | ||
// This will remove starting and ending quote characters, and the | ||
// appropriate characters will be escaped correctly as if JSON decoded. | ||
// New space will be reallocated if more space is needed. | ||
func AppendUnescape(b []byte, s []byte, flags ParseFlags) []byte { | ||
d := decoder{flags: flags} | ||
buf := new(string) | ||
d.decodeString(s, unsafe.Pointer(buf)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not super familiar with this code, so bear with me here. 😁 Is it possible to decode straight into There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unlike There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess you'd have to reimplement the handling of parsing flag, maybe that's not the right path. |
||
return append(b, *buf...) | ||
} | ||
|
||
// Compact is documented at https://golang.org/pkg/encoding/json/#Compact | ||
func Compact(dst *bytes.Buffer, src []byte) error { | ||
return json.Compact(dst, src) | ||
|
Uh oh!
There was an error while loading. Please reload this page.