JSON for Modern C++ 3.10.4

◆ get_ref() [2/2]

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 = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer, class BinaryType = std::vector<std::uint8_t>>
template<typename ReferenceType , typename std::enable_if< std::is_reference< ReferenceType >::value &&std::is_const< typename std::remove_reference< ReferenceType >::type >::value, int >::type = 0>
ReferenceType nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType >::get_ref ( ) const
inline

get a reference value (implicit) Implicit reference access to the internally stored JSON value. No copies are made.

Warning
Writing data to the referee of the result yields an undefined state.
Template Parameters
ReferenceTypereference type; must be a reference to array_t, object_t, string_t, boolean_t, number_integer_t, or number_float_t. Enforced by static assertion.
Returns
reference to the internally stored JSON value if the requested reference type ReferenceType fits to the JSON value; throws type_error.303 otherwise
Exceptions
type_error.303in case passed type ReferenceType is incompatible with the stored JSON value; see example below
Complexity
Constant.
Example
The example shows several calls to get_ref().
1#include <iostream>
2#include <nlohmann/json.hpp>
3
4using json = nlohmann::json;
5
6int main()
7{
8 // create a JSON number
9 json value = 17;
10
11 // explicitly getting references
12 auto r1 = value.get_ref<const json::number_integer_t&>();
13 auto r2 = value.get_ref<json::number_integer_t&>();
14
15 // print the values
16 std::cout << r1 << ' ' << r2 << '\n';
17
18 // incompatible type throws exception
19 try
20 {
21 auto r3 = value.get_ref<json::number_float_t&>();
22 }
23 catch (json::type_error& ex)
24 {
25 std::cout << ex.what() << '\n';
26 }
27}
ValueType value(const typename object_t::key_type &key, const ValueType &default_value) const
access specified object element with default value
Definition: json.hpp:21500
NumberIntegerType number_integer_t
a type for a number (integer)
Definition: json.hpp:18212
NumberFloatType number_float_t
a type for a number (floating-point)
Definition: json.hpp:18351
detail::type_error type_error
exception indicating executing a member function with a wrong type
Definition: json.hpp:17769
basic_json<> json
default JSON class
Definition: json.hpp:3472

Output (play with this example online):
17 17
[json.exception.type_error.303] incompatible ReferenceType for get_ref, actual type is number
The example code above can be translated with
g++ -std=c++11 -Isingle_include doc/examples/get_ref.cpp -o get_ref 
Since
version 1.1.0

Definition at line 20866 of file json.hpp.