JSON for Modern C++ 3.10.4

◆ basic_json() [1/10]

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>>
nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType >::basic_json ( const value_t  v)
inline

Create an empty JSON value with a given type. The value will be default initialized with an empty value which depends on the type:

Value type initial value
null null
boolean false
string ""
number 0
object {}
array []
binary empty array
Parameters
[in]vthe type of the value to create
Complexity
Constant.
Exception safety
Strong guarantee: if an exception is thrown, there are no changes to any JSON value.
Example
The following code shows the constructor for different value_t values
1#include <iostream>
2#include <nlohmann/json.hpp>
3
4using json = nlohmann::json;
5
6int main()
7{
8 // create the different JSON values with default values
9 json j_null(json::value_t::null);
10 json j_boolean(json::value_t::boolean);
11 json j_number_integer(json::value_t::number_integer);
12 json j_number_float(json::value_t::number_float);
13 json j_object(json::value_t::object);
14 json j_array(json::value_t::array);
15 json j_string(json::value_t::string);
16
17 // serialize the JSON values
18 std::cout << j_null << '\n';
19 std::cout << j_boolean << '\n';
20 std::cout << j_number_integer << '\n';
21 std::cout << j_number_float << '\n';
22 std::cout << j_object << '\n';
23 std::cout << j_array << '\n';
24 std::cout << j_string << '\n';
25}
basic_json<> json
default JSON class
Definition: json.hpp:3472

Output (play with this example online):
null
false
0
0.0
{}
[]
""
The example code above can be translated with
g++ -std=c++11 -Isingle_include doc/examples/basic_json__value_t.cpp -o basic_json__value_t 
See also
see clear() – restores the postcondition of this constructor
Since
version 1.0.0

Definition at line 18975 of file json.hpp.