Skip to content
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

encoding/json: Add support for line breaks for base64 input to json.Unmarshal() #16578

Closed
flimzy opened this issue Aug 2, 2016 · 2 comments
Closed

Comments

@flimzy
Copy link
Contributor

flimzy commented Aug 2, 2016

I would like to see encoding/json support email-style formatted base64 input. That is to say, I would like to see the following code not panic.

var data []byte = []byte(`
["VGhpcyBpcyBhIGxvbmcgdGV4dCBmaWxlLCBqdXN0IGZpbGxlZCBpbiB3aXRoIHJhbmRvbSB0ZXh0
IHRvIGZvcmNlIGxpbmUKd3JhcHBpbmcgb25jZSBpdCdzIEJhc2U2NC1lbmNvZGVkLgo="]
`)

func main() {
    dec := make([]string,0)
    err := json.Unmarshal(data, &dec)
    if err != nil {
        panic(err)
    }
}

With the current implementation, however, it does panic:

panic: invalid character '\n' in string literal

Why? It would make it easier to write readable unit tests of code that does a lot of marshaling and unmarshaling of custom data types.

I can't imagine it would be especially expensive to support this feature always, as I imagine simply filtering out all whitespace from the input stream would do it. But if it did prove to be expensive, supporting it only optionally some how would be reasonable for me as well.

@cespare
Copy link
Contributor

cespare commented Aug 2, 2016

Your input isn't valid JSON, though. As http://www.json.org/ says, strings cannot contain control characters -- and that's what the error message indicates.

@flimzy
Copy link
Contributor Author

flimzy commented Aug 2, 2016

Ah quite. Then any implementation ought to be strictly optional. But that does seem rather evil then.

@flimzy flimzy closed this as completed Aug 2, 2016
@golang golang locked and limited conversation to collaborators Aug 2, 2017
@mikioh mikioh changed the title Add support for line breaks for base64 input to json.Unmarshal() encoding/json: Add support for line breaks for base64 input to json.Unmarshal() Aug 3, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants