JSON for Modern C++ 2.1.1
Loading...
Searching...
No Matches

◆ basic_json() [1/9]

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>
nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::basic_json ( const value_t  value_type)
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 []
Parameters
[in]value_typethe type of the value to create
Complexity\n Constant.
Exceptions
std::bad_allocif allocation for object, array, or string value fails
Example\n The following code shows the constructor for different @ref
value_t values
1#include <json.hpp>
2
3using json = nlohmann::json;
4
5int main()
6{
7 // create the different JSON values with default values
8 json j_null(json::value_t::null);
9 json j_boolean(json::value_t::boolean);
10 json j_number_integer(json::value_t::number_integer);
11 json j_number_float(json::value_t::number_float);
14 json j_string(json::value_t::string);
15
16 // serialize the JSON values
17 std::cout << j_null << '\n';
18 std::cout << j_boolean << '\n';
19 std::cout << j_number_integer << '\n';
20 std::cout << j_number_float << '\n';
21 std::cout << j_object << '\n';
22 std::cout << j_array << '\n';
23 std::cout << j_string << '\n';
24}
static basic_json array(std::initializer_list< basic_json > init=std::initializer_list< basic_json >())
explicitly create an array from an initializer list
Definition json.hpp:2165
static basic_json object(std::initializer_list< basic_json > init=std::initializer_list< basic_json >())
explicitly create an object from an initializer list
Definition json.hpp:2205
a class to store JSON values
Definition json.hpp:1040
basic_json<> json
default JSON class
Definition json.hpp:12369

Output (play with this example online):
null
false
0
0.0
{}
[]
""

The example code above can be translated with
g++ -std=c++11 -Isrc doc/examples/basic_json__value_t.cpp -o basic_json__value_t 
Since
version 1.0.0

Definition at line 1916 of file json.hpp.