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
807de404
Commit
807de404
authored
Jan 23, 2015
by
Niels
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
implemented missing operator[]
parent
36f14d21
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
54 additions
and
10 deletions
+54
-10
header_only/json.h
header_only/json.h
+18
-5
src/json.cc
src/json.cc
+10
-2
src/json.h
src/json.h
+8
-3
test/json_unit.cc
test/json_unit.cc
+18
-0
No files found.
header_only/json.h
View file @
807de404
...
...
@@ -192,11 +192,14 @@ class json
@brief create an array object
@param v any type of container whose elements can be use to construct
JSON objects (e.g., std::vector, std::set, std::array)
@note For some reason, we need to explicitly forbid JSON iterator types.
*/
template
<
class
V
,
typename
std
::
enable_if
<
not
std
::
is_same
<
V
,
json
::
const_iterator
>
::
value
and
not
std
::
is_same
<
V
,
json
::
iterator
>
::
value
and
not
std
::
is_same
<
V
,
json
::
const_iterator
>::
value
and
not
std
::
is_same
<
V
,
json
::
reverse_iterator
>::
value
and
not
std
::
is_same
<
V
,
json
::
const_reverse_iterator
>::
value
and
std
::
is_constructible
<
json
,
typename
V
::
value_type
>::
value
,
int
>::
type
=
0
>
json
(
const
V
&
v
)
:
json
(
array_t
(
v
.
begin
(),
v
.
end
()))
...
...
@@ -325,9 +328,9 @@ class json
void
push_back
(
list_init_t
);
/// operator to set an element in an array
json
&
operator
[](
const
int
);
reference
operator
[](
const
int
);
/// operator to get an element in an array
const
json
&
operator
[](
const
int
)
const
;
const
_reference
operator
[](
const
int
)
const
;
/// operator to get an element in an array
reference
at
(
const
int
);
/// operator to get an element in an array
...
...
@@ -339,6 +342,8 @@ class json
reference
operator
[](
const
char
*
);
/// operator to get an element in an object
const_reference
operator
[](
const
std
::
string
&
)
const
;
/// operator to get an element in an object
const_reference
operator
[](
const
char
*
)
const
;
/// operator to set an element in an object
reference
at
(
const
std
::
string
&
);
/// operator to set an element in an object
...
...
@@ -1546,6 +1551,14 @@ json::reference json::operator[](const char* key)
return
(
*
value_
.
object
)[
key
];
}
/*!
@copydoc json::operator[](const char* key)
*/
json
::
const_reference
json
::
operator
[](
const
std
::
string
&
key
)
const
{
return
operator
[](
key
.
c_str
());
}
/*!
This operator realizes read-only access to object elements given a string
key.
...
...
@@ -1557,7 +1570,7 @@ key.
@exception std::domain_error if object is not an object
@exception std::out_of_range if key is not found in object
*/
json
::
const_reference
json
::
operator
[](
const
std
::
string
&
key
)
const
json
::
const_reference
json
::
operator
[](
const
char
*
key
)
const
{
// this [] operator only works for objects
if
(
type_
!=
value_t
::
object
)
...
...
@@ -1572,7 +1585,7 @@ json::const_reference json::operator[](const std::string& key) const
// make sure the key exists in the object
if
(
it
==
value_
.
object
->
end
())
{
throw
std
::
out_of_range
(
"key "
+
key
+
" not found"
);
throw
std
::
out_of_range
(
"key "
+
std
::
string
(
key
)
+
" not found"
);
}
// return element from array at given key
...
...
src/json.cc
View file @
807de404
...
...
@@ -981,6 +981,14 @@ json::reference json::operator[](const char* key)
return
(
*
value_
.
object
)[
key
];
}
/*!
@copydoc json::operator[](const char* key)
*/
json
::
const_reference
json
::
operator
[](
const
std
::
string
&
key
)
const
{
return
operator
[](
key
.
c_str
());
}
/*!
This operator realizes read-only access to object elements given a string
key.
...
...
@@ -992,7 +1000,7 @@ key.
@exception std::domain_error if object is not an object
@exception std::out_of_range if key is not found in object
*/
json
::
const_reference
json
::
operator
[](
const
std
::
string
&
key
)
const
json
::
const_reference
json
::
operator
[](
const
char
*
key
)
const
{
// this [] operator only works for objects
if
(
type_
!=
value_t
::
object
)
...
...
@@ -1007,7 +1015,7 @@ json::const_reference json::operator[](const std::string& key) const
// make sure the key exists in the object
if
(
it
==
value_
.
object
->
end
())
{
throw
std
::
out_of_range
(
"key "
+
key
+
" not found"
);
throw
std
::
out_of_range
(
"key "
+
std
::
string
(
key
)
+
" not found"
);
}
// return element from array at given key
...
...
src/json.h
View file @
807de404
...
...
@@ -192,11 +192,14 @@ class json
@brief create an array object
@param v any type of container whose elements can be use to construct
JSON objects (e.g., std::vector, std::set, std::array)
@note For some reason, we need to explicitly forbid JSON iterator types.
*/
template
<
class
V
,
typename
std
::
enable_if
<
not
std
::
is_same
<
V
,
json
::
const_iterator
>
::
value
and
not
std
::
is_same
<
V
,
json
::
iterator
>
::
value
and
not
std
::
is_same
<
V
,
json
::
const_iterator
>::
value
and
not
std
::
is_same
<
V
,
json
::
reverse_iterator
>::
value
and
not
std
::
is_same
<
V
,
json
::
const_reverse_iterator
>::
value
and
std
::
is_constructible
<
json
,
typename
V
::
value_type
>::
value
,
int
>::
type
=
0
>
json
(
const
V
&
v
)
:
json
(
array_t
(
v
.
begin
(),
v
.
end
()))
...
...
@@ -325,9 +328,9 @@ class json
void
push_back
(
list_init_t
);
/// operator to set an element in an array
json
&
operator
[](
const
int
);
reference
operator
[](
const
int
);
/// operator to get an element in an array
const
json
&
operator
[](
const
int
)
const
;
const
_reference
operator
[](
const
int
)
const
;
/// operator to get an element in an array
reference
at
(
const
int
);
/// operator to get an element in an array
...
...
@@ -339,6 +342,8 @@ class json
reference
operator
[](
const
char
*
);
/// operator to get an element in an object
const_reference
operator
[](
const
std
::
string
&
)
const
;
/// operator to get an element in an object
const_reference
operator
[](
const
char
*
)
const
;
/// operator to set an element in an object
reference
at
(
const
std
::
string
&
);
/// operator to set an element in an object
...
...
test/json_unit.cc
View file @
807de404
...
...
@@ -504,6 +504,18 @@ TEST_CASE("object")
bool
v4
=
j
[
std
::
string
(
"k4"
)];
CHECK
(
v4
==
true
);
}
{
const
std
::
string
v0
=
k
[
"k0"
];
CHECK
(
v0
==
"v0"
);
auto
v1
=
k
[
"k1"
];
CHECK
(
v1
==
nullptr
);
int
v2
=
k
[
"k2"
];
CHECK
(
v2
==
42
);
double
v3
=
k
[
"k3"
];
CHECK
(
v3
==
3.141
);
bool
v4
=
k
[
"k4"
];
CHECK
(
v4
==
true
);
}
{
const
std
::
string
v0
=
k
[
std
::
string
(
"k0"
)];
CHECK
(
v0
==
"v0"
);
...
...
@@ -925,6 +937,12 @@ TEST_CASE("null")
CHECK
(
n1
==
json
());
CHECK
(
n2
==
json
());
json
::
reverse_iterator
ri
=
n1
.
rbegin
();
ri
--
;
json
::
const_reverse_iterator
rci
=
n1
.
crbegin
();
rci
--
;
}
}
...
...
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