This commit is contained in:
Niels 2016-04-07 20:13:58 +00:00
commit b265a7ffab
3 changed files with 134 additions and 47 deletions

View File

@ -103,6 +103,36 @@ class DecimalSeparator : public std::numpunct<char>
} }
/*!
@brief default type for objects
This type is used to store objects if no other type is provided to @ref
basic_json via the @a ObjectType template parameter. Defaults to `std::map<Key,
T>`.
@tparam Key type of the object keys
@tparam T type of the object elements
@since version 2.0.0
*/
template<typename Key, typename T>
using default_object_type = std::map<Key, T>;
/*!
@brief default type for arrays
This type is used to store arrays if no other type is provided to @ref
basic_json via the @a ArrayType template parameter. Defaults to
`std::vector<T>`.
@tparam T type of the array elements
@since version 2.0.0
*/
template<typename T>
using default_array_type = std::vector<T>;
/*! /*!
@brief a class to store JSON values @brief a class to store JSON values
@ -175,8 +205,8 @@ Format](http://rfc7159.net/rfc7159)
@nosubgrouping @nosubgrouping
*/ */
template < template <
template<typename U, typename V, typename... Args> class ObjectType = std::map, template<typename Key, typename T> class ObjectType = default_object_type,
template<typename U, typename... Args> class ArrayType = std::vector, template<typename T> class ArrayType = default_array_type,
class StringType = std::string, class StringType = std::string,
class BooleanType = bool, class BooleanType = bool,
class NumberIntegerType = std::int64_t, class NumberIntegerType = std::int64_t,
@ -270,18 +300,20 @@ class basic_json
described below. described below.
@tparam ObjectType the container to store objects (e.g., `std::map` or @tparam ObjectType the container to store objects (e.g., `std::map` or
`std::unordered_map`) `std::unordered_map`). This container must have exactly two template
@tparam StringType the type of the keys or names (e.g., `std::string`). The arguments: @a Key for the keys and @a V for the stored values. That is,
comparison function `std::less<StringType>` is used to order elements `std::map` cannot be passed directly, because it has four template
inside the container. arguments. Instead, a type alias (like @ref default_object_type) has to be
@tparam AllocatorType the allocator to use for objects (e.g., provided, e.g.
`std::allocator`) @code {.cpp}
template<typename Key, typename V>
using my_object_type = std::map<Key, V>;
@endcode
#### Default type #### Default type
With the default values for @a ObjectType (`std::map`), @a StringType With the default values for @a ObjectType (@ref default_object_type) and @a
(`std::string`), and @a AllocatorType (`std::allocator`), the default value StringType (`std::string`), the default value for @a object_t is:
for @a object_t is:
@code {.cpp} @code {.cpp}
std::map< std::map<
@ -340,11 +372,7 @@ class basic_json
7159](http://rfc7159.net/rfc7159), because any order implements the 7159](http://rfc7159.net/rfc7159), because any order implements the
specified "unordered" nature of JSON objects. specified "unordered" nature of JSON objects.
*/ */
using object_t = ObjectType<StringType, using object_t = ObjectType<StringType, basic_json>;
basic_json,
std::less<StringType>,
AllocatorType<std::pair<const StringType,
basic_json>>>;
/*! /*!
@brief a type for an array @brief a type for an array
@ -356,13 +384,19 @@ class basic_json
explained below. explained below.
@tparam ArrayType container type to store arrays (e.g., `std::vector` or @tparam ArrayType container type to store arrays (e.g., `std::vector` or
`std::list`) `std::list`). This container must have exactly one template arguments: @a V
@tparam AllocatorType allocator to use for arrays (e.g., `std::allocator`) for the stored values. That is, `std::vector` cannot be passed directly,
because it has two template arguments. Instead, a type alias (like @ref
default_array_type) has to be provided, e.g.
@code {.cpp}
template<typename V>
using my_array_type = std::vector<V>;
@endcode
#### Default type #### Default type
With the default values for @a ArrayType (`std::vector`) and @a With the default values for @a ArrayType (@ref default_array_type), the
AllocatorType (`std::allocator`), the default value for @a array_t is: default value for @a array_t is:
@code {.cpp} @code {.cpp}
std::vector< std::vector<
@ -390,7 +424,7 @@ class basic_json
@since version 1.0.0 @since version 1.0.0
*/ */
using array_t = ArrayType<basic_json, AllocatorType<basic_json>>; using array_t = ArrayType<basic_json>;
/*! /*!
@brief a type for a string @brief a type for a string

View File

@ -103,6 +103,36 @@ class DecimalSeparator : public std::numpunct<char>
} }
/*!
@brief default type for objects
This type is used to store objects if no other type is provided to @ref
basic_json via the @a ObjectType template parameter. Defaults to `std::map<Key,
T>`.
@tparam Key type of the object keys
@tparam T type of the object elements
@since version 2.0.0
*/
template<typename Key, typename T>
using default_object_type = std::map<Key, T>;
/*!
@brief default type for arrays
This type is used to store arrays if no other type is provided to @ref
basic_json via the @a ArrayType template parameter. Defaults to
`std::vector<T>`.
@tparam T type of the array elements
@since version 2.0.0
*/
template<typename T>
using default_array_type = std::vector<T>;
/*! /*!
@brief a class to store JSON values @brief a class to store JSON values
@ -175,8 +205,8 @@ Format](http://rfc7159.net/rfc7159)
@nosubgrouping @nosubgrouping
*/ */
template < template <
template<typename U, typename V, typename... Args> class ObjectType = std::map, template<typename Key, typename T> class ObjectType = default_object_type,
template<typename U, typename... Args> class ArrayType = std::vector, template<typename T> class ArrayType = default_array_type,
class StringType = std::string, class StringType = std::string,
class BooleanType = bool, class BooleanType = bool,
class NumberIntegerType = std::int64_t, class NumberIntegerType = std::int64_t,
@ -270,18 +300,20 @@ class basic_json
described below. described below.
@tparam ObjectType the container to store objects (e.g., `std::map` or @tparam ObjectType the container to store objects (e.g., `std::map` or
`std::unordered_map`) `std::unordered_map`). This container must have exactly two template
@tparam StringType the type of the keys or names (e.g., `std::string`). The arguments: @a Key for the keys and @a V for the stored values. That is,
comparison function `std::less<StringType>` is used to order elements `std::map` cannot be passed directly, because it has four template
inside the container. arguments. Instead, a type alias (like @ref default_object_type) has to be
@tparam AllocatorType the allocator to use for objects (e.g., provided, e.g.
`std::allocator`) @code {.cpp}
template<typename Key, typename V>
using my_object_type = std::map<Key, V>;
@endcode
#### Default type #### Default type
With the default values for @a ObjectType (`std::map`), @a StringType With the default values for @a ObjectType (@ref default_object_type) and @a
(`std::string`), and @a AllocatorType (`std::allocator`), the default value StringType (`std::string`), the default value for @a object_t is:
for @a object_t is:
@code {.cpp} @code {.cpp}
std::map< std::map<
@ -340,11 +372,7 @@ class basic_json
7159](http://rfc7159.net/rfc7159), because any order implements the 7159](http://rfc7159.net/rfc7159), because any order implements the
specified "unordered" nature of JSON objects. specified "unordered" nature of JSON objects.
*/ */
using object_t = ObjectType<StringType, using object_t = ObjectType<StringType, basic_json>;
basic_json,
std::less<StringType>,
AllocatorType<std::pair<const StringType,
basic_json>>>;
/*! /*!
@brief a type for an array @brief a type for an array
@ -356,13 +384,19 @@ class basic_json
explained below. explained below.
@tparam ArrayType container type to store arrays (e.g., `std::vector` or @tparam ArrayType container type to store arrays (e.g., `std::vector` or
`std::list`) `std::list`). This container must have exactly one template arguments: @a V
@tparam AllocatorType allocator to use for arrays (e.g., `std::allocator`) for the stored values. That is, `std::vector` cannot be passed directly,
because it has two template arguments. Instead, a type alias (like @ref
default_array_type) has to be provided, e.g.
@code {.cpp}
template<typename V>
using my_array_type = std::vector<V>;
@endcode
#### Default type #### Default type
With the default values for @a ArrayType (`std::vector`) and @a With the default values for @a ArrayType (@ref default_array_type), the
AllocatorType (`std::allocator`), the default value for @a array_t is: default value for @a array_t is:
@code {.cpp} @code {.cpp}
std::vector< std::vector<
@ -390,7 +424,7 @@ class basic_json
@since version 1.0.0 @since version 1.0.0
*/ */
using array_t = ArrayType<basic_json, AllocatorType<basic_json>>; using array_t = ArrayType<basic_json>;
/*! /*!
@brief a type for a string @brief a type for a string

View File

@ -11970,6 +11970,9 @@ TEST_CASE("RFC 7159 examples")
} }
} }
template<typename Key, typename V>
using unordered_map_type = std::unordered_map<Key, V>;
TEST_CASE("Unicode", "[hide]") TEST_CASE("Unicode", "[hide]")
{ {
SECTION("full enumeration of Unicode codepoints") SECTION("full enumeration of Unicode codepoints")
@ -12150,8 +12153,17 @@ TEST_CASE("regression tests")
{ {
// create JSON class with nonstandard integer number type // create JSON class with nonstandard integer number type
using custom_json = using custom_json =
nlohmann::basic_json<std::map, std::vector, std::string, bool, int32_t, uint32_t, float>; nlohmann::basic_json <
nlohmann::default_object_type,
nlohmann::default_array_type,
std::string,
bool,
int32_t,
uint32_t,
float >;
custom_json j; custom_json j;
j["int_1"] = 1; j["int_1"] = 1;
// we need to cast to int to compile with Catch - the value is int32_t // we need to cast to int to compile with Catch - the value is int32_t
CHECK(static_cast<int>(j["int_1"]) == 1); CHECK(static_cast<int>(j["int_1"]) == 1);
@ -12271,11 +12283,16 @@ TEST_CASE("regression tests")
CHECK(s2 == "value"); CHECK(s2 == "value");
} }
SECTION("issue #146 - character following a surrogate pair is skipped") SECTION("issue #146 character following a surrogate pair is skipped")
{ {
CHECK(json::parse("\"\\ud80c\\udc60abc\"").get<json::string_t>() == u8"\U00013060abc"); CHECK(json::parse("\"\\ud80c\\udc60abc\"").get<json::string_t>() == u8"\U00013060abc");
} }
SECTION("issue #164 - std::unordered_map cannot be used as Object Type")
{
nlohmann::basic_json<unordered_map_type> unordered_json;
}
SECTION("issue #171 - Cannot index by key of type static constexpr const char*") SECTION("issue #171 - Cannot index by key of type static constexpr const char*")
{ {
json j; json j;
@ -12355,17 +12372,19 @@ TEST_CASE("regression tests")
// create JSON class with nonstandard float number type // create JSON class with nonstandard float number type
// float // float
nlohmann::basic_json<std::map, std::vector, std::string, bool, int32_t, uint32_t, float> j_float = nlohmann::basic_json<nlohmann::default_object_type, nlohmann::default_array_type, std::string, bool, int32_t, uint32_t, float>
j_float =
1.23e25f; 1.23e25f;
CHECK(j_float.get<float>() == 1.23e25f); CHECK(j_float.get<float>() == 1.23e25f);
// double // double
nlohmann::basic_json<std::map, std::vector, std::string, bool, int64_t, uint64_t, double> j_double = nlohmann::basic_json<nlohmann::default_object_type, nlohmann::default_array_type, std::string, bool, int64_t, uint64_t, double>
j_double =
1.23e35f; 1.23e35f;
CHECK(j_double.get<double>() == 1.23e35f); CHECK(j_double.get<double>() == 1.23e35f);
// long double // long double
nlohmann::basic_json<std::map, std::vector, std::string, bool, int64_t, uint64_t, long double> nlohmann::basic_json<nlohmann::default_object_type, nlohmann::default_array_type, std::string, bool, int64_t, uint64_t, long double>
j_long_double = 1.23e45L; j_long_double = 1.23e45L;
CHECK(j_long_double.get<long double>() == 1.23e45L); CHECK(j_long_double.get<long double>() == 1.23e45L);
} }