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
7cd9067e
Commit
7cd9067e
authored
Feb 08, 2015
by
Niels
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
more test cases
parent
43ec6477
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
264 additions
and
14 deletions
+264
-14
src/json.hpp
src/json.hpp
+7
-7
src/json.hpp.re2c
src/json.hpp.re2c
+7
-7
test/unit.cpp
test/unit.cpp
+250
-0
No files found.
src/json.hpp
View file @
7cd9067e
...
...
@@ -44,7 +44,7 @@ namespace nlohmann
(@c bool by default)
@tparam NumberIntegerType type for JSON integer numbers
(@c int64_t by default)
@tparam NumberFloatType type for JSON floating
point numbers
@tparam NumberFloatType type for JSON floating
-
point numbers
(@c double by default)
*/
template
<
...
...
@@ -104,7 +104,7 @@ class basic_json
using
boolean_t
=
BooleanType
;
/// a type for a number (integer)
using
number_integer_t
=
NumberIntegerType
;
/// a type for a number (floating
point)
/// a type for a number (floating
-
point)
using
number_float_t
=
NumberFloatType
;
/// a type for list initialization
using
list_init_t
=
std
::
initializer_list
<
basic_json
>
;
...
...
@@ -127,7 +127,7 @@ class basic_json
boolean_t
boolean
;
/// number (integer)
number_integer_t
number_integer
;
/// number (floating
point)
/// number (floating
-
point)
number_float_t
number_float
;
/// default constructor (for null values)
...
...
@@ -142,7 +142,7 @@ class basic_json
json_value
(
boolean_t
v
)
:
boolean
(
v
)
{}
/// constructor for numbers (integer)
json_value
(
number_integer_t
v
)
:
number_integer
(
v
)
{}
/// constructor for numbers (floating
point)
/// constructor for numbers (floating
-
point)
json_value
(
number_float_t
v
)
:
number_float
(
v
)
{}
};
...
...
@@ -166,7 +166,7 @@ class basic_json
boolean
,
/// number value (integer)
number_integer
,
/// number value (floating
point)
/// number value (floating
-
point)
number_float
};
...
...
@@ -306,12 +306,12 @@ class basic_json
:
m_type
(
value_t
::
number_integer
),
m_value
(
number_integer_t
(
value
))
{}
/// create a floating
point number (explicit)
/// create a floating
-
point number (explicit)
inline
basic_json
(
const
number_float_t
&
value
)
:
m_type
(
value_t
::
number_float
),
m_value
(
value
)
{}
/// create a floating
point number (implicit)
/// create a floating
-
point number (implicit)
template
<
typename
T
,
typename
=
typename
std
::
enable_if
<
std
::
is_constructible
<
number_float_t
,
T
>
::
value
and
...
...
src/json.hpp.re2c
View file @
7cd9067e
...
...
@@ -44,7 +44,7 @@ namespace nlohmann
(@c bool by default)
@tparam NumberIntegerType type for JSON integer numbers
(@c int64_t by default)
@tparam NumberFloatType type for JSON floating
point numbers
@tparam NumberFloatType type for JSON floating
-
point numbers
(@c double by default)
*/
template <
...
...
@@ -104,7 +104,7 @@ class basic_json
using boolean_t = BooleanType;
/// a type for a number (integer)
using number_integer_t = NumberIntegerType;
/// a type for a number (floating
point)
/// a type for a number (floating
-
point)
using number_float_t = NumberFloatType;
/// a type for list initialization
using list_init_t = std::initializer_list<basic_json>;
...
...
@@ -127,7 +127,7 @@ class basic_json
boolean_t boolean;
/// number (integer)
number_integer_t number_integer;
/// number (floating
point)
/// number (floating
-
point)
number_float_t number_float;
/// default constructor (for null values)
...
...
@@ -142,7 +142,7 @@ class basic_json
json_value(boolean_t v) : boolean(v) {}
/// constructor for numbers (integer)
json_value(number_integer_t v) : number_integer(v) {}
/// constructor for numbers (floating
point)
/// constructor for numbers (floating
-
point)
json_value(number_float_t v) : number_float(v) {}
};
...
...
@@ -166,7 +166,7 @@ class basic_json
boolean,
/// number value (integer)
number_integer,
/// number value (floating
point)
/// number value (floating
-
point)
number_float
};
...
...
@@ -306,12 +306,12 @@ class basic_json
: m_type(value_t::number_integer), m_value(number_integer_t(value))
{}
/// create a floating
point number (explicit)
/// create a floating
-
point number (explicit)
inline basic_json(const number_float_t& value)
: m_type(value_t::number_float), m_value(value)
{}
/// create a floating
point number (implicit)
/// create a floating
-
point number (implicit)
template<typename T, typename = typename
std::enable_if<
std::is_constructible<number_float_t, T>::value and
...
...
test/unit.cpp
View file @
7cd9067e
...
...
@@ -643,4 +643,254 @@ TEST_CASE("Constructors")
CHECK
(
j
==
j_reference
);
}
}
SECTION
(
"create a floating-point number (explicit)"
)
{
SECTION
(
"uninitialized value"
)
{
json
::
number_float_t
n
{};
json
j
(
n
);
CHECK
(
j
.
type
()
==
json
::
value_t
::
number_float
);
}
SECTION
(
"initialized value"
)
{
json
::
number_float_t
n
(
42.23
);
json
j
(
n
);
CHECK
(
j
.
type
()
==
json
::
value_t
::
number_float
);
}
}
SECTION
(
"create a floating-point number (implicit)"
)
{
// reference object
json
::
number_float_t
n_reference
=
42.23
;
json
j_reference
(
n_reference
);
SECTION
(
"float"
)
{
float
n
=
42.23
;
json
j
(
n
);
CHECK
(
j
.
type
()
==
json
::
value_t
::
number_float
);
CHECK
(
j
.
m_value
.
number_float
==
Approx
(
j_reference
.
m_value
.
number_float
));
}
SECTION
(
"double"
)
{
double
n
=
42.23
;
json
j
(
n
);
CHECK
(
j
.
type
()
==
json
::
value_t
::
number_float
);
CHECK
(
j
.
m_value
.
number_float
==
Approx
(
j_reference
.
m_value
.
number_float
));
}
SECTION
(
"long double"
)
{
long
double
n
=
42.23
;
json
j
(
n
);
CHECK
(
j
.
type
()
==
json
::
value_t
::
number_float
);
CHECK
(
j
.
m_value
.
number_float
==
Approx
(
j_reference
.
m_value
.
number_float
));
}
SECTION
(
"floating-point literal without suffix"
)
{
json
j
(
42.23
);
CHECK
(
j
.
type
()
==
json
::
value_t
::
number_float
);
CHECK
(
j
.
m_value
.
number_float
==
Approx
(
j_reference
.
m_value
.
number_float
));
}
SECTION
(
"integer literal with f suffix"
)
{
json
j
(
42.23
f
);
CHECK
(
j
.
type
()
==
json
::
value_t
::
number_float
);
CHECK
(
j
.
m_value
.
number_float
==
Approx
(
j_reference
.
m_value
.
number_float
));
}
SECTION
(
"integer literal with l suffix"
)
{
json
j
(
42.23
l
);
CHECK
(
j
.
type
()
==
json
::
value_t
::
number_float
);
CHECK
(
j
.
m_value
.
number_float
==
Approx
(
j_reference
.
m_value
.
number_float
));
}
}
SECTION
(
"create a container (array or object) from an initializer list"
)
{
SECTION
(
"empty initializer list"
)
{
SECTION
(
"explicit"
)
{
std
::
initializer_list
<
json
>
l
;
json
j
(
l
);
CHECK
(
j
.
type
()
==
json
::
value_t
::
object
);
}
SECTION
(
"implicit"
)
{
json
j
{};
CHECK
(
j
.
type
()
==
json
::
value_t
::
null
);
}
}
SECTION
(
"one element"
)
{
SECTION
(
"array"
)
{
SECTION
(
"explicit"
)
{
std
::
initializer_list
<
json
>
l
=
{
json
(
json
::
array_t
())};
json
j
(
l
);
CHECK
(
j
.
type
()
==
json
::
value_t
::
array
);
}
SECTION
(
"implicit"
)
{
json
j
{
json
::
array_t
()};
CHECK
(
j
.
type
()
==
json
::
value_t
::
array
);
}
}
SECTION
(
"object"
)
{
SECTION
(
"explicit"
)
{
std
::
initializer_list
<
json
>
l
=
{
json
(
json
::
object_t
())};
json
j
(
l
);
CHECK
(
j
.
type
()
==
json
::
value_t
::
array
);
}
SECTION
(
"implicit"
)
{
json
j
{
json
::
object_t
()};
CHECK
(
j
.
type
()
==
json
::
value_t
::
array
);
}
}
SECTION
(
"string"
)
{
SECTION
(
"explicit"
)
{
std
::
initializer_list
<
json
>
l
=
{
json
(
"Hello world"
)};
json
j
(
l
);
CHECK
(
j
.
type
()
==
json
::
value_t
::
array
);
}
SECTION
(
"implicit"
)
{
json
j
{
"Hello world"
};
CHECK
(
j
.
type
()
==
json
::
value_t
::
array
);
}
}
SECTION
(
"boolean"
)
{
SECTION
(
"explicit"
)
{
std
::
initializer_list
<
json
>
l
=
{
json
(
true
)};
json
j
(
l
);
CHECK
(
j
.
type
()
==
json
::
value_t
::
array
);
}
SECTION
(
"implicit"
)
{
json
j
{
true
};
CHECK
(
j
.
type
()
==
json
::
value_t
::
array
);
}
}
SECTION
(
"number (integer)"
)
{
SECTION
(
"explicit"
)
{
std
::
initializer_list
<
json
>
l
=
{
json
(
1
)};
json
j
(
l
);
CHECK
(
j
.
type
()
==
json
::
value_t
::
array
);
}
SECTION
(
"implicit"
)
{
json
j
{
1
};
CHECK
(
j
.
type
()
==
json
::
value_t
::
array
);
}
}
SECTION
(
"number (floating-point)"
)
{
SECTION
(
"explicit"
)
{
std
::
initializer_list
<
json
>
l
=
{
json
(
42.23
)};
json
j
(
l
);
CHECK
(
j
.
type
()
==
json
::
value_t
::
array
);
}
SECTION
(
"implicit"
)
{
json
j
{
42.23
};
CHECK
(
j
.
type
()
==
json
::
value_t
::
array
);
}
}
}
SECTION
(
"more elements"
)
{
SECTION
(
"explicit"
)
{
std
::
initializer_list
<
json
>
l
=
{
1
,
42.23
,
true
,
nullptr
,
json
::
object_t
(),
json
::
array_t
()};
json
j
(
l
);
CHECK
(
j
.
type
()
==
json
::
value_t
::
array
);
}
SECTION
(
"implicit"
)
{
json
j
{
1
,
42.23
,
true
,
nullptr
,
json
::
object_t
(),
json
::
array_t
()};
CHECK
(
j
.
type
()
==
json
::
value_t
::
array
);
}
}
SECTION
(
"implicit type deduction"
)
{
SECTION
(
"object"
)
{
json
j
{
{
"one"
,
1
},
{
"two"
,
2.2
},
{
"three"
,
false
}
};
CHECK
(
j
.
type
()
==
json
::
value_t
::
object
);
}
SECTION
(
"array"
)
{
json
j
{
{
"one"
,
1
},
{
"two"
,
2.2
},
{
"three"
,
false
},
13
};
CHECK
(
j
.
type
()
==
json
::
value_t
::
array
);
}
}
SECTION
(
"explicit type deduction"
)
{
SECTION
(
"empty object"
)
{
json
j
=
json
::
object
();
CHECK
(
j
.
type
()
==
json
::
value_t
::
object
);
}
SECTION
(
"object"
)
{
json
j
=
json
::
object
({
{
"one"
,
1
},
{
"two"
,
2.2
},
{
"three"
,
false
}
});
CHECK
(
j
.
type
()
==
json
::
value_t
::
object
);
}
SECTION
(
"object with error"
)
{
CHECK_THROWS_AS
(
json
::
object
({
{
"one"
,
1
},
{
"two"
,
2.2
},
{
"three"
,
false
},
13
}),
std
::
logic_error
);
}
SECTION
(
"empty array"
)
{
json
j
=
json
::
array
();
CHECK
(
j
.
type
()
==
json
::
value_t
::
array
);
}
SECTION
(
"array"
)
{
json
j
=
json
::
array
({
{
"one"
,
1
},
{
"two"
,
2.2
},
{
"three"
,
false
}
});
CHECK
(
j
.
type
()
==
json
::
value_t
::
array
);
}
}
}
}
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