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
75dbbc55
Commit
75dbbc55
authored
Jul 23, 2016
by
Niels
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
started to implement #283
parent
ddfe86cc
Changes
3
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
262 additions
and
115 deletions
+262
-115
src/json.hpp
src/json.hpp
+50
-3
src/json.hpp.re2c
src/json.hpp.re2c
+50
-3
test/src/unit.cpp
test/src/unit.cpp
+162
-109
No files found.
src/json.hpp
View file @
75dbbc55
...
...
@@ -3633,8 +3633,8 @@ class basic_json
/*!
@brief access specified object element with default value
Returns either a copy of an object's element at the specified key @a key
or
a given default value if no element with key @a key exists.
Returns either a copy of an object's element at the specified key @a key
or
a given default value if no element with key @a key exists.
The function is basically equivalent to executing
@code {.cpp}
...
...
@@ -3706,13 +3706,60 @@ class basic_json
/*!
@brief overload for a default value of type const char*
@copydoc basic_json::value()
@copydoc basic_json::value(
const typename object_t::key_type&, ValueType
)
*/
string_t
value
(
const
typename
object_t
::
key_type
&
key
,
const
char
*
default_value
)
const
{
return
value
(
key
,
string_t
(
default_value
));
}
/*!
@brief access specified object element via JSON Pointer with default value
@param[in] ptr a JSON pointer to the element to access
@param[in] default_value the value to return if @a ptr found no value
@tparam ValueType type compatible to JSON values, for instance `int` for
JSON integer numbers, `bool` for JSON booleans, or `std::vector` types for
JSON arrays. Note the type of the expected value at @a key and the default
value @a default_value must be compatible.
@since version 2.0.2
*/
template
<
class
ValueType
,
typename
std
::
enable_if
<
std
::
is_convertible
<
basic_json_t
,
ValueType
>
::
value
,
int
>::
type
=
0
>
ValueType
value
(
const
json_pointer
&
ptr
,
ValueType
default_value
)
const
{
// at only works for objects
if
(
is_object
())
{
// if pointer resolves a value, return it or use default value
try
{
return
ptr
.
get_checked
(
this
);
}
catch
(
std
::
out_of_range
&
)
{
return
default_value
;
}
}
else
{
throw
std
::
domain_error
(
"cannot use value() with "
+
type_name
());
}
}
/*!
@brief overload for a default value of type const char*
@copydoc basic_json::value(const json_pointer&, ValueType)
*/
string_t
value
(
const
json_pointer
&
ptr
,
const
char
*
default_value
)
const
{
return
value
(
ptr
,
string_t
(
default_value
));
}
/*!
@brief access the first element
...
...
src/json.hpp.re2c
View file @
75dbbc55
...
...
@@ -3633,8 +3633,8 @@ class basic_json
/*!
@brief access specified object element with default value
Returns either a copy of an object's element at the specified key @a key
or
a given default value if no element with key @a key exists.
Returns either a copy of an object's element at the specified key @a key
or
a given default value if no element with key @a key exists.
The function is basically equivalent to executing
@code {.cpp}
...
...
@@ -3706,13 +3706,60 @@ class basic_json
/*!
@brief overload for a default value of type const char*
@copydoc basic_json::value()
@copydoc basic_json::value(
const typename object_t::key_type&, ValueType
)
*/
string_t value(const typename object_t::key_type& key, const char* default_value) const
{
return value(key, string_t(default_value));
}
/*!
@brief access specified object element via JSON Pointer with default value
@param[in] ptr a JSON pointer to the element to access
@param[in] default_value the value to return if @a ptr found no value
@tparam ValueType type compatible to JSON values, for instance `int` for
JSON integer numbers, `bool` for JSON booleans, or `std::vector` types for
JSON arrays. Note the type of the expected value at @a key and the default
value @a default_value must be compatible.
@since version 2.0.2
*/
template <class ValueType, typename
std::enable_if<
std::is_convertible<basic_json_t, ValueType>::value
, int>::type = 0>
ValueType value(const json_pointer& ptr, ValueType default_value) const
{
// at only works for objects
if (is_object())
{
// if pointer resolves a value, return it or use default value
try
{
return ptr.get_checked(this);
}
catch (std::out_of_range&)
{
return default_value;
}
}
else
{
throw std::domain_error("cannot use value() with " + type_name());
}
}
/*!
@brief overload for a default value of type const char*
@copydoc basic_json::value(const json_pointer&, ValueType)
*/
string_t value(const json_pointer& ptr, const char* default_value) const
{
return value(ptr, string_t(default_value));
}
/*!
@brief access the first element
...
...
test/src/unit.cpp
View file @
75dbbc55
This diff is collapsed.
Click to expand it.
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