Join GitHub today
GitHub is home to over 36 million developers working together to host and review code, manage projects, and build software together.Sign up
encoding/json: Encode results in invalid json if json.Number is not a valid number #10281
When encoding a struct with a
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
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 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:
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: