-
Notifications
You must be signed in to change notification settings - Fork 18k
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: Encode results in invalid json if json.Number is not a valid number #10281
Comments
Interesting. Too late for Go 1.5 but worth looking into early in Go 1.6. Probably there needs to be a separate routine to validate the form of Also, probably both Marshal and Unmarshal should use it. I think. |
I wrote a simple This method could be used at decode.go#L783 to validate the number before setting it. I guess it's best to return an error when it's not valid. The same goes for encode.go#L532. If you want I can submit a diff for this using the Contribution Guidelines? The function is based on this official JSON diagram: It is quite a lot faster than using a regexp based method:
|
Yes, please mail your change, thanks. Note that the tree is frozen for Go 1.5, so a reviewer will mostly likely mark the change as being for Go 1.6, and it won't be reviewed until the tree reopens. |
json.Number is a special case which didn't have any checks and could result in invalid JSON. Fixes golang#10281 Change-Id: Ie3e726e4d6bf6a6aba535d36f6107013ceac913a
CL https://golang.org/cl/12250 mentions this issue. |
json.Number is a special case which didn't have any checks and could result in invalid JSON. Fixes golang#10281 Change-Id: Ie3e726e4d6bf6a6aba535d36f6107013ceac913a
json.Number is a special case which didn't have any checks and could result in invalid JSON. Fixes golang#10281 Change-Id: Ie3e726e4d6bf6a6aba535d36f6107013ceac913a
json.Number is a special case which didn't have any checks and could result in invalid JSON. Fixes golang#10281 Change-Id: Ie3e726e4d6bf6a6aba535d36f6107013ceac913a
While fuzzy testing I found some edge cases and fixed them. I also added a regexp benchmark function to show the speed increase from not using the regexp:
|
CL https://golang.org/cl/17199 mentions this issue. |
Followup to CL 12250. For #10281. Change-Id: If25d9cac92f10327bb355f2d11b00c625b464661 Reviewed-on: https://go-review.googlesource.com/17199 Reviewed-by: Ian Lance Taylor <iant@golang.org>
When encoding a struct with a
json.Number
containing an invalid number the resulting json is invalid.See: http://play.golang.org/p/Xx4GjIgL_X
This is because of the exception made at: https://github.com/golang/go/blob/master/src/encoding/json/encode.go#L533
If this is intentional I think it should be documented in the
json.Number
documentation. If not it should be fixed by trying thenumber.Float64()
method first and if it fails writing the result as a String enclosed in quotes.The text was updated successfully, but these errors were encountered: