mirror of
https://github.com/nlohmann/json.git
synced 2026-10-05 06:00:29 +00:00
Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ea72ffe0a3 | ||
|
|
7e4b496811 | ||
|
|
97f724951b | ||
|
|
c7a0d59e16 |
@@ -109,6 +109,9 @@ The class satisfies the following concept requirements:
|
|||||||
- **initializer_list_t** - type for initializer lists of `basic_json` values
|
- **initializer_list_t** - type for initializer lists of `basic_json` values
|
||||||
- [**input_format_t**](input_format_t.md) - type to choose the format to parse
|
- [**input_format_t**](input_format_t.md) - type to choose the format to parse
|
||||||
- [**json_sax_t**](../json_sax/index.md) - type for SAX events
|
- [**json_sax_t**](../json_sax/index.md) - type for SAX events
|
||||||
|
- [**with_object_t, with_array_t, with_string_t, with_boolean_t, with_integers_t, with_float_t, with_allocator_t,
|
||||||
|
with_json_serializer_t, with_binary_t, with_base_class_t**](with_t.md) - types to create a `basic_json` type with
|
||||||
|
one (or two) replaced template parameters
|
||||||
|
|
||||||
### Exceptions
|
### Exceptions
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,124 @@
|
|||||||
|
# <small>nlohmann::basic_json::</small>with_t
|
||||||
|
|
||||||
|
Member alias templates `with_object_t`, `with_array_t`, `with_string_t`, `with_boolean_t`, `with_integers_t`,
|
||||||
|
`with_float_t`, `with_allocator_t`, `with_json_serializer_t`, `with_binary_t`, and `with_base_class_t`.
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
template<template<typename, typename, typename...> class ObjectType2>
|
||||||
|
using with_object_t = basic_json<ObjectType2, ArrayType, StringType, BooleanType,
|
||||||
|
NumberIntegerType, NumberUnsignedType, NumberFloatType,
|
||||||
|
AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>;
|
||||||
|
|
||||||
|
template<template<typename, typename...> class ArrayType2>
|
||||||
|
using with_array_t = basic_json<ObjectType, ArrayType2, StringType, BooleanType,
|
||||||
|
NumberIntegerType, NumberUnsignedType, NumberFloatType,
|
||||||
|
AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>;
|
||||||
|
|
||||||
|
template<class StringType2>
|
||||||
|
using with_string_t = basic_json<ObjectType, ArrayType, StringType2, BooleanType,
|
||||||
|
NumberIntegerType, NumberUnsignedType, NumberFloatType,
|
||||||
|
AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>;
|
||||||
|
|
||||||
|
template<class BooleanType2>
|
||||||
|
using with_boolean_t = basic_json<ObjectType, ArrayType, StringType, BooleanType2,
|
||||||
|
NumberIntegerType, NumberUnsignedType, NumberFloatType,
|
||||||
|
AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>;
|
||||||
|
|
||||||
|
template<class NumberIntegerType2, class NumberUnsignedType2>
|
||||||
|
using with_integers_t = basic_json<ObjectType, ArrayType, StringType, BooleanType,
|
||||||
|
NumberIntegerType2, NumberUnsignedType2, NumberFloatType,
|
||||||
|
AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>;
|
||||||
|
|
||||||
|
template<class NumberFloatType2>
|
||||||
|
using with_float_t = basic_json<ObjectType, ArrayType, StringType, BooleanType,
|
||||||
|
NumberIntegerType, NumberUnsignedType, NumberFloatType2,
|
||||||
|
AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>;
|
||||||
|
|
||||||
|
template<template<typename> class AllocatorType2>
|
||||||
|
using with_allocator_t = basic_json<ObjectType, ArrayType, StringType, BooleanType,
|
||||||
|
NumberIntegerType, NumberUnsignedType, NumberFloatType,
|
||||||
|
AllocatorType2, JSONSerializer, BinaryType, CustomBaseClass>;
|
||||||
|
|
||||||
|
template<template<typename, typename = void> class JSONSerializer2>
|
||||||
|
using with_json_serializer_t = basic_json<ObjectType, ArrayType, StringType, BooleanType,
|
||||||
|
NumberIntegerType, NumberUnsignedType, NumberFloatType,
|
||||||
|
AllocatorType, JSONSerializer2, BinaryType, CustomBaseClass>;
|
||||||
|
|
||||||
|
template<class BinaryType2>
|
||||||
|
using with_binary_t = basic_json<ObjectType, ArrayType, StringType, BooleanType,
|
||||||
|
NumberIntegerType, NumberUnsignedType, NumberFloatType,
|
||||||
|
AllocatorType, JSONSerializer, BinaryType2, CustomBaseClass>;
|
||||||
|
|
||||||
|
template<class CustomBaseClass2>
|
||||||
|
using with_base_class_t = basic_json<ObjectType, ArrayType, StringType, BooleanType,
|
||||||
|
NumberIntegerType, NumberUnsignedType, NumberFloatType,
|
||||||
|
AllocatorType, JSONSerializer, BinaryType, CustomBaseClass2>;
|
||||||
|
```
|
||||||
|
|
||||||
|
These member alias templates make it easier to create a `basic_json` type that is identical to the current type except
|
||||||
|
for one (or, in the case of `with_integers_t`, two) of its [template parameters](index.md#template-parameters).
|
||||||
|
Spelling out all 11 template parameters of `basic_json` just to change a single one is verbose and error-prone; these
|
||||||
|
aliases only require the replacement type(s).
|
||||||
|
|
||||||
|
with_object_t<ObjectType2>
|
||||||
|
: replaces `ObjectType`
|
||||||
|
|
||||||
|
with_array_t<ArrayType2>
|
||||||
|
: replaces `ArrayType`
|
||||||
|
|
||||||
|
with_string_t<StringType2>
|
||||||
|
: replaces `StringType`
|
||||||
|
|
||||||
|
with_boolean_t<BooleanType2>
|
||||||
|
: replaces `BooleanType`
|
||||||
|
|
||||||
|
with_integers_t<NumberIntegerType2, NumberUnsignedType2>
|
||||||
|
: replaces both `NumberIntegerType` and `NumberUnsignedType`; the two are combined into a single alias because they
|
||||||
|
are usually changed together (for instance, when switching to fixed-width integer types)
|
||||||
|
|
||||||
|
with_float_t<NumberFloatType2>
|
||||||
|
: replaces `NumberFloatType`
|
||||||
|
|
||||||
|
with_allocator_t<AllocatorType2>
|
||||||
|
: replaces `AllocatorType`
|
||||||
|
|
||||||
|
with_json_serializer_t<JSONSerializer2>
|
||||||
|
: replaces `JSONSerializer`
|
||||||
|
|
||||||
|
with_binary_t<BinaryType2>
|
||||||
|
: replaces `BinaryType`
|
||||||
|
|
||||||
|
with_base_class_t<CustomBaseClass2>
|
||||||
|
: replaces `CustomBaseClass`; see also [`json_base_class_t`](json_base_class_t.md)
|
||||||
|
|
||||||
|
## Notes
|
||||||
|
|
||||||
|
All other template parameters are kept unchanged, so the resulting type still uses, for instance, the same
|
||||||
|
`ObjectType` unless `with_object_t` itself is used.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
??? example
|
||||||
|
|
||||||
|
The following code shows how `with_object_t` can be used to create a JSON type that stores object elements in a
|
||||||
|
`std::map` and therefore keeps them sorted by key, unlike the default type which preserves insertion order
|
||||||
|
only when `nlohmann::ordered_json` is used.
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
--8<-- "examples/with_t.cpp"
|
||||||
|
```
|
||||||
|
|
||||||
|
Output:
|
||||||
|
|
||||||
|
```json
|
||||||
|
--8<-- "examples/with_t.output"
|
||||||
|
```
|
||||||
|
|
||||||
|
## See also
|
||||||
|
|
||||||
|
- [basic_json](index.md#template-parameters) - the template parameters that can be replaced
|
||||||
|
- [json_base_class_t](json_base_class_t.md) - the type used for `CustomBaseClass`
|
||||||
|
|
||||||
|
## Version history
|
||||||
|
|
||||||
|
- Added in version 3.13.0.
|
||||||
@@ -13,19 +13,7 @@ class visitor_adaptor_with_metadata
|
|||||||
void do_visit(const Ptr& ptr, const Fnc& fnc) const;
|
void do_visit(const Ptr& ptr, const Fnc& fnc) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
using json = nlohmann::basic_json <
|
using json = nlohmann::json::with_base_class_t<visitor_adaptor_with_metadata>;
|
||||||
std::map,
|
|
||||||
std::vector,
|
|
||||||
std::string,
|
|
||||||
bool,
|
|
||||||
std::int64_t,
|
|
||||||
std::uint64_t,
|
|
||||||
double,
|
|
||||||
std::allocator,
|
|
||||||
nlohmann::adl_serializer,
|
|
||||||
std::vector<std::uint8_t>,
|
|
||||||
visitor_adaptor_with_metadata
|
|
||||||
>;
|
|
||||||
|
|
||||||
template <class Fnc>
|
template <class Fnc>
|
||||||
void visitor_adaptor_with_metadata::visit(const Fnc& fnc) const
|
void visitor_adaptor_with_metadata::visit(const Fnc& fnc) const
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <map>
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
|
// a JSON type that stores objects in a std::map (which keeps keys sorted)
|
||||||
|
// instead of the default ordered associative container
|
||||||
|
using sorted_json = nlohmann::json::with_object_t<std::map>;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
sorted_json j;
|
||||||
|
j["c"] = 1;
|
||||||
|
j["a"] = 2;
|
||||||
|
j["b"] = 3;
|
||||||
|
|
||||||
|
// keys are sorted, because std::map is used to store the object
|
||||||
|
std::cout << j.dump() << std::endl;
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
{"a":2,"b":3,"c":1}
|
||||||
@@ -232,6 +232,7 @@ nav:
|
|||||||
- 'update': api/basic_json/update.md
|
- 'update': api/basic_json/update.md
|
||||||
- 'value': api/basic_json/value.md
|
- 'value': api/basic_json/value.md
|
||||||
- 'value_t': api/basic_json/value_t.md
|
- 'value_t': api/basic_json/value_t.md
|
||||||
|
- 'with_t': api/basic_json/with_t.md
|
||||||
- byte_container_with_subtype:
|
- byte_container_with_subtype:
|
||||||
- 'Overview': api/byte_container_with_subtype/index.md
|
- 'Overview': api/byte_container_with_subtype/index.md
|
||||||
- '(constructor)': api/byte_container_with_subtype/byte_container_with_subtype.md
|
- '(constructor)': api/byte_container_with_subtype/byte_container_with_subtype.md
|
||||||
|
|||||||
@@ -226,6 +226,70 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
/// SAX interface type, see @ref nlohmann::json_sax
|
/// SAX interface type, see @ref nlohmann::json_sax
|
||||||
using json_sax_t = json_sax<basic_json>;
|
using json_sax_t = json_sax<basic_json>;
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// utility templates to create a json type with different template parameters //
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
/// Json type using a different type for storing objects
|
||||||
|
/// @sa https://json.nlohmann.me/api/basic_json/with_t/
|
||||||
|
template<template<typename, typename, typename...> class ObjectType2>
|
||||||
|
using with_object_t = basic_json<ObjectType2, ArrayType, StringType, BooleanType,
|
||||||
|
NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>;
|
||||||
|
|
||||||
|
/// Json type using a different type for storing arrays
|
||||||
|
/// @sa https://json.nlohmann.me/api/basic_json/with_t/
|
||||||
|
template<template<typename, typename...> class ArrayType2>
|
||||||
|
using with_array_t = basic_json<ObjectType, ArrayType2, StringType, BooleanType,
|
||||||
|
NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>;
|
||||||
|
|
||||||
|
/// Json type using a different type for storing strings
|
||||||
|
/// @sa https://json.nlohmann.me/api/basic_json/with_t/
|
||||||
|
template<class StringType2>
|
||||||
|
using with_string_t = basic_json<ObjectType, ArrayType, StringType2, BooleanType,
|
||||||
|
NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>;
|
||||||
|
|
||||||
|
/// Json type using a different type for storing booleans
|
||||||
|
/// @sa https://json.nlohmann.me/api/basic_json/with_t/
|
||||||
|
template<class BooleanType2>
|
||||||
|
using with_boolean_t = basic_json<ObjectType, ArrayType, StringType, BooleanType2,
|
||||||
|
NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>;
|
||||||
|
|
||||||
|
/// Json type using different types for storing signed and unsigned integers
|
||||||
|
/// @sa https://json.nlohmann.me/api/basic_json/with_t/
|
||||||
|
template<class NumberIntegerType2, class NumberUnsignedType2>
|
||||||
|
using with_integers_t = basic_json<ObjectType, ArrayType, StringType, BooleanType,
|
||||||
|
NumberIntegerType2, NumberUnsignedType2, NumberFloatType, AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>;
|
||||||
|
|
||||||
|
/// Json type using a different type for storing floating point numbers
|
||||||
|
/// @sa https://json.nlohmann.me/api/basic_json/with_t/
|
||||||
|
template<class NumberFloatType2>
|
||||||
|
using with_float_t = basic_json<ObjectType, ArrayType, StringType, BooleanType,
|
||||||
|
NumberIntegerType, NumberUnsignedType, NumberFloatType2, AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>;
|
||||||
|
|
||||||
|
/// Json type using a different type as base allocator
|
||||||
|
/// @sa https://json.nlohmann.me/api/basic_json/with_t/
|
||||||
|
template<template<typename> class AllocatorType2>
|
||||||
|
using with_allocator_t = basic_json<ObjectType, ArrayType, StringType, BooleanType,
|
||||||
|
NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType2, JSONSerializer, BinaryType, CustomBaseClass>;
|
||||||
|
|
||||||
|
/// Json type using a different type as json serializer
|
||||||
|
/// @sa https://json.nlohmann.me/api/basic_json/with_t/
|
||||||
|
template<template<typename, typename = void> class JSONSerializer2>
|
||||||
|
using with_json_serializer_t = basic_json<ObjectType, ArrayType, StringType, BooleanType,
|
||||||
|
NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer2, BinaryType, CustomBaseClass>;
|
||||||
|
|
||||||
|
/// Json type using a different type for storing binary data
|
||||||
|
/// @sa https://json.nlohmann.me/api/basic_json/with_t/
|
||||||
|
template<class BinaryType2>
|
||||||
|
using with_binary_t = basic_json<ObjectType, ArrayType, StringType, BooleanType,
|
||||||
|
NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType2, CustomBaseClass>;
|
||||||
|
|
||||||
|
/// Json type using a different type as base class
|
||||||
|
/// @sa https://json.nlohmann.me/api/basic_json/with_t/
|
||||||
|
template<class CustomBaseClass2>
|
||||||
|
using with_base_class_t = basic_json<ObjectType, ArrayType, StringType, BooleanType,
|
||||||
|
NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType, CustomBaseClass2>;
|
||||||
|
|
||||||
////////////////
|
////////////////
|
||||||
// exceptions //
|
// exceptions //
|
||||||
////////////////
|
////////////////
|
||||||
|
|||||||
@@ -27348,6 +27348,70 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
/// SAX interface type, see @ref nlohmann::json_sax
|
/// SAX interface type, see @ref nlohmann::json_sax
|
||||||
using json_sax_t = json_sax<basic_json>;
|
using json_sax_t = json_sax<basic_json>;
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// utility templates to create a json type with different template parameters //
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
/// Json type using a different type for storing objects
|
||||||
|
/// @sa https://json.nlohmann.me/api/basic_json/with_t/
|
||||||
|
template<template<typename, typename, typename...> class ObjectType2>
|
||||||
|
using with_object_t = basic_json<ObjectType2, ArrayType, StringType, BooleanType,
|
||||||
|
NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>;
|
||||||
|
|
||||||
|
/// Json type using a different type for storing arrays
|
||||||
|
/// @sa https://json.nlohmann.me/api/basic_json/with_t/
|
||||||
|
template<template<typename, typename...> class ArrayType2>
|
||||||
|
using with_array_t = basic_json<ObjectType, ArrayType2, StringType, BooleanType,
|
||||||
|
NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>;
|
||||||
|
|
||||||
|
/// Json type using a different type for storing strings
|
||||||
|
/// @sa https://json.nlohmann.me/api/basic_json/with_t/
|
||||||
|
template<class StringType2>
|
||||||
|
using with_string_t = basic_json<ObjectType, ArrayType, StringType2, BooleanType,
|
||||||
|
NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>;
|
||||||
|
|
||||||
|
/// Json type using a different type for storing booleans
|
||||||
|
/// @sa https://json.nlohmann.me/api/basic_json/with_t/
|
||||||
|
template<class BooleanType2>
|
||||||
|
using with_boolean_t = basic_json<ObjectType, ArrayType, StringType, BooleanType2,
|
||||||
|
NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>;
|
||||||
|
|
||||||
|
/// Json type using different types for storing signed and unsigned integers
|
||||||
|
/// @sa https://json.nlohmann.me/api/basic_json/with_t/
|
||||||
|
template<class NumberIntegerType2, class NumberUnsignedType2>
|
||||||
|
using with_integers_t = basic_json<ObjectType, ArrayType, StringType, BooleanType,
|
||||||
|
NumberIntegerType2, NumberUnsignedType2, NumberFloatType, AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>;
|
||||||
|
|
||||||
|
/// Json type using a different type for storing floating point numbers
|
||||||
|
/// @sa https://json.nlohmann.me/api/basic_json/with_t/
|
||||||
|
template<class NumberFloatType2>
|
||||||
|
using with_float_t = basic_json<ObjectType, ArrayType, StringType, BooleanType,
|
||||||
|
NumberIntegerType, NumberUnsignedType, NumberFloatType2, AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>;
|
||||||
|
|
||||||
|
/// Json type using a different type as base allocator
|
||||||
|
/// @sa https://json.nlohmann.me/api/basic_json/with_t/
|
||||||
|
template<template<typename> class AllocatorType2>
|
||||||
|
using with_allocator_t = basic_json<ObjectType, ArrayType, StringType, BooleanType,
|
||||||
|
NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType2, JSONSerializer, BinaryType, CustomBaseClass>;
|
||||||
|
|
||||||
|
/// Json type using a different type as json serializer
|
||||||
|
/// @sa https://json.nlohmann.me/api/basic_json/with_t/
|
||||||
|
template<template<typename, typename = void> class JSONSerializer2>
|
||||||
|
using with_json_serializer_t = basic_json<ObjectType, ArrayType, StringType, BooleanType,
|
||||||
|
NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer2, BinaryType, CustomBaseClass>;
|
||||||
|
|
||||||
|
/// Json type using a different type for storing binary data
|
||||||
|
/// @sa https://json.nlohmann.me/api/basic_json/with_t/
|
||||||
|
template<class BinaryType2>
|
||||||
|
using with_binary_t = basic_json<ObjectType, ArrayType, StringType, BooleanType,
|
||||||
|
NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType2, CustomBaseClass>;
|
||||||
|
|
||||||
|
/// Json type using a different type as base class
|
||||||
|
/// @sa https://json.nlohmann.me/api/basic_json/with_t/
|
||||||
|
template<class CustomBaseClass2>
|
||||||
|
using with_base_class_t = basic_json<ObjectType, ArrayType, StringType, BooleanType,
|
||||||
|
NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType, CustomBaseClass2>;
|
||||||
|
|
||||||
////////////////
|
////////////////
|
||||||
// exceptions //
|
// exceptions //
|
||||||
////////////////
|
////////////////
|
||||||
|
|||||||
@@ -48,14 +48,7 @@ TEST_CASE("bad_alloc")
|
|||||||
SECTION("bad_alloc")
|
SECTION("bad_alloc")
|
||||||
{
|
{
|
||||||
// create JSON type using the throwing allocator
|
// create JSON type using the throwing allocator
|
||||||
using bad_json = nlohmann::basic_json<std::map,
|
using bad_json = nlohmann::json::with_allocator_t<bad_allocator>;
|
||||||
std::vector,
|
|
||||||
std::string,
|
|
||||||
bool,
|
|
||||||
std::int64_t,
|
|
||||||
std::uint64_t,
|
|
||||||
double,
|
|
||||||
bad_allocator>;
|
|
||||||
|
|
||||||
// creating an object should throw
|
// creating an object should throw
|
||||||
CHECK_THROWS_AS(bad_json(bad_json::value_t::object), std::bad_alloc&);
|
CHECK_THROWS_AS(bad_json(bad_json::value_t::object), std::bad_alloc&);
|
||||||
@@ -129,14 +122,7 @@ void my_allocator_clean_up(T* p)
|
|||||||
TEST_CASE("controlled bad_alloc")
|
TEST_CASE("controlled bad_alloc")
|
||||||
{
|
{
|
||||||
// create JSON type using the throwing allocator
|
// create JSON type using the throwing allocator
|
||||||
using my_json = nlohmann::basic_json<std::map,
|
using my_json = nlohmann::json::with_allocator_t<my_allocator>;
|
||||||
std::vector,
|
|
||||||
std::string,
|
|
||||||
bool,
|
|
||||||
std::int64_t,
|
|
||||||
std::uint64_t,
|
|
||||||
double,
|
|
||||||
my_allocator>;
|
|
||||||
|
|
||||||
SECTION("class json_value")
|
SECTION("class json_value")
|
||||||
{
|
{
|
||||||
@@ -588,14 +574,7 @@ TEST_CASE("bad my_allocator::construct")
|
|||||||
{
|
{
|
||||||
SECTION("my_allocator::construct doesn't forward")
|
SECTION("my_allocator::construct doesn't forward")
|
||||||
{
|
{
|
||||||
using bad_alloc_json = nlohmann::basic_json<std::map,
|
using bad_alloc_json = nlohmann::json::with_allocator_t<allocator_no_forward>;
|
||||||
std::vector,
|
|
||||||
std::string,
|
|
||||||
bool,
|
|
||||||
std::int64_t,
|
|
||||||
std::uint64_t,
|
|
||||||
double,
|
|
||||||
allocator_no_forward>;
|
|
||||||
|
|
||||||
bad_alloc_json j;
|
bad_alloc_json j;
|
||||||
j["test"] = bad_alloc_json::array_t();
|
j["test"] = bad_alloc_json::array_t();
|
||||||
|
|||||||
@@ -163,16 +163,7 @@ void int_to_string(alt_string& target, std::size_t value)
|
|||||||
target = std::to_string(value).c_str();
|
target = std::to_string(value).c_str();
|
||||||
}
|
}
|
||||||
|
|
||||||
using alt_json = nlohmann::basic_json <
|
using alt_json = nlohmann::json::with_string_t<alt_string>;
|
||||||
std::map,
|
|
||||||
std::vector,
|
|
||||||
alt_string,
|
|
||||||
bool,
|
|
||||||
std::int64_t,
|
|
||||||
std::uint64_t,
|
|
||||||
double,
|
|
||||||
std::allocator,
|
|
||||||
nlohmann::adl_serializer >;
|
|
||||||
|
|
||||||
bool operator<(const char* op1, const alt_string& op2) noexcept
|
bool operator<(const char* op1, const alt_string& op2) noexcept
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -38,20 +38,7 @@ class json_metadata
|
|||||||
};
|
};
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
using json_with_metadata =
|
using json_with_metadata = nlohmann::json::with_base_class_t<json_metadata<T>>;
|
||||||
nlohmann::basic_json <
|
|
||||||
std::map,
|
|
||||||
std::vector,
|
|
||||||
std::string,
|
|
||||||
bool,
|
|
||||||
std::int64_t,
|
|
||||||
std::uint64_t,
|
|
||||||
double,
|
|
||||||
std::allocator,
|
|
||||||
nlohmann::adl_serializer,
|
|
||||||
std::vector<std::uint8_t>,
|
|
||||||
json_metadata<T>
|
|
||||||
>;
|
|
||||||
|
|
||||||
TEST_CASE("JSON Node Metadata")
|
TEST_CASE("JSON Node Metadata")
|
||||||
{
|
{
|
||||||
@@ -268,19 +255,7 @@ class visitor_adaptor
|
|||||||
void do_visit(const Ptr& ptr, const Fnc& fnc) const;
|
void do_visit(const Ptr& ptr, const Fnc& fnc) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
using json_with_visitor_t = nlohmann::basic_json <
|
using json_with_visitor_t = nlohmann::json::with_base_class_t<visitor_adaptor>;
|
||||||
std::map,
|
|
||||||
std::vector,
|
|
||||||
std::string,
|
|
||||||
bool,
|
|
||||||
std::int64_t,
|
|
||||||
std::uint64_t,
|
|
||||||
double,
|
|
||||||
std::allocator,
|
|
||||||
nlohmann::adl_serializer,
|
|
||||||
std::vector<std::uint8_t>,
|
|
||||||
visitor_adaptor
|
|
||||||
>;
|
|
||||||
|
|
||||||
template <class Fnc>
|
template <class Fnc>
|
||||||
void visitor_adaptor::visit(const Fnc& fnc) const
|
void visitor_adaptor::visit(const Fnc& fnc) const
|
||||||
|
|||||||
+65
-3
@@ -23,6 +23,7 @@ using nlohmann::json;
|
|||||||
using namespace nlohmann::literals; // NOLINT(google-build-using-namespace)
|
using namespace nlohmann::literals; // NOLINT(google-build-using-namespace)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <deque>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
@@ -684,8 +685,7 @@ static std::ostream& operator<<(std::ostream& os, small_pod l)
|
|||||||
TEST_CASE("custom serializer for pods" * doctest::test_suite("udt"))
|
TEST_CASE("custom serializer for pods" * doctest::test_suite("udt"))
|
||||||
{
|
{
|
||||||
using custom_json =
|
using custom_json =
|
||||||
nlohmann::basic_json<std::map, std::vector, std::string, bool,
|
nlohmann::json::with_json_serializer_t<pod_serializer>;
|
||||||
std::int64_t, std::uint64_t, double, std::allocator, pod_serializer>;
|
|
||||||
|
|
||||||
auto p = udt::small_pod{42, '/', 42};
|
auto p = udt::small_pod{42, '/', 42};
|
||||||
custom_json const j = p;
|
custom_json const j = p;
|
||||||
@@ -703,7 +703,7 @@ TEST_CASE("custom serializer for pods" * doctest::test_suite("udt"))
|
|||||||
template <typename T, typename>
|
template <typename T, typename>
|
||||||
struct another_adl_serializer;
|
struct another_adl_serializer;
|
||||||
|
|
||||||
using custom_json = nlohmann::basic_json<std::map, std::vector, std::string, bool, std::int64_t, std::uint64_t, double, std::allocator, another_adl_serializer>;
|
using custom_json = nlohmann::json::with_json_serializer_t<another_adl_serializer>;
|
||||||
|
|
||||||
template <typename T, typename>
|
template <typename T, typename>
|
||||||
struct another_adl_serializer
|
struct another_adl_serializer
|
||||||
@@ -734,6 +734,68 @@ TEST_CASE("custom serializer that does adl by default" * doctest::test_suite("ud
|
|||||||
CHECK(me == cj.get<udt::person>());
|
CHECK(me == cj.get<udt::person>());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("with_*_t aliases" * doctest::test_suite("udt"))
|
||||||
|
{
|
||||||
|
// a custom base class used to check with_base_class_t
|
||||||
|
struct custom_base_class {};
|
||||||
|
|
||||||
|
CHECK(std::is_same<json::with_object_t<std::deque>,
|
||||||
|
nlohmann::basic_json<std::deque, std::vector, std::string, bool,
|
||||||
|
std::int64_t, std::uint64_t, double, std::allocator,
|
||||||
|
nlohmann::adl_serializer, std::vector<std::uint8_t>>>::value);
|
||||||
|
|
||||||
|
CHECK(std::is_same<json::with_array_t<std::deque>,
|
||||||
|
nlohmann::basic_json<std::map, std::deque, std::string, bool,
|
||||||
|
std::int64_t, std::uint64_t, double, std::allocator,
|
||||||
|
nlohmann::adl_serializer, std::vector<std::uint8_t>>>::value);
|
||||||
|
|
||||||
|
CHECK(std::is_same<json::with_string_t<std::wstring>,
|
||||||
|
nlohmann::basic_json<std::map, std::vector, std::wstring, bool,
|
||||||
|
std::int64_t, std::uint64_t, double, std::allocator,
|
||||||
|
nlohmann::adl_serializer, std::vector<std::uint8_t>>>::value);
|
||||||
|
|
||||||
|
CHECK(std::is_same<json::with_boolean_t<int>,
|
||||||
|
nlohmann::basic_json<std::map, std::vector, std::string, int,
|
||||||
|
std::int64_t, std::uint64_t, double, std::allocator,
|
||||||
|
nlohmann::adl_serializer, std::vector<std::uint8_t>>>::value);
|
||||||
|
|
||||||
|
CHECK(std::is_same<json::with_integers_t<std::int32_t, std::uint32_t>,
|
||||||
|
nlohmann::basic_json<std::map, std::vector, std::string, bool,
|
||||||
|
std::int32_t, std::uint32_t, double, std::allocator,
|
||||||
|
nlohmann::adl_serializer, std::vector<std::uint8_t>>>::value);
|
||||||
|
|
||||||
|
CHECK(std::is_same<json::with_float_t<float>,
|
||||||
|
nlohmann::basic_json<std::map, std::vector, std::string, bool,
|
||||||
|
std::int64_t, std::uint64_t, float, std::allocator,
|
||||||
|
nlohmann::adl_serializer, std::vector<std::uint8_t>>>::value);
|
||||||
|
|
||||||
|
CHECK(std::is_same<json::with_allocator_t<std::allocator>,
|
||||||
|
nlohmann::basic_json<std::map, std::vector, std::string, bool,
|
||||||
|
std::int64_t, std::uint64_t, double, std::allocator,
|
||||||
|
nlohmann::adl_serializer, std::vector<std::uint8_t>>>::value);
|
||||||
|
|
||||||
|
CHECK(std::is_same<json::with_json_serializer_t<nlohmann::adl_serializer>,
|
||||||
|
nlohmann::basic_json<std::map, std::vector, std::string, bool,
|
||||||
|
std::int64_t, std::uint64_t, double, std::allocator,
|
||||||
|
nlohmann::adl_serializer, std::vector<std::uint8_t>>>::value);
|
||||||
|
|
||||||
|
CHECK(std::is_same<json::with_binary_t<std::vector<char>>,
|
||||||
|
nlohmann::basic_json<std::map, std::vector, std::string, bool,
|
||||||
|
std::int64_t, std::uint64_t, double, std::allocator,
|
||||||
|
nlohmann::adl_serializer, std::vector<char>>>::value);
|
||||||
|
|
||||||
|
CHECK(std::is_same<json::with_base_class_t<custom_base_class>,
|
||||||
|
nlohmann::basic_json<std::map, std::vector, std::string, bool,
|
||||||
|
std::int64_t, std::uint64_t, double, std::allocator,
|
||||||
|
nlohmann::adl_serializer, std::vector<std::uint8_t>, custom_base_class>>::value);
|
||||||
|
|
||||||
|
// with_string_t on ordered_json must keep ordered_map as the object type
|
||||||
|
CHECK(std::is_same<nlohmann::ordered_json::with_string_t<std::wstring>,
|
||||||
|
nlohmann::basic_json<nlohmann::ordered_map, std::vector, std::wstring, bool,
|
||||||
|
std::int64_t, std::uint64_t, double, std::allocator,
|
||||||
|
nlohmann::adl_serializer, std::vector<std::uint8_t>>>::value);
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE("different basic_json types conversions")
|
TEST_CASE("different basic_json types conversions")
|
||||||
{
|
{
|
||||||
SECTION("null")
|
SECTION("null")
|
||||||
|
|||||||
Reference in New Issue
Block a user