Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
C
capnproto
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
capnproto
Commits
e93c9aca
Commit
e93c9aca
authored
Nov 20, 2015
by
Kenton Varda
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #262 from kamalmarhubi/json-encode-nan-inf
JSON: encode NaNs and infinite floats as `null`
parents
0a35b416
359e65e4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
19 additions
and
3 deletions
+19
-3
json-test.c++
c++/src/capnp/compat/json-test.c++
+2
-2
json.c++
c++/src/capnp/compat/json.c++
+12
-1
json.h
c++/src/capnp/compat/json.h
+2
-0
common.h
c++/src/kj/common.h
+3
-0
No files found.
c++/src/capnp/compat/json-test.c++
View file @
e93c9aca
...
@@ -127,8 +127,8 @@ const char ALL_TYPES_JSON[] =
...
@@ -127,8 +127,8 @@ const char ALL_TYPES_JSON[] =
"
\"
uInt16List
\"
: [33333, 44444],
\n
"
"
\"
uInt16List
\"
: [33333, 44444],
\n
"
"
\"
uInt32List
\"
: [3333333333],
\n
"
"
\"
uInt32List
\"
: [3333333333],
\n
"
"
\"
uInt64List
\"
: [
\"
11111111111111111111
\"
],
\n
"
"
\"
uInt64List
\"
: [
\"
11111111111111111111
\"
],
\n
"
"
\"
float32List
\"
: [5555.5,
inf, -inf, nan
],
\n
"
"
\"
float32List
\"
: [5555.5,
null, null, null
],
\n
"
"
\"
float64List
\"
: [7777.75,
inf, -inf, nan
],
\n
"
"
\"
float64List
\"
: [7777.75,
null, null, null
],
\n
"
"
\"
textList
\"
: [
\"
plugh
\"
,
\"
xyzzy
\"
,
\"
thud
\"
],
\n
"
"
\"
textList
\"
: [
\"
plugh
\"
,
\"
xyzzy
\"
,
\"
thud
\"
],
\n
"
"
\"
dataList
\"
: [[111, 111, 112, 115], [101, 120, 104, 97, 117, 115, 116, 101, 100], [114, 102, 99, 51, 48, 57, 50]],
\n
"
"
\"
dataList
\"
: [[111, 111, 112, 115], [101, 120, 104, 97, 117, 115, 116, 101, 100], [114, 102, 99, 51, 48, 57, 50]],
\n
"
"
\"
structList
\"
: [
\n
"
"
\"
structList
\"
: [
\n
"
...
...
c++/src/capnp/compat/json.c++
View file @
e93c9aca
...
@@ -237,9 +237,20 @@ void JsonCodec::encode(DynamicValue::Reader input, Type type, JsonValue::Builder
...
@@ -237,9 +237,20 @@ void JsonCodec::encode(DynamicValue::Reader input, Type type, JsonValue::Builder
case
schema
:
:
Type
::
UINT8
:
case
schema
:
:
Type
::
UINT8
:
case
schema
:
:
Type
::
UINT16
:
case
schema
:
:
Type
::
UINT16
:
case
schema
:
:
Type
::
UINT32
:
case
schema
:
:
Type
::
UINT32
:
output
.
setNumber
(
input
.
as
<
double
>
());
break
;
case
schema
:
:
Type
::
FLOAT32
:
case
schema
:
:
Type
::
FLOAT32
:
case
schema
:
:
Type
::
FLOAT64
:
case
schema
:
:
Type
::
FLOAT64
:
output
.
setNumber
(
input
.
as
<
double
>
());
{
double
value
=
input
.
as
<
double
>
();
if
(
kj
::
inf
()
==
value
||
-
kj
::
inf
()
==
value
||
kj
::
isNaN
(
value
))
{
// These values are not allowed in the JSON spec. Setting the field as null matches the
// behavior of JSON.stringify in Firefox and Chrome.
output
.
setNull
();
}
else
{
output
.
setNumber
(
value
);
}
}
break
;
break
;
case
schema
:
:
Type
::
INT64
:
case
schema
:
:
Type
::
INT64
:
output
.
setString
(
kj
::
str
(
input
.
as
<
int64_t
>
()));
output
.
setString
(
kj
::
str
(
input
.
as
<
int64_t
>
()));
...
...
c++/src/capnp/compat/json.h
View file @
e93c9aca
...
@@ -49,6 +49,8 @@ class JsonCodec {
...
@@ -49,6 +49,8 @@ class JsonCodec {
// fields are only encoded if they are non-null.
// fields are only encoded if they are non-null.
// - 64-bit integers are encoded as strings, since JSON "numbers" are double-precision floating
// - 64-bit integers are encoded as strings, since JSON "numbers" are double-precision floating
// points which cannot store a 64-bit integer without losing data.
// points which cannot store a 64-bit integer without losing data.
// - NaNs and infinite floating point numbers are not allowed by the JSON spec, and so are encoded
// as null. This matches the behavior of `JSON.stringify` in at least Firefox and Chrome.
// - Data is encoded as an array of numbers in the range [0,255]. You probably want to register
// - Data is encoded as an array of numbers in the range [0,255]. You probably want to register
// a handler that does something better, like maybe base64 encoding, but there are a zillion
// a handler that does something better, like maybe base64 encoding, but there are a zillion
// different ways people do this.
// different ways people do this.
...
...
c++/src/kj/common.h
View file @
e93c9aca
...
@@ -594,6 +594,9 @@ float nan();
...
@@ -594,6 +594,9 @@ float nan();
#error "Not sure how to support your compiler."
#error "Not sure how to support your compiler."
#endif
#endif
inline
constexpr
bool
isNaN
(
float
f
)
{
return
f
!=
f
;
}
inline
constexpr
bool
isNaN
(
double
f
)
{
return
f
!=
f
;
}
// =======================================================================================
// =======================================================================================
// Useful fake containers
// Useful fake containers
...
...
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