Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
c20c0925
Commit
c20c0925
authored
Jan 03, 2012
by
Evan Shaw
Committed by
Andrew Gerrand
Jan 03, 2012
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
encoding/json: don't marshal special float values
R=golang-dev, adg CC=golang-dev
https://golang.org/cl/5500084
parent
38ff98b4
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
34 additions
and
1 deletion
+34
-1
src/pkg/encoding/json/encode.go
src/pkg/encoding/json/encode.go
+15
-1
src/pkg/encoding/json/encode_test.go
src/pkg/encoding/json/encode_test.go
+19
-0
No files found.
src/pkg/encoding/json/encode.go
View file @
c20c0925
...
...
@@ -12,6 +12,7 @@ package json
import
(
"bytes"
"encoding/base64"
"math"
"reflect"
"runtime"
"sort"
...
...
@@ -170,6 +171,15 @@ func (e *UnsupportedTypeError) Error() string {
return
"json: unsupported type: "
+
e
.
Type
.
String
()
}
type
UnsupportedValueError
struct
{
Value
reflect
.
Value
Str
string
}
func
(
e
*
UnsupportedValueError
)
Error
()
string
{
return
"json: unsupported value: "
+
e
.
Str
}
type
InvalidUTF8Error
struct
{
S
string
}
...
...
@@ -290,7 +300,11 @@ func (e *encodeState) reflectValueQuoted(v reflect.Value, quoted bool) {
e
.
Write
(
b
)
}
case
reflect
.
Float32
,
reflect
.
Float64
:
b
:=
strconv
.
AppendFloat
(
e
.
scratch
[
:
0
],
v
.
Float
(),
'g'
,
-
1
,
v
.
Type
()
.
Bits
())
f
:=
v
.
Float
()
if
math
.
IsInf
(
f
,
0
)
||
math
.
IsNaN
(
f
)
{
e
.
error
(
&
UnsupportedValueError
{
v
,
strconv
.
FormatFloat
(
f
,
'g'
,
-
1
,
v
.
Type
()
.
Bits
())})
}
b
:=
strconv
.
AppendFloat
(
e
.
scratch
[
:
0
],
f
,
'g'
,
-
1
,
v
.
Type
()
.
Bits
())
if
quoted
{
writeString
(
e
,
string
(
b
))
}
else
{
...
...
src/pkg/encoding/json/encode_test.go
View file @
c20c0925
...
...
@@ -6,6 +6,7 @@ package json
import
(
"bytes"
"math"
"reflect"
"testing"
)
...
...
@@ -107,3 +108,21 @@ func TestEncodeRenamedByteSlice(t *testing.T) {
t
.
Errorf
(
" got %s want %s"
,
result
,
expect
)
}
}
var
unsupportedValues
=
[]
interface
{}{
math
.
NaN
(),
math
.
Inf
(
-
1
),
math
.
Inf
(
1
),
}
func
TestUnsupportedValues
(
t
*
testing
.
T
)
{
for
_
,
v
:=
range
unsupportedValues
{
if
_
,
err
:=
Marshal
(
v
);
err
!=
nil
{
if
_
,
ok
:=
err
.
(
*
UnsupportedValueError
);
!
ok
{
t
.
Errorf
(
"for %v, got %T want UnsupportedValueError"
,
v
,
err
)
}
}
else
{
t
.
Errorf
(
"for %v, expected error"
,
v
)
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment