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
441a6f26
Commit
441a6f26
authored
Feb 08, 2015
by
Niels
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
more test cases
parent
8ed9eaa6
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
62 additions
and
16 deletions
+62
-16
src/json.hpp
src/json.hpp
+8
-6
src/json.hpp.re2c
src/json.hpp.re2c
+8
-6
test/unit.cpp
test/unit.cpp
+46
-4
No files found.
src/json.hpp
View file @
441a6f26
...
@@ -497,6 +497,8 @@ class basic_json
...
@@ -497,6 +497,8 @@ class basic_json
///////////////////////
///////////////////////
/*!
/*!
@brief serialization
Serialization function for JSON objects. The function tries to mimick Python's
Serialization function for JSON objects. The function tries to mimick Python's
@p json.dumps() function, and currently supports its @p indent parameter.
@p json.dumps() function, and currently supports its @p indent parameter.
...
@@ -536,7 +538,7 @@ class basic_json
...
@@ -536,7 +538,7 @@ class basic_json
// value conversion //
// value conversion //
//////////////////////
//////////////////////
/// get an object
/// get an object
(explicit)
template
<
class
T
,
typename
template
<
class
T
,
typename
std
::
enable_if
<
std
::
enable_if
<
std
::
is_constructible
<
string_t
,
typename
T
::
key_type
>
::
value
and
std
::
is_constructible
<
string_t
,
typename
T
::
key_type
>
::
value
and
...
@@ -553,7 +555,7 @@ class basic_json
...
@@ -553,7 +555,7 @@ class basic_json
}
}
}
}
/// get an array
/// get an array
(explicit)
template
<
class
T
,
typename
template
<
class
T
,
typename
std
::
enable_if
<
std
::
enable_if
<
not
std
::
is_same
<
T
,
string_t
>
::
value
and
not
std
::
is_same
<
T
,
string_t
>
::
value
and
...
@@ -570,7 +572,7 @@ class basic_json
...
@@ -570,7 +572,7 @@ class basic_json
}
}
}
}
/// get a string
/// get a string
(explicit)
template
<
typename
T
,
typename
template
<
typename
T
,
typename
std
::
enable_if
<
std
::
enable_if
<
std
::
is_constructible
<
T
,
string_t
>
::
value
,
int
>::
type
std
::
is_constructible
<
T
,
string_t
>
::
value
,
int
>::
type
...
@@ -586,7 +588,7 @@ class basic_json
...
@@ -586,7 +588,7 @@ class basic_json
}
}
}
}
/// get a boolean
/// get a boolean
(explicit)
template
<
typename
T
,
typename
template
<
typename
T
,
typename
std
::
enable_if
<
std
::
enable_if
<
std
::
is_same
<
boolean_t
,
T
>
::
value
,
int
>::
type
std
::
is_same
<
boolean_t
,
T
>
::
value
,
int
>::
type
...
@@ -602,7 +604,7 @@ class basic_json
...
@@ -602,7 +604,7 @@ class basic_json
}
}
}
}
///
explicitly get a number
///
get a number (explicit)
template
<
typename
T
,
typename
template
<
typename
T
,
typename
std
::
enable_if
<
std
::
enable_if
<
not
std
::
is_same
<
boolean_t
,
T
>
::
value
and
not
std
::
is_same
<
boolean_t
,
T
>
::
value
and
...
@@ -621,7 +623,7 @@ class basic_json
...
@@ -621,7 +623,7 @@ class basic_json
}
}
}
}
///
explicitly get a value
///
get a value (implicit)
template
<
typename
T
>
template
<
typename
T
>
inline
operator
T
()
const
inline
operator
T
()
const
{
{
...
...
src/json.hpp.re2c
View file @
441a6f26
...
@@ -497,6 +497,8 @@ class basic_json
...
@@ -497,6 +497,8 @@ class basic_json
///////////////////////
///////////////////////
/*!
/*!
@brief serialization
Serialization function for JSON objects. The function tries to mimick Python's
Serialization function for JSON objects. The function tries to mimick Python's
@p json.dumps() function, and currently supports its @p indent parameter.
@p json.dumps() function, and currently supports its @p indent parameter.
...
@@ -536,7 +538,7 @@ class basic_json
...
@@ -536,7 +538,7 @@ class basic_json
// value conversion //
// value conversion //
//////////////////////
//////////////////////
/// get an object
/// get an object
(explicit)
template <class T, typename
template <class T, typename
std::enable_if<
std::enable_if<
std::is_constructible<string_t, typename T::key_type>::value and
std::is_constructible<string_t, typename T::key_type>::value and
...
@@ -553,7 +555,7 @@ class basic_json
...
@@ -553,7 +555,7 @@ class basic_json
}
}
}
}
/// get an array
/// get an array
(explicit)
template <class T, typename
template <class T, typename
std::enable_if<
std::enable_if<
not std::is_same<T, string_t>::value and
not std::is_same<T, string_t>::value and
...
@@ -570,7 +572,7 @@ class basic_json
...
@@ -570,7 +572,7 @@ class basic_json
}
}
}
}
/// get a string
/// get a string
(explicit)
template <typename T, typename
template <typename T, typename
std::enable_if<
std::enable_if<
std::is_constructible<T, string_t>::value, int>::type
std::is_constructible<T, string_t>::value, int>::type
...
@@ -586,7 +588,7 @@ class basic_json
...
@@ -586,7 +588,7 @@ class basic_json
}
}
}
}
/// get a boolean
/// get a boolean
(explicit)
template <typename T, typename
template <typename T, typename
std::enable_if<
std::enable_if<
std::is_same<boolean_t, T>::value, int>::type
std::is_same<boolean_t, T>::value, int>::type
...
@@ -602,7 +604,7 @@ class basic_json
...
@@ -602,7 +604,7 @@ class basic_json
}
}
}
}
///
explicitly get a number
///
get a number (explicit)
template<typename T, typename
template<typename T, typename
std::enable_if<
std::enable_if<
not std::is_same<boolean_t, T>::value and
not std::is_same<boolean_t, T>::value and
...
@@ -621,7 +623,7 @@ class basic_json
...
@@ -621,7 +623,7 @@ class basic_json
}
}
}
}
///
explicitly get a value
///
get a value (implicit)
template<typename T>
template<typename T>
inline operator T() const
inline operator T() const
{
{
...
...
test/unit.cpp
View file @
441a6f26
...
@@ -1034,23 +1034,26 @@ TEST_CASE("other constructors and destructor")
...
@@ -1034,23 +1034,26 @@ TEST_CASE("other constructors and destructor")
TEST_CASE
(
"object inspection"
)
TEST_CASE
(
"object inspection"
)
{
{
SECTION
(
"
dump
"
)
SECTION
(
"
serialization
"
)
{
{
json
j
{{
"object"
,
json
::
object
()},
{
"array"
,
{
1
,
2
,
3
,
4
}},
{
"number"
,
42
},
{
"boolean"
,
false
},
{
"null"
,
nullptr
},
{
"string"
,
"Hello world"
}
};
json
j
{{
"object"
,
json
::
object
()},
{
"array"
,
{
1
,
2
,
3
,
4
}},
{
"number"
,
42
},
{
"boolean"
,
false
},
{
"null"
,
nullptr
},
{
"string"
,
"Hello world"
}
};
SECTION
(
"no indent"
)
SECTION
(
"no indent"
)
{
{
CHECK
(
j
.
dump
()
==
"{
\"
array
\"
:[1,2,3,4],
\"
boolean
\"
:false,
\"
null
\"
:null,
\"
number
\"
:42,
\"
object
\"
:{},
\"
string
\"
:
\"
Hello world
\"
}"
);
CHECK
(
j
.
dump
()
==
"{
\"
array
\"
:[1,2,3,4],
\"
boolean
\"
:false,
\"
null
\"
:null,
\"
number
\"
:42,
\"
object
\"
:{},
\"
string
\"
:
\"
Hello world
\"
}"
);
}
}
SECTION
(
"indent=0"
)
SECTION
(
"indent=0"
)
{
{
CHECK
(
j
.
dump
(
0
)
==
"{
\n\"
array
\"
: [
\n
1,
\n
2,
\n
3,
\n
4
\n
],
\n\"
boolean
\"
: false,
\n\"
null
\"
: null,
\n\"
number
\"
: 42,
\n\"
object
\"
: {},
\n\"
string
\"
:
\"
Hello world
\"\n
}"
);
CHECK
(
j
.
dump
(
0
)
==
"{
\n\"
array
\"
: [
\n
1,
\n
2,
\n
3,
\n
4
\n
],
\n\"
boolean
\"
: false,
\n\"
null
\"
: null,
\n\"
number
\"
: 42,
\n\"
object
\"
: {},
\n\"
string
\"
:
\"
Hello world
\"\n
}"
);
}
}
SECTION
(
"indent=4"
)
SECTION
(
"indent=4"
)
{
{
CHECK
(
j
.
dump
(
4
)
==
"{
\n
\"
array
\"
: [
\n
1,
\n
2,
\n
3,
\n
4
\n
],
\n
\"
boolean
\"
: false,
\n
\"
null
\"
: null,
\n
\"
number
\"
: 42,
\n
\"
object
\"
: {},
\n
\"
string
\"
:
\"
Hello world
\"\n
}"
);
CHECK
(
j
.
dump
(
4
)
==
"{
\n
\"
array
\"
: [
\n
1,
\n
2,
\n
3,
\n
4
\n
],
\n
\"
boolean
\"
: false,
\n
\"
null
\"
: null,
\n
\"
number
\"
: 42,
\n
\"
object
\"
: {},
\n
\"
string
\"
:
\"
Hello world
\"\n
}"
);
}
}
SECTION
(
"dump and floating-point numbers"
)
SECTION
(
"dump and floating-point numbers"
)
...
@@ -1157,3 +1160,42 @@ TEST_CASE("object inspection")
...
@@ -1157,3 +1160,42 @@ TEST_CASE("object inspection")
}
}
}
}
}
}
TEST_CASE
(
"value conversion"
)
{
SECTION
(
"get an object (explicit)"
)
{
json
::
object_t
o_reference
=
{{
"object"
,
json
::
object
()},
{
"array"
,
{
1
,
2
,
3
,
4
}},
{
"number"
,
42
},
{
"boolean"
,
false
},
{
"null"
,
nullptr
},
{
"string"
,
"Hello world"
}
};
json
j
(
o_reference
);
SECTION
(
"json::object_t"
)
{
json
::
object_t
o
=
j
.
get
<
std
::
map
<
std
::
string
,
json
>>
();
CHECK
(
json
(
o
)
==
j
);
}
SECTION
(
"std::map<std::string, json>"
)
{
std
::
map
<
std
::
string
,
json
>
o
=
j
.
get
<
std
::
map
<
std
::
string
,
json
>>
();
CHECK
(
json
(
o
)
==
j
);
}
SECTION
(
"std::multimap<std::string, json>"
)
{
std
::
multimap
<
std
::
string
,
json
>
o
=
j
.
get
<
std
::
multimap
<
std
::
string
,
json
>>
();
CHECK
(
json
(
o
)
==
j
);
}
SECTION
(
"std::unordered_map<std::string, json>"
)
{
std
::
unordered_map
<
std
::
string
,
json
>
o
=
j
.
get
<
std
::
unordered_map
<
std
::
string
,
json
>>
();
CHECK
(
json
(
o
)
==
j
);
}
SECTION
(
"std::unordered_multimap<std::string, json>"
)
{
std
::
unordered_multimap
<
std
::
string
,
json
>
o
=
j
.
get
<
std
::
unordered_multimap
<
std
::
string
,
json
>>
();
CHECK
(
json
(
o
)
==
j
);
}
}
}
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