From 120d1d77d4371db55dffefa27c1ec16fd648e40a Mon Sep 17 00:00:00 2001
From: Julian Becker <becker.julian@gmail.com>
Date: Sat, 15 Sep 2018 13:40:20 +0200
Subject: [PATCH] BSON: test case for a more complex document

---
 .../nlohmann/detail/output/binary_writer.hpp  |  2 +-
 single_include/nlohmann/json.hpp              |  2 +-
 test/src/unit-bson.cpp                        | 32 +++++++++++++++++++
 3 files changed, 34 insertions(+), 2 deletions(-)

diff --git a/include/nlohmann/detail/output/binary_writer.hpp b/include/nlohmann/detail/output/binary_writer.hpp
index 1ec1d794..aa4f34fe 100644
--- a/include/nlohmann/detail/output/binary_writer.hpp
+++ b/include/nlohmann/detail/output/binary_writer.hpp
@@ -792,7 +792,7 @@ class binary_writer
         switch (j.type())
         {
             default:
-                JSON_THROW(type_error::create(317, "JSON value cannot be serialized to requested format"));
+                JSON_THROW(type_error::create(317, "JSON value of type be serialized to requested format: " + std::to_string((int)j.type())));
                 break;
             case value_t::object:
                 return write_bson_object_internal(name, j);
diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp
index 6201f047..1c966bc3 100644
--- a/single_include/nlohmann/json.hpp
+++ b/single_include/nlohmann/json.hpp
@@ -8633,7 +8633,7 @@ class binary_writer
         switch (j.type())
         {
             default:
-                JSON_THROW(type_error::create(317, "JSON value cannot be serialized to requested format"));
+                JSON_THROW(type_error::create(317, "JSON value of type be serialized to requested format: " + std::to_string((int)j.type())));
                 break;
             case value_t::object:
                 return write_bson_object_internal(name, j);
diff --git a/test/src/unit-bson.cpp b/test/src/unit-bson.cpp
index bdc6fe74..c016466d 100644
--- a/test/src/unit-bson.cpp
+++ b/test/src/unit-bson.cpp
@@ -380,5 +380,37 @@ TEST_CASE("BSON")
             CHECK(json::from_bson(result) == j);
             CHECK(json::from_bson(result, true, false) == j);
         }
+
+        SECTION("Some more complex document")
+        {
+            // directly encoding uint64 is not supported in bson (only for timestamp values)
+            json j =
+            {
+                {"double", 42.5},
+                {"entry", 4.2},
+                {"number", 12345},
+                {"object", {{ "string", "value" }}}
+            };
+
+            std::vector<uint8_t> expected =
+            {
+                /*size */ 0x4f, 0x00, 0x00, 0x00,
+                /*entry*/ 0x01, 'd',  'o',  'u',  'b',  'l',  'e',  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x45, 0x40,
+                /*entry*/ 0x01, 'e',  'n',  't',  'r',  'y',  0x00, 0xcd, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0x10, 0x40,
+                /*entry*/ 0x10, 'n',  'u',  'm',  'b',  'e',  'r',  0x00, 0x39, 0x30, 0x00, 0x00,
+                /*entry*/ 0x03, 'o',  'b',  'j',  'e',  'c',  't',  0x00,
+                /*entry: obj-size */ 0x17, 0x00, 0x00, 0x00,
+                /*entry: obj-entry*/0x02, 's',  't',  'r',  'i',  'n',  'g', 0x00, 0x06, 0x00, 0x00, 0x00, 'v', 'a', 'l', 'u', 'e', 0,
+                /*entry: obj-term.*/0x00,
+                /*obj-term*/ 0x00
+            };
+
+            const auto result = json::to_bson(j);
+            CHECK(result == expected);
+
+            // roundtrip
+            CHECK(json::from_bson(result) == j);
+            CHECK(json::from_bson(result, true, false) == j);
+        }
     }
 }