From 421a08439678e1d8fcb21091d45f2608ac03b428 Mon Sep 17 00:00:00 2001
From: Niels Lohmann <mail@nlohmann.me>
Date: Wed, 6 May 2020 21:23:45 +0200
Subject: [PATCH] :sparkles: add convenience function to create binary value
 with given subtype

---
 include/nlohmann/json.hpp        | 54 +++++++++++++++++++++++++++++---
 single_include/nlohmann/json.hpp | 54 +++++++++++++++++++++++++++++---
 2 files changed, 100 insertions(+), 8 deletions(-)

diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp
index f85e8e47..792263e2 100644
--- a/include/nlohmann/json.hpp
+++ b/include/nlohmann/json.hpp
@@ -889,9 +889,25 @@ class basic_json
     struct internal_binary_t : public BinaryType
     {
         using BinaryType::BinaryType;
-        internal_binary_t() noexcept(noexcept(BinaryType())) : BinaryType() {}
-        internal_binary_t(BinaryType const& bint) noexcept(noexcept(BinaryType(bint))) : BinaryType(bint) {}
-        internal_binary_t(BinaryType&& bint)  noexcept(noexcept(BinaryType(std::move(bint)))) : BinaryType(std::move(bint)) {}
+        internal_binary_t() noexcept(noexcept(BinaryType()))
+            : BinaryType()
+        {}
+        internal_binary_t(const BinaryType& bint) noexcept(noexcept(BinaryType(bint)))
+            : BinaryType(bint)
+        {}
+        internal_binary_t(BinaryType&& bint) noexcept(noexcept(BinaryType(std::move(bint))))
+            : BinaryType(std::move(bint))
+        {}
+        internal_binary_t(const BinaryType& bint, std::uint8_t st) noexcept(noexcept(BinaryType(bint)))
+            : BinaryType(bint)
+            , subtype(st)
+            , has_subtype(true)
+        {}
+        internal_binary_t(BinaryType&& bint, std::uint8_t st) noexcept(noexcept(BinaryType(std::move(bint))))
+            : BinaryType(std::move(bint))
+            , subtype(st)
+            , has_subtype(true)
+        {}
 
         // TOOD: If minimum C++ version is ever bumped to C++17, this field
         // deserves to be a std::optional
@@ -1098,6 +1114,18 @@ class basic_json
             binary = create<internal_binary_t>(std::move(value));
         }
 
+        /// constructor for binary arrays (internal type)
+        json_value(const internal_binary_t& value)
+        {
+            binary = create<internal_binary_t>(value);
+        }
+
+        /// constructor for rvalue binary arrays (internal type)
+        json_value(internal_binary_t&& value)
+        {
+            binary = create<internal_binary_t>(std::move(value));
+        }
+
         void destroy(value_t t) noexcept
         {
             // flatten the current json_value to a heap-allocated stack
@@ -1655,7 +1683,7 @@ class basic_json
     @since version 3.8.0
     */
     JSON_HEDLEY_WARN_UNUSED_RESULT
-    static basic_json binary_array(binary_t const& init)
+    static basic_json binary_array(const binary_t& init)
     {
         auto res = basic_json();
         res.m_type = value_t::binary;
@@ -1663,6 +1691,15 @@ class basic_json
         return res;
     }
 
+    JSON_HEDLEY_WARN_UNUSED_RESULT
+    static basic_json binary_array(const binary_t& init, std::uint8_t subtype)
+    {
+        auto res = basic_json();
+        res.m_type = value_t::binary;
+        res.m_value = internal_binary_t(init, subtype);
+        return res;
+    }
+
     /*!
     @brief explicitly create a binary array from an already constructed rvalue
     copy of its base type
@@ -1699,6 +1736,15 @@ class basic_json
         return res;
     }
 
+    JSON_HEDLEY_WARN_UNUSED_RESULT
+    static basic_json binary_array(binary_t&& init, std::uint8_t subtype)
+    {
+        auto res = basic_json();
+        res.m_type = value_t::binary;
+        res.m_value = internal_binary_t(std::move(init), subtype);
+        return res;
+    }
+
     /*!
     @brief explicitly create an array from an initializer list
 
diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp
index a1bf8c8b..f8986c1d 100644
--- a/single_include/nlohmann/json.hpp
+++ b/single_include/nlohmann/json.hpp
@@ -16396,9 +16396,25 @@ class basic_json
     struct internal_binary_t : public BinaryType
     {
         using BinaryType::BinaryType;
-        internal_binary_t() noexcept(noexcept(BinaryType())) : BinaryType() {}
-        internal_binary_t(BinaryType const& bint) noexcept(noexcept(BinaryType(bint))) : BinaryType(bint) {}
-        internal_binary_t(BinaryType&& bint)  noexcept(noexcept(BinaryType(std::move(bint)))) : BinaryType(std::move(bint)) {}
+        internal_binary_t() noexcept(noexcept(BinaryType()))
+            : BinaryType()
+        {}
+        internal_binary_t(const BinaryType& bint) noexcept(noexcept(BinaryType(bint)))
+            : BinaryType(bint)
+        {}
+        internal_binary_t(BinaryType&& bint) noexcept(noexcept(BinaryType(std::move(bint))))
+            : BinaryType(std::move(bint))
+        {}
+        internal_binary_t(const BinaryType& bint, std::uint8_t st) noexcept(noexcept(BinaryType(bint)))
+            : BinaryType(bint)
+            , subtype(st)
+            , has_subtype(true)
+        {}
+        internal_binary_t(BinaryType&& bint, std::uint8_t st) noexcept(noexcept(BinaryType(std::move(bint))))
+            : BinaryType(std::move(bint))
+            , subtype(st)
+            , has_subtype(true)
+        {}
 
         // TOOD: If minimum C++ version is ever bumped to C++17, this field
         // deserves to be a std::optional
@@ -16605,6 +16621,18 @@ class basic_json
             binary = create<internal_binary_t>(std::move(value));
         }
 
+        /// constructor for binary arrays (internal type)
+        json_value(const internal_binary_t& value)
+        {
+            binary = create<internal_binary_t>(value);
+        }
+
+        /// constructor for rvalue binary arrays (internal type)
+        json_value(internal_binary_t&& value)
+        {
+            binary = create<internal_binary_t>(std::move(value));
+        }
+
         void destroy(value_t t) noexcept
         {
             // flatten the current json_value to a heap-allocated stack
@@ -17162,7 +17190,7 @@ class basic_json
     @since version 3.8.0
     */
     JSON_HEDLEY_WARN_UNUSED_RESULT
-    static basic_json binary_array(binary_t const& init)
+    static basic_json binary_array(const binary_t& init)
     {
         auto res = basic_json();
         res.m_type = value_t::binary;
@@ -17170,6 +17198,15 @@ class basic_json
         return res;
     }
 
+    JSON_HEDLEY_WARN_UNUSED_RESULT
+    static basic_json binary_array(const binary_t& init, std::uint8_t subtype)
+    {
+        auto res = basic_json();
+        res.m_type = value_t::binary;
+        res.m_value = internal_binary_t(init, subtype);
+        return res;
+    }
+
     /*!
     @brief explicitly create a binary array from an already constructed rvalue
     copy of its base type
@@ -17206,6 +17243,15 @@ class basic_json
         return res;
     }
 
+    JSON_HEDLEY_WARN_UNUSED_RESULT
+    static basic_json binary_array(binary_t&& init, std::uint8_t subtype)
+    {
+        auto res = basic_json();
+        res.m_type = value_t::binary;
+        res.m_value = internal_binary_t(std::move(init), subtype);
+        return res;
+    }
+
     /*!
     @brief explicitly create an array from an initializer list