Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
J
json
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
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
json
Commits
170b70f0
Commit
170b70f0
authored
Apr 07, 2016
by
Niels
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #232 from nlohmann/issue228
make serialization locale-independent (fixes #228)
parents
9c233be5
a744c626
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
122 additions
and
54 deletions
+122
-54
src/json.hpp
src/json.hpp
+42
-27
src/json.hpp.re2c
src/json.hpp.re2c
+42
-27
test/unit.cpp
test/unit.cpp
+38
-0
No files found.
src/json.hpp
View file @
170b70f0
...
@@ -88,6 +88,19 @@ struct has_mapped_type
...
@@ -88,6 +88,19 @@ struct has_mapped_type
static
constexpr
bool
value
=
sizeof
(
test
<
T
>
(
0
))
==
1
;
static
constexpr
bool
value
=
sizeof
(
test
<
T
>
(
0
))
==
1
;
};
};
/*!
@brief helper class to create locales with decimal point
@sa https://github.com/nlohmann/json/issues/51#issuecomment-86869315
*/
class
DecimalSeparator
:
public
std
::
numpunct
<
char
>
{
protected:
char
do_decimal_point
()
const
{
return
'.'
;
}
};
}
}
/*!
/*!
...
@@ -6114,24 +6127,26 @@ class basic_json
...
@@ -6114,24 +6127,26 @@ class basic_json
case
value_t
:
:
number_float
:
case
value_t
:
:
number_float
:
{
{
// buffer size: precision (2^8-1 = 255) + other ('-.e-xxx' = 7) + null (1)
char
buf
[
263
];
int
len
;
// check if number was parsed from a string
// check if number was parsed from a string
if
(
m_type
.
bits
.
parsed
)
if
(
m_type
.
bits
.
parsed
)
{
{
// check if parsed number had an exponent given
// check if parsed number had an exponent given
if
(
m_type
.
bits
.
has_exp
)
if
(
m_type
.
bits
.
has_exp
)
{
{
// buffer size: precision (2^8-1 = 255) + other ('-.e-xxx' = 7) + null (1)
char
buf
[
263
];
int
len
;
// handle capitalization of the exponent
// handle capitalization of the exponent
if
(
m_type
.
bits
.
exp_cap
)
if
(
m_type
.
bits
.
exp_cap
)
{
{
len
=
snprintf
(
buf
,
sizeof
(
buf
),
"%.*E"
,
m_type
.
bits
.
precision
,
m_value
.
number_float
)
+
1
;
len
=
snprintf
(
buf
,
sizeof
(
buf
),
"%.*E"
,
m_type
.
bits
.
precision
,
m_value
.
number_float
)
+
1
;
}
}
else
else
{
{
len
=
snprintf
(
buf
,
sizeof
(
buf
),
"%.*e"
,
m_type
.
bits
.
precision
,
m_value
.
number_float
)
+
1
;
len
=
snprintf
(
buf
,
sizeof
(
buf
),
"%.*e"
,
m_type
.
bits
.
precision
,
m_value
.
number_float
)
+
1
;
}
}
// remove '+' sign from the exponent if necessary
// remove '+' sign from the exponent if necessary
...
@@ -6152,40 +6167,40 @@ class basic_json
...
@@ -6152,40 +6167,40 @@ class basic_json
}
}
}
}
}
}
o
<<
buf
;
}
}
else
else
{
{
// no exponent - output as a decimal
// no exponent - output as a decimal
snprintf
(
buf
,
sizeof
(
buf
),
"%.*f"
,
std
::
stringstream
ss
;
m_type
.
bits
.
precision
,
m_value
.
number_float
);
ss
.
imbue
(
std
::
locale
(
std
::
locale
(),
new
DecimalSeparator
));
// fix locale problems
ss
<<
std
::
setprecision
(
m_type
.
bits
.
precision
)
<<
std
::
fixed
<<
m_value
.
number_float
;
o
<<
ss
.
str
();
}
}
}
}
else
if
(
m_value
.
number_float
==
0
)
else
{
{
// special case for zero to get "0.0"/"-0.0"
if
(
m_value
.
number_float
==
0
)
if
(
std
::
signbit
(
m_value
.
number_float
))
{
{
o
<<
"-0.0"
;
// special case for zero to get "0.0"/"-0.0"
o
<<
(
std
::
signbit
(
m_value
.
number_float
)
?
"-0.0"
:
"0.0"
);
}
}
else
else
{
{
o
<<
"0.0"
;
// Otherwise 6, 15 or 16 digits of precision allows
// round-trip IEEE 754 string->float->string,
// string->double->string or string->long double->string;
// to be safe, we read this value from
// std::numeric_limits<number_float_t>::digits10
std
::
stringstream
ss
;
ss
.
imbue
(
std
::
locale
(
std
::
locale
(),
new
DecimalSeparator
));
// fix locale problems
ss
<<
std
::
setprecision
(
std
::
numeric_limits
<
double
>::
digits10
)
<<
m_value
.
number_float
;
o
<<
ss
.
str
();
}
}
return
;
}
}
else
{
// Otherwise 6, 15 or 16 digits of precision allows
// round-trip IEEE 754 string->float->string,
// string->double->string or string->long double->string;
// to be safe, we read this value from
// std::numeric_limits<number_float_t>::digits10
snprintf
(
buf
,
sizeof
(
buf
),
"%.*g"
,
std
::
numeric_limits
<
double
>::
digits10
,
m_value
.
number_float
);
}
o
<<
buf
;
return
;
return
;
}
}
...
...
src/json.hpp.re2c
View file @
170b70f0
...
@@ -88,6 +88,19 @@ struct has_mapped_type
...
@@ -88,6 +88,19 @@ struct has_mapped_type
static constexpr bool value = sizeof(test<T>(0)) == 1;
static constexpr bool value = sizeof(test<T>(0)) == 1;
};
};
/*!
@brief helper class to create locales with decimal point
@sa https://github.com/nlohmann/json/issues/51#issuecomment-86869315
*/
class DecimalSeparator : public std::numpunct<char>
{
protected:
char do_decimal_point() const
{
return '.';
}
};
}
}
/*!
/*!
...
@@ -6114,24 +6127,26 @@ class basic_json
...
@@ -6114,24 +6127,26 @@ class basic_json
case value_t::number_float:
case value_t::number_float:
{
{
// buffer size: precision (2^8-1 = 255) + other ('-.e-xxx' = 7) + null (1)
char buf[263];
int len;
// check if number was parsed from a string
// check if number was parsed from a string
if (m_type.bits.parsed)
if (m_type.bits.parsed)
{
{
// check if parsed number had an exponent given
// check if parsed number had an exponent given
if (m_type.bits.has_exp)
if (m_type.bits.has_exp)
{
{
// buffer size: precision (2^8-1 = 255) + other ('-.e-xxx' = 7) + null (1)
char buf[263];
int len;
// handle capitalization of the exponent
// handle capitalization of the exponent
if (m_type.bits.exp_cap)
if (m_type.bits.exp_cap)
{
{
len = snprintf(buf, sizeof(buf), "%.*E", m_type.bits.precision, m_value.number_float) + 1;
len = snprintf(buf, sizeof(buf), "%.*E",
m_type.bits.precision, m_value.number_float) + 1;
}
}
else
else
{
{
len = snprintf(buf, sizeof(buf), "%.*e", m_type.bits.precision, m_value.number_float) + 1;
len = snprintf(buf, sizeof(buf), "%.*e",
m_type.bits.precision, m_value.number_float) + 1;
}
}
// remove '+' sign from the exponent if necessary
// remove '+' sign from the exponent if necessary
...
@@ -6152,40 +6167,40 @@ class basic_json
...
@@ -6152,40 +6167,40 @@ class basic_json
}
}
}
}
}
}
o << buf;
}
}
else
else
{
{
// no exponent - output as a decimal
// no exponent - output as a decimal
snprintf(buf, sizeof(buf), "%.*f",
std::stringstream ss;
m_type.bits.precision, m_value.number_float);
ss.imbue(std::locale(std::locale(), new DecimalSeparator)); // fix locale problems
ss << std::setprecision(m_type.bits.precision)
<< std::fixed << m_value.number_float;
o << ss.str();
}
}
}
}
else
if (m_value.number_float == 0)
else
{
{
// special case for zero to get "0.0"/"-0.0"
if (m_value.number_float == 0)
if (std::signbit(m_value.number_float))
{
{
o << "-0.0";
// special case for zero to get "0.0"/"-0.0"
o << (std::signbit(m_value.number_float) ? "-0.0" : "0.0");
}
}
else
else
{
{
o << "0.0";
// Otherwise 6, 15 or 16 digits of precision allows
// round-trip IEEE 754 string->float->string,
// string->double->string or string->long double->string;
// to be safe, we read this value from
// std::numeric_limits<number_float_t>::digits10
std::stringstream ss;
ss.imbue(std::locale(std::locale(), new DecimalSeparator)); // fix locale problems
ss << std::setprecision(std::numeric_limits<double>::digits10)
<< m_value.number_float;
o << ss.str();
}
}
return;
}
}
else
{
// Otherwise 6, 15 or 16 digits of precision allows
// round-trip IEEE 754 string->float->string,
// string->double->string or string->long double->string;
// to be safe, we read this value from
// std::numeric_limits<number_float_t>::digits10
snprintf(buf, sizeof(buf), "%.*g",
std::numeric_limits<double>::digits10,
m_value.number_float);
}
o << buf;
return;
return;
}
}
...
...
test/unit.cpp
View file @
170b70f0
...
@@ -12369,5 +12369,43 @@ TEST_CASE("regression tests")
...
@@ -12369,5 +12369,43 @@ TEST_CASE("regression tests")
j_long_double
=
1.23e45L
;
j_long_double
=
1.23e45L
;
CHECK
(
j_long_double
.
get
<
long
double
>
()
==
1.23e45L
);
CHECK
(
j_long_double
.
get
<
long
double
>
()
==
1.23e45L
);
}
}
SECTION
(
"issue #228 - double values are serialized with commas as decimal points"
)
{
json
j1a
=
23.42
;
json
j1b
=
json
::
parse
(
"23.42"
);
json
j2a
=
2342e-2
;
//issue #230
//json j2b = json::parse("2342e-2");
json
j3a
=
10E3
;
json
j3b
=
json
::
parse
(
"10E3"
);
json
j3c
=
json
::
parse
(
"10e3"
);
// class to create a locale that would use a comma for decimals
class
CommaDecimalSeparator
:
public
std
::
numpunct
<
char
>
{
protected:
char
do_decimal_point
()
const
{
return
','
;
}
};
// change locale to mess with decimal points
std
::
locale
::
global
(
std
::
locale
(
std
::
locale
(),
new
CommaDecimalSeparator
));
CHECK
(
j1a
.
dump
()
==
"23.42"
);
CHECK
(
j1b
.
dump
()
==
"23.42"
);
CHECK
(
j2a
.
dump
()
==
"23.42"
);
//issue #230
//CHECK(j2b.dump() == "23.42");
CHECK
(
j3a
.
dump
()
==
"10000"
);
CHECK
(
j3b
.
dump
()
==
"1E04"
);
CHECK
(
j3c
.
dump
()
==
"1e04"
);
}
}
}
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