Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
J
json
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
json
Commits
0b345b20
Unverified
Commit
0b345b20
authored
Sep 12, 2021
by
Niels Lohmann
Committed by
GitHub
Sep 12, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow allocators for output_vector_adapter (#2989)
*
♻
allow allocators for vectors *
✅
add regression tests
parent
58b83b71
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
28 additions
and
10 deletions
+28
-10
include/nlohmann/detail/output/output_adapters.hpp
include/nlohmann/detail/output/output_adapters.hpp
+6
-5
single_include/nlohmann/json.hpp
single_include/nlohmann/json.hpp
+6
-5
test/src/unit-regression2.cpp
test/src/unit-regression2.cpp
+16
-0
No files found.
include/nlohmann/detail/output/output_adapters.hpp
View file @
0b345b20
...
@@ -37,11 +37,11 @@ template<typename CharType>
...
@@ -37,11 +37,11 @@ template<typename CharType>
using
output_adapter_t
=
std
::
shared_ptr
<
output_adapter_protocol
<
CharType
>>
;
using
output_adapter_t
=
std
::
shared_ptr
<
output_adapter_protocol
<
CharType
>>
;
/// output adapter for byte vectors
/// output adapter for byte vectors
template
<
typename
CharType
>
template
<
typename
CharType
,
typename
AllocatorType
=
std
::
allocator
<
CharType
>
>
class
output_vector_adapter
:
public
output_adapter_protocol
<
CharType
>
class
output_vector_adapter
:
public
output_adapter_protocol
<
CharType
>
{
{
public:
public:
explicit
output_vector_adapter
(
std
::
vector
<
CharType
>&
vec
)
noexcept
explicit
output_vector_adapter
(
std
::
vector
<
CharType
,
AllocatorType
>&
vec
)
noexcept
:
v
(
vec
)
:
v
(
vec
)
{}
{}
...
@@ -57,7 +57,7 @@ class output_vector_adapter : public output_adapter_protocol<CharType>
...
@@ -57,7 +57,7 @@ class output_vector_adapter : public output_adapter_protocol<CharType>
}
}
private:
private:
std
::
vector
<
CharType
>&
v
;
std
::
vector
<
CharType
,
AllocatorType
>&
v
;
};
};
#ifndef JSON_NO_IO
#ifndef JSON_NO_IO
...
@@ -114,8 +114,9 @@ template<typename CharType, typename StringType = std::basic_string<CharType>>
...
@@ -114,8 +114,9 @@ template<typename CharType, typename StringType = std::basic_string<CharType>>
class
output_adapter
class
output_adapter
{
{
public:
public:
output_adapter
(
std
::
vector
<
CharType
>&
vec
)
template
<
typename
AllocatorType
=
std
::
allocator
<
CharType
>
>
:
oa
(
std
::
make_shared
<
output_vector_adapter
<
CharType
>>
(
vec
))
{}
output_adapter
(
std
::
vector
<
CharType
,
AllocatorType
>&
vec
)
:
oa
(
std
::
make_shared
<
output_vector_adapter
<
CharType
,
AllocatorType
>>
(
vec
))
{}
#ifndef JSON_NO_IO
#ifndef JSON_NO_IO
output_adapter
(
std
::
basic_ostream
<
CharType
>&
s
)
output_adapter
(
std
::
basic_ostream
<
CharType
>&
s
)
...
...
single_include/nlohmann/json.hpp
View file @
0b345b20
...
@@ -13485,11 +13485,11 @@ template<typename CharType>
...
@@ -13485,11 +13485,11 @@ template<typename CharType>
using
output_adapter_t
=
std
::
shared_ptr
<
output_adapter_protocol
<
CharType
>>
;
using
output_adapter_t
=
std
::
shared_ptr
<
output_adapter_protocol
<
CharType
>>
;
/// output adapter for byte vectors
/// output adapter for byte vectors
template<typename CharType>
template
<
typename
CharType
,
typename
AllocatorType
=
std
::
allocator
<
CharType
>
>
class
output_vector_adapter
:
public
output_adapter_protocol
<
CharType
>
class
output_vector_adapter
:
public
output_adapter_protocol
<
CharType
>
{
{
public:
public:
explicit output_vector_adapter(std::vector<CharType>& vec) noexcept
explicit
output_vector_adapter
(
std
::
vector
<
CharType
,
AllocatorType
>&
vec
)
noexcept
:
v
(
vec
)
:
v
(
vec
)
{}
{}
...
@@ -13505,7 +13505,7 @@ class output_vector_adapter : public output_adapter_protocol<CharType>
...
@@ -13505,7 +13505,7 @@ class output_vector_adapter : public output_adapter_protocol<CharType>
}
}
private:
private:
std::vector<CharType>& v;
std
::
vector
<
CharType
,
AllocatorType
>&
v
;
};
};
#ifndef JSON_NO_IO
#ifndef JSON_NO_IO
...
@@ -13562,8 +13562,9 @@ template<typename CharType, typename StringType = std::basic_string<CharType>>
...
@@ -13562,8 +13562,9 @@ template<typename CharType, typename StringType = std::basic_string<CharType>>
class
output_adapter
class
output_adapter
{
{
public:
public:
output_adapter(std::vector<CharType>& vec)
template
<
typename
AllocatorType
=
std
::
allocator
<
CharType
>
>
: oa(std::make_shared<output_vector_adapter<CharType>>(vec)) {}
output_adapter
(
std
::
vector
<
CharType
,
AllocatorType
>&
vec
)
:
oa
(
std
::
make_shared
<
output_vector_adapter
<
CharType
,
AllocatorType
>>
(
vec
))
{}
#ifndef JSON_NO_IO
#ifndef JSON_NO_IO
output_adapter
(
std
::
basic_ostream
<
CharType
>&
s
)
output_adapter
(
std
::
basic_ostream
<
CharType
>&
s
)
...
...
test/src/unit-regression2.cpp
View file @
0b345b20
...
@@ -181,6 +181,13 @@ class sax_no_exception : public nlohmann::detail::json_sax_dom_parser<json>
...
@@ -181,6 +181,13 @@ class sax_no_exception : public nlohmann::detail::json_sax_dom_parser<json>
std
::
string
*
sax_no_exception
::
error_string
=
nullptr
;
std
::
string
*
sax_no_exception
::
error_string
=
nullptr
;
/////////////////////////////////////////////////////////////////////
// for #2982
/////////////////////////////////////////////////////////////////////
template
<
class
T
>
class
my_allocator
:
public
std
::
allocator
<
T
>
{};
TEST_CASE
(
"regression tests 2"
)
TEST_CASE
(
"regression tests 2"
)
{
{
...
@@ -679,6 +686,15 @@ TEST_CASE("regression tests 2")
...
@@ -679,6 +686,15 @@ TEST_CASE("regression tests 2")
test3
[
json
::
json_pointer
(
p
)]
=
json
::
object
();
test3
[
json
::
json_pointer
(
p
)]
=
json
::
object
();
CHECK
(
test3
.
dump
()
==
"{
\"
/root
\"
:{}}"
);
CHECK
(
test3
.
dump
()
==
"{
\"
/root
\"
:{}}"
);
}
}
SECTION
(
"issue #2982 - to_{binary format} does not provide a mechanism for specifying a custom allocator for the returned type"
)
{
std
::
vector
<
std
::
uint8_t
,
my_allocator
<
std
::
uint8_t
>>
my_vector
;
json
j
=
{
1
,
2
,
3
,
4
};
json
::
to_cbor
(
j
,
my_vector
);
json
k
=
json
::
from_cbor
(
my_vector
);
CHECK
(
j
==
k
);
}
}
}
DOCTEST_CLANG_SUPPRESS_WARNING_POP
DOCTEST_CLANG_SUPPRESS_WARNING_POP
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment