encoding/json: Marshal outputs invalid JSON when json.Number contains an invalid number #12793
Comments
Please don't ignore error return values, especially in bug reports. That's not the problem here, but people may not believe the bug report as much if you throw away errors. Better example: https://play.golang.org/p/ezMFRg_v9k package main
import (
"encoding/json"
"fmt"
)
type js struct {
Test1 json.Number `json:"test1"`
Test2 json.Number `json:"test2"`
}
func main() {
res, err := json.Marshal(js{
Test1: "100",
Test2: "100%",
})
fmt.Printf("%s, %v", res, err)
} Outputs:
|
Thank you Brad, That I can think of an option for |
Dup of #10281? |
Yup. |
See this discussion: https://groups.google.com/forum/#!topic/golang-nuts/EErMkG2NWIM
Playground example: http://play.golang.org/p/9ngj404GyR
My service parses JSON data from multiple 3rd party websites and they all provide json differently.. some of them use numbers, some use strings.. after receiving json I do several manipulations with it and then pass it to other service.. but it in cases when site returns invalid number - my json becomes invalid too which shouldn't happen by design.. json.Number value in this case should contain 0, right?
The text was updated successfully, but these errors were encountered: