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
2ec0c79e
Commit
2ec0c79e
authored
Feb 09, 2015
by
Niels
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
test cases and clean up
parent
0cfd0f5d
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
136 additions
and
88 deletions
+136
-88
src/json.hpp
src/json.hpp
+7
-44
src/json.hpp.re2c
src/json.hpp.re2c
+7
-44
test/unit.cpp
test/unit.cpp
+122
-0
No files found.
src/json.hpp
View file @
2ec0c79e
...
@@ -971,6 +971,13 @@ class basic_json
...
@@ -971,6 +971,13 @@ class basic_json
value
.
m_type
=
value_t
::
null
;
value
.
m_type
=
value_t
::
null
;
}
}
/// add an object to an array
inline
reference
operator
+=
(
basic_json
&&
value
)
{
push_back
(
std
::
move
(
value
));
return
*
this
;
}
/// add an object to an array
/// add an object to an array
inline
void
push_back
(
const
basic_json
&
value
)
inline
void
push_back
(
const
basic_json
&
value
)
{
{
...
@@ -991,32 +998,12 @@ class basic_json
...
@@ -991,32 +998,12 @@ class basic_json
m_value
.
array
->
push_back
(
value
);
m_value
.
array
->
push_back
(
value
);
}
}
/*
/// add an object to an array
/// add an object to an array
inline
reference
operator
+=
(
const
basic_json
&
value
)
inline
reference
operator
+=
(
const
basic_json
&
value
)
{
{
push_back
(
value
);
push_back
(
value
);
return
*
this
;
return
*
this
;
}
}
*/
/// add constructible objects to an array
template
<
class
T
,
typename
std
::
enable_if
<
std
::
is_constructible
<
basic_json
,
T
>
::
value
>::
type
=
0
>
inline
void
push_back
(
const
T
&
value
)
{
assert
(
false
);
// not sure if function will ever be called
push_back
(
basic_json
(
value
));
}
/*
/// add constructible objects to an array
template<class T, typename std::enable_if<std::is_constructible<basic_json, T>::value>::type = 0>
inline reference operator+=(const T& value)
{
push_back(basic_json(value));
return *this;
}
*/
/// add an object to an object
/// add an object to an object
inline
void
push_back
(
const
typename
object_t
::
value_type
&
value
)
inline
void
push_back
(
const
typename
object_t
::
value_type
&
value
)
...
@@ -1047,30 +1034,6 @@ class basic_json
...
@@ -1047,30 +1034,6 @@ class basic_json
}
}
*/
*/
/// constructs element in-place at the end of an array
template
<
typename
T
,
typename
std
::
enable_if
<
std
::
is_constructible
<
basic_json
,
T
>
::
value
,
int
>::
type
=
0
>
inline
void
emplace_back
(
T
&&
arg
)
{
// push_back only works for null objects or arrays
if
(
not
(
m_type
==
value_t
::
null
or
m_type
==
value_t
::
array
))
{
throw
std
::
runtime_error
(
"cannot add element to "
+
type_name
());
}
// transform null object into an array
if
(
m_type
==
value_t
::
null
)
{
m_type
=
value_t
::
array
;
m_value
.
array
=
new
array_t
;
}
// add element to array
m_value
.
array
->
emplace_back
(
std
::
forward
<
T
>
(
arg
));
}
/// swaps the contents
/// swaps the contents
inline
void
swap
(
reference
other
)
noexcept
inline
void
swap
(
reference
other
)
noexcept
{
{
...
...
src/json.hpp.re2c
View file @
2ec0c79e
...
@@ -971,6 +971,13 @@ class basic_json
...
@@ -971,6 +971,13 @@ class basic_json
value.m_type = value_t::null;
value.m_type = value_t::null;
}
}
/// add an object to an array
inline reference operator+=(basic_json&& value)
{
push_back(std::move(value));
return *this;
}
/// add an object to an array
/// add an object to an array
inline void push_back(const basic_json& value)
inline void push_back(const basic_json& value)
{
{
...
@@ -991,32 +998,12 @@ class basic_json
...
@@ -991,32 +998,12 @@ class basic_json
m_value.array->push_back(value);
m_value.array->push_back(value);
}
}
/*
/// add an object to an array
/// add an object to an array
inline reference operator+=(const basic_json& value)
inline reference operator+=(const basic_json& value)
{
{
push_back(value);
push_back(value);
return *this;
return *this;
}
}
*/
/// add constructible objects to an array
template<class T, typename std::enable_if<std::is_constructible<basic_json, T>::value>::type = 0>
inline void push_back(const T& value)
{
assert(false); // not sure if function will ever be called
push_back(basic_json(value));
}
/*
/// add constructible objects to an array
template<class T, typename std::enable_if<std::is_constructible<basic_json, T>::value>::type = 0>
inline reference operator+=(const T& value)
{
push_back(basic_json(value));
return *this;
}
*/
/// add an object to an object
/// add an object to an object
inline void push_back(const typename object_t::value_type& value)
inline void push_back(const typename object_t::value_type& value)
...
@@ -1047,30 +1034,6 @@ class basic_json
...
@@ -1047,30 +1034,6 @@ class basic_json
}
}
*/
*/
/// constructs element in-place at the end of an array
template <typename T, typename
std::enable_if<
std::is_constructible<basic_json, T>::value, int>::type
= 0>
inline void emplace_back(T && arg)
{
// push_back only works for null objects or arrays
if (not(m_type == value_t::null or m_type == value_t::array))
{
throw std::runtime_error("cannot add element to " + type_name());
}
// transform null object into an array
if (m_type == value_t::null)
{
m_type = value_t::array;
m_value.array = new array_t;
}
// add element to array
m_value.array->emplace_back(std::forward<T>(arg));
}
/// swaps the contents
/// swaps the contents
inline void swap(reference other) noexcept
inline void swap(reference other) noexcept
{
{
...
...
test/unit.cpp
View file @
2ec0c79e
...
@@ -3465,6 +3465,128 @@ TEST_CASE("modifiers")
...
@@ -3465,6 +3465,128 @@ TEST_CASE("modifiers")
}
}
}
}
SECTION
(
"push_back()"
)
{
SECTION
(
"to array"
)
{
SECTION
(
"json&&"
)
{
SECTION
(
"null"
)
{
json
j
;
j
.
push_back
(
1
);
j
.
push_back
(
2
);
CHECK
(
j
.
type
()
==
json
::
value_t
::
array
);
CHECK
(
j
==
json
({
1
,
2
}));
}
SECTION
(
"array"
)
{
json
j
=
{
1
,
2
,
3
};
j
.
push_back
(
"Hello"
);
CHECK
(
j
.
type
()
==
json
::
value_t
::
array
);
CHECK
(
j
==
json
({
1
,
2
,
3
,
"Hello"
}));
}
SECTION
(
"other type"
)
{
json
j
=
1
;
CHECK_THROWS_AS
(
j
.
push_back
(
"Hello"
),
std
::
runtime_error
);
}
}
SECTION
(
"const json&"
)
{
SECTION
(
"null"
)
{
json
j
;
json
k
(
1
);
j
.
push_back
(
k
);
j
.
push_back
(
k
);
CHECK
(
j
.
type
()
==
json
::
value_t
::
array
);
CHECK
(
j
==
json
({
1
,
1
}));
}
SECTION
(
"array"
)
{
json
j
=
{
1
,
2
,
3
};
json
k
(
"Hello"
);
j
.
push_back
(
k
);
CHECK
(
j
.
type
()
==
json
::
value_t
::
array
);
CHECK
(
j
==
json
({
1
,
2
,
3
,
"Hello"
}));
}
SECTION
(
"other type"
)
{
json
j
=
1
;
json
k
(
"Hello"
);
CHECK_THROWS_AS
(
j
.
push_back
(
k
),
std
::
runtime_error
);
}
}
}
}
SECTION
(
"operator+="
)
{
SECTION
(
"to array"
)
{
SECTION
(
"json&&"
)
{
SECTION
(
"null"
)
{
json
j
;
j
+=
1
;
j
+=
2
;
CHECK
(
j
.
type
()
==
json
::
value_t
::
array
);
CHECK
(
j
==
json
({
1
,
2
}));
}
SECTION
(
"array"
)
{
json
j
=
{
1
,
2
,
3
};
j
+=
"Hello"
;
CHECK
(
j
.
type
()
==
json
::
value_t
::
array
);
CHECK
(
j
==
json
({
1
,
2
,
3
,
"Hello"
}));
}
SECTION
(
"other type"
)
{
json
j
=
1
;
CHECK_THROWS_AS
(
j
+=
"Hello"
,
std
::
runtime_error
);
}
}
SECTION
(
"const json&"
)
{
SECTION
(
"null"
)
{
json
j
;
json
k
(
1
);
j
+=
k
;
j
+=
k
;
CHECK
(
j
.
type
()
==
json
::
value_t
::
array
);
CHECK
(
j
==
json
({
1
,
1
}));
}
SECTION
(
"array"
)
{
json
j
=
{
1
,
2
,
3
};
json
k
(
"Hello"
);
j
+=
k
;
CHECK
(
j
.
type
()
==
json
::
value_t
::
array
);
CHECK
(
j
==
json
({
1
,
2
,
3
,
"Hello"
}));
}
SECTION
(
"other type"
)
{
json
j
=
1
;
json
k
(
"Hello"
);
CHECK_THROWS_AS
(
j
+=
k
,
std
::
runtime_error
);
}
}
}
}
SECTION
(
"swap()"
)
SECTION
(
"swap()"
)
{
{
SECTION
(
"json"
)
SECTION
(
"json"
)
...
...
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