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
a68b4caf
Commit
a68b4caf
authored
Dec 28, 2014
by
Niels
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
+ more test cases
parent
2e26faf9
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
18 additions
and
2 deletions
+18
-2
JSON.cc
src/JSON.cc
+12
-2
JSON.h
src/JSON.h
+2
-0
JSON_unit.cc
test/JSON_unit.cc
+4
-0
No files found.
src/JSON.cc
View file @
a68b4caf
...
@@ -1099,7 +1099,8 @@ const JSON& JSON::operator[](const std::string& key) const
...
@@ -1099,7 +1099,8 @@ const JSON& JSON::operator[](const std::string& key) const
// this [] operator only works for objects
// this [] operator only works for objects
if
(
_type
!=
value_type
::
object
)
if
(
_type
!=
value_type
::
object
)
{
{
throw
std
::
domain_error
(
"cannot get entry with key "
+
std
::
string
(
key
)
+
" from "
+
_typename
());
throw
std
::
domain_error
(
"cannot get entry with key "
+
std
::
string
(
key
)
+
" from "
+
_typename
());
}
}
// search for the key
// search for the key
...
@@ -1152,6 +1153,14 @@ JSON& JSON::at(const char* key)
...
@@ -1152,6 +1153,14 @@ JSON& JSON::at(const char* key)
}
}
/*!
/*!
@copydoc JSON::at(const char *key) const
*/
const
JSON
&
JSON
::
at
(
const
std
::
string
&
key
)
const
{
return
at
(
key
.
c_str
());
}
/*!
This operator realizes read-only access to object elements given a string
This operator realizes read-only access to object elements given a string
key.
key.
...
@@ -1162,7 +1171,7 @@ key.
...
@@ -1162,7 +1171,7 @@ key.
@exception std::domain_error if object is not an object
@exception std::domain_error if object is not an object
@exception std::out_of_range if key is not found (via std::map::at)
@exception std::out_of_range if key is not found (via std::map::at)
*/
*/
const
JSON
&
JSON
::
at
(
const
std
::
string
&
key
)
const
const
JSON
&
JSON
::
at
(
const
char
*
key
)
const
{
{
// this [] operator only works for objects
// this [] operator only works for objects
if
(
_type
!=
value_type
::
object
)
if
(
_type
!=
value_type
::
object
)
...
@@ -1175,6 +1184,7 @@ const JSON& JSON::at(const std::string& key) const
...
@@ -1175,6 +1184,7 @@ const JSON& JSON::at(const std::string& key) const
return
_value
.
object
->
at
(
key
);
return
_value
.
object
->
at
(
key
);
}
}
/*!
/*!
Returns the size of the JSON object.
Returns the size of the JSON object.
...
...
src/JSON.h
View file @
a68b4caf
...
@@ -257,6 +257,8 @@ class JSON
...
@@ -257,6 +257,8 @@ class JSON
JSON
&
at
(
const
char
*
);
JSON
&
at
(
const
char
*
);
/// operator to get an element in an object
/// operator to get an element in an object
const
JSON
&
at
(
const
std
::
string
&
)
const
;
const
JSON
&
at
(
const
std
::
string
&
)
const
;
/// operator to get an element in an object
const
JSON
&
at
(
const
char
*
)
const
;
/// return the number of stored values
/// return the number of stored values
size_t
size
()
const
noexcept
;
size_t
size
()
const
noexcept
;
...
...
test/JSON_unit.cc
View file @
a68b4caf
...
@@ -507,6 +507,10 @@ TEST_CASE("object")
...
@@ -507,6 +507,10 @@ TEST_CASE("object")
JSON
nonarray
=
1
;
JSON
nonarray
=
1
;
CHECK_THROWS_AS
(
const
int
i
=
nonarray
[
"v1"
],
std
::
domain_error
);
CHECK_THROWS_AS
(
const
int
i
=
nonarray
[
"v1"
],
std
::
domain_error
);
CHECK_THROWS_AS
(
nonarray
[
"v1"
]
=
10
,
std
::
domain_error
);
CHECK_THROWS_AS
(
nonarray
[
"v1"
]
=
10
,
std
::
domain_error
);
{
const
JSON
c
=
{{
"foo"
,
"bar"
}};
CHECK_THROWS_AS
(
c
[
std
::
string
(
"baz"
)],
std
::
out_of_range
);
}
// clear()
// clear()
JSON
j7
=
{{
"k0"
,
0
},
{
"k1"
,
1
},
{
"k2"
,
2
},
{
"k3"
,
3
}};
JSON
j7
=
{{
"k0"
,
0
},
{
"k1"
,
1
},
{
"k2"
,
2
},
{
"k3"
,
3
}};
...
...
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