JSON for Modern C++ 1.0.0

- Main Page
- Classes

allocator_type
array
array_t
at
at
at
at
back
back
basic_json
basic_json
basic_json
basic_json
basic_json
basic_json
basic_json
basic_json
basic_json
basic_json
basic_json
basic_json
basic_json
basic_json
basic_json
basic_json
basic_json
basic_json
basic_json
basic_json
basic_json
begin
begin
boolean_t
cbegin
cend
clear
const_pointer
const_reference
const_reverse_iterator
count
crbegin
crend
difference_type
dump
empty
end
end
erase
erase
erase
erase
find
find
front
front
get
get
get
get_allocator
get_ptr
get_ptr
get_ref
get_ref
insert
insert
insert
insert
insert
is_array
is_boolean
is_discarded
is_null
is_number
is_number_float
is_number_integer
is_object
is_primitive
is_string
is_structured
iterator_wrapper
iterator_wrapper
max_size
number_float_t
number_integer_t
object
object_t
operator value_t
operator ValueType
operator!=
operator!=
operator!=
operator+=
operator+=
operator+=
operator<
operator<
operator<<
operator<<
operator<=
operator=
operator==
operator==
operator==
operator>
operator>=
operator>>
operator>>
operator[]
operator[]
operator[]
operator[]
operator[]
operator[]
operator[]
operator[]
parse
parse
parse
parse_event_t
parser_callback_t
pointer
push_back
push_back
push_back
rbegin
rbegin
reference
rend
rend
reverse_iterator
size
size_type
string_t
swap
swap
swap
swap
type
value
value
value_t
value_type
~basic_json
| template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = int64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator> template<class ValueType , typename std::enable_if< std::is_convertible< basic_json_t, ValueType >::value , int >::type = 0>
ValueType nlohmann::basic_json::value | ( | const typename object_t::key_type & | key,
|  | ValueType | default_value
| ) |  | const
| inline
  Returns either a copy of an object's element at the specified key key or a given default value if no element with key key exists. The function is basically equivalent to executing try { return at(key); } catch(std::out_of_range) { return default_value; } Note Unlike at(const typename object_t::key_type&), this function does not throw if the given key key was not found. Unlike operator[](const typename object_t::key_type& key), this function does not implicitly add an element to the position defined by key. This function is furthermore also applicable to const objects. Parameters
  [in] | key | key of the element to access
  [in] | default_value | the value to return if key is not found
  Template Parameters
  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 key and the default value default_value must be compatible.
  Returns copy of the element at key key or default_value if key is not found Exceptions
  std::domain_error | if JSON is not an object; example: `"cannot use value() with null"`
  Complexity Logarithmic in the size of the container. Example The example below shows how object elements can be queried with a default value. 1 #include <json.hpp> 2 3 using namespace nlohmann; 4 5 int main() 6 { 7 // create a JSON object with different entry types 8 json j = 9 { 10 {"integer", 1}, 11 {"floating", 42.23}, 12 {"string", "hello world"}, 13 {"boolean", true}, 14 {"object", {{"key1", 1}, {"key2", 2}}}, 15 {"array", {1, 2, 3}} 16 }; 17 18 // access existing values 19 int v_integer = j.value("integer", 0); 20 double v_floating = j.value("floating", 47.11); 21 22 // access nonexisting values and rely on default value 23 std::string v_string = j.value("nonexisting", "oops"); 24 bool v_boolean = j.value("nonexisting", false); 25 26 // output values 27 std::cout << std::boolalpha << v_integer << " " << v_floating 28 << " " << v_string << " " << v_boolean << "\n"; 29 } nlohmann::basic_json a class to store JSON values Definition: json.hpp:191 nlohmann::basic_json::value ValueType value(const typename object_t::key_type &key, ValueType default_value) const access specified object element with default value Definition: json.hpp:3277 nlohmann namespace for Niels Lohmann Definition: json.hpp:88 Output (play with this example online):
      1 42.23 oops false
  The example code above can be translated with
      g++ -std=c++11 -Isrc doc/examples/basic_json__value.cpp -o basic_json__value
See also at(const typename object_t::key_type&) for access by reference with range checking operator[](const typename object_t::key_type&) for unchecked access by reference Since version 1.0.0 Definition at line 3277 of file json.hpp.