Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
json
Project
Overview
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
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Chen Yisong
json
Commits
51edad39
Commit
51edad39
authored
Jun 15, 2015
by
Niels
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixed #89 without breaking #71
parent
8e9a7119
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
77 additions
and
14 deletions
+77
-14
json.hpp
src/json.hpp
+34
-7
json.hpp.re2c
src/json.hpp.re2c
+34
-7
unit.cpp
test/unit.cpp
+9
-0
No files found.
src/json.hpp
View file @
51edad39
...
@@ -425,14 +425,40 @@ class basic_json
...
@@ -425,14 +425,40 @@ class basic_json
:
m_type
(
value_t
::
boolean
),
m_value
(
value
)
:
m_type
(
value_t
::
boolean
),
m_value
(
value
)
{}
{}
/// create an integer number (explicit)
/*!
basic_json
(
const
number_integer_t
&
value
)
@brief create an integer number (explicit)
@tparam T helper type to compare number_integer_t and int
@param value an integer to create a JSON number from
This constructor takes care about explicitly passed values of type
number_integer_t. However, this constructor would have the same signature
as the existing one for const int values, so we need to switch this one off
in case number_integer_t is the same as int.
*/
template
<
typename
T
,
typename
std
::
enable_if
<
not
(
std
::
is_same
<
T
,
int
>::
value
)
and
std
::
is_same
<
T
,
number_integer_t
>::
value
,
int
>::
type
=
0
>
basic_json
(
const
number_integer_t
value
)
:
m_type
(
value_t
::
number_integer
),
m_value
(
value
)
:
m_type
(
value_t
::
number_integer
),
m_value
(
value
)
{}
{}
/// create an int number to support enum type (implicit)
/*!
basic_json
(
const
int
int_enum
)
@brief create an int number to support enum type (implicit)
:
basic_json
(
static_cast
<
number_integer_t
>
(
int_enum
))
@param value an integer to create a JSON number from
This constructor allows to pass enums directly to a constructor. As C++ has
no way of specifying the type of an anonymous enum explicitly, we can only
rely on the fact that such values implicitly convert to int. As int may
already be the same type of number_integer_t, we may need to switch off
that constructor, which is done above.
*/
basic_json
(
const
int
value
)
:
m_type
(
value_t
::
number_integer
),
m_value
(
static_cast
<
number_integer_t
>
(
value
))
{}
{}
/// create an integer number (implicit)
/// create an integer number (implicit)
...
@@ -442,11 +468,12 @@ class basic_json
...
@@ -442,11 +468,12 @@ class basic_json
std
::
numeric_limits
<
T
>::
is_integer
,
T
>::
type
std
::
numeric_limits
<
T
>::
is_integer
,
T
>::
type
=
0
>
=
0
>
basic_json
(
const
T
value
)
noexcept
basic_json
(
const
T
value
)
noexcept
:
basic_json
(
number_integer_t
(
value
))
:
m_type
(
value_t
::
number_integer
),
m_value
(
static_cast
<
number_integer_t
>
(
value
))
{}
{}
/// create a floating-point number (explicit)
/// create a floating-point number (explicit)
basic_json
(
const
number_float_t
&
value
)
basic_json
(
const
number_float_t
value
)
:
m_type
(
value_t
::
number_float
),
m_value
(
value
)
:
m_type
(
value_t
::
number_float
),
m_value
(
value
)
{
{
// replace infinity and NAN by null
// replace infinity and NAN by null
...
...
src/json.hpp.re2c
View file @
51edad39
...
@@ -425,14 +425,40 @@ class basic_json
...
@@ -425,14 +425,40 @@ class basic_json
: m_type(value_t::boolean), m_value(value)
: m_type(value_t::boolean), m_value(value)
{}
{}
/// create an integer number (explicit)
/*!
basic_json(const number_integer_t& value)
@brief create an integer number (explicit)
@tparam T helper type to compare number_integer_t and int
@param value an integer to create a JSON number from
This constructor takes care about explicitly passed values of type
number_integer_t. However, this constructor would have the same signature
as the existing one for const int values, so we need to switch this one off
in case number_integer_t is the same as int.
*/
template<typename T,
typename std::enable_if<
not (std::is_same<T, int>::value)
and std::is_same<T, number_integer_t>::value
, int>::type = 0>
basic_json(const number_integer_t value)
: m_type(value_t::number_integer), m_value(value)
: m_type(value_t::number_integer), m_value(value)
{}
{}
/// create an int number to support enum type (implicit)
/*!
basic_json(const int int_enum)
@brief create an int number to support enum type (implicit)
: basic_json(static_cast<number_integer_t>(int_enum))
@param value an integer to create a JSON number from
This constructor allows to pass enums directly to a constructor. As C++ has
no way of specifying the type of an anonymous enum explicitly, we can only
rely on the fact that such values implicitly convert to int. As int may
already be the same type of number_integer_t, we may need to switch off
that constructor, which is done above.
*/
basic_json(const int value)
: m_type(value_t::number_integer),
m_value(static_cast<number_integer_t>(value))
{}
{}
/// create an integer number (implicit)
/// create an integer number (implicit)
...
@@ -442,11 +468,12 @@ class basic_json
...
@@ -442,11 +468,12 @@ class basic_json
std::numeric_limits<T>::is_integer, T>::type
std::numeric_limits<T>::is_integer, T>::type
= 0>
= 0>
basic_json(const T value) noexcept
basic_json(const T value) noexcept
: basic_json(number_integer_t(value))
: m_type(value_t::number_integer),
m_value(static_cast<number_integer_t>(value))
{}
{}
/// create a floating-point number (explicit)
/// create a floating-point number (explicit)
basic_json(const number_float_t
&
value)
basic_json(const number_float_t value)
: m_type(value_t::number_float), m_value(value)
: m_type(value_t::number_float), m_value(value)
{
{
// replace infinity and NAN by null
// replace infinity and NAN by null
...
...
test/unit.cpp
View file @
51edad39
...
@@ -8892,4 +8892,13 @@ TEST_CASE("regression tests")
...
@@ -8892,4 +8892,13 @@ TEST_CASE("regression tests")
float
foo
=
j
[
"Foo"
];
float
foo
=
j
[
"Foo"
];
CHECK
(
foo
==
Approx
(
42.42
));
CHECK
(
foo
==
Approx
(
42.42
));
}
}
SECTION
(
"issue #89 - nonstandard integer type"
)
{
// create JSON class with nonstandard integer number type
nlohmann
::
basic_json
<
std
::
map
,
std
::
vector
,
std
::
string
,
bool
,
int32_t
,
float
>
j
;
j
[
"int_1"
]
=
1
;
// we need to cast to int to compile with Catch - the value is int32_t
CHECK
(
static_cast
<
int
>
(
j
[
"int_1"
])
==
1
);
}
}
}
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