Merge pull request #38 from goccy/feature/fix-encoding-json-number

Fix encoding of json.Number
This commit is contained in:
Masaaki Goshima 2020-08-21 01:14:27 +09:00 committed by GitHub
commit 991c8c411b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 0 deletions

View File

@ -1211,3 +1211,25 @@ func TestUnsupportedValues(t *testing.T) {
}
}
}
func TestIssue10281(t *testing.T) {
type Foo struct {
N json.Number
}
x := Foo{json.Number(`invalid`)}
b, err := json.Marshal(&x)
if err == nil {
t.Errorf("Marshal(&x) = %#q; want error", b)
}
}
func TestHTMLEscape(t *testing.T) {
var b, want bytes.Buffer
m := `{"M":"<html>foo &` + "\xe2\x80\xa8 \xe2\x80\xa9" + `</html>"}`
want.Write([]byte(`{"M":"\u003chtml\u003efoo \u0026\u2028 \u2029\u003c/html\u003e"}`))
json.HTMLEscape(&b, []byte(m))
if !bytes.Equal(b.Bytes(), want.Bytes()) {
t.Errorf("HTMLEscape(&b, []byte(m)) = %s; want %s", b.Bytes(), want.Bytes())
}
}

View File

@ -297,6 +297,9 @@ func (n Number) Int64() (int64, error) {
}
func (n Number) MarshalJSON() ([]byte, error) {
if _, err := n.Float64(); err != nil {
return nil, err
}
return []byte(n), nil
}