Răsfoiți Sursa

Added support `uint8_t` for `serializeMsgPack()` (closes #1142)

Benoit Blanchon 6 ani în urmă
părinte
comite
8bf6f6e09f

+ 1 - 0
CHANGELOG.md

@@ -5,6 +5,7 @@ HEAD
 ----
 
 * Added `BasicJsonDocument::shrinkToFit()`
+* Added support of `uint8_t` for `serializeJson()`, `serializeJsonPretty()`, and `serializeMsgPack()` (issue #1142)
 
 v6.13.0 (2019-11-01)
 -------

+ 64 - 0
extras/tests/Misc/unsigned_char.cpp

@@ -28,6 +28,70 @@ TEST_CASE("unsigned char[]") {
     REQUIRE(err == DeserializationError::Ok);
   }
 
+  SECTION("serializeMsgPack(unsigned char[])") {
+    unsigned char buffer[32];
+    StaticJsonDocument<JSON_OBJECT_SIZE(2)> doc;
+    doc["hello"] = "world";
+
+    size_t n = serializeMsgPack(doc, buffer);
+
+    REQUIRE(n == 13);
+    REQUIRE(memcmp(buffer, "\x81\xA5hello\xA5world", 13) == 0);
+  }
+
+  SECTION("serializeMsgPack(unsigned char*)") {
+    unsigned char buffer[32];
+    StaticJsonDocument<JSON_OBJECT_SIZE(2)> doc;
+    doc["hello"] = "world";
+
+    size_t n = serializeMsgPack(doc, buffer, sizeof(buffer));
+
+    REQUIRE(n == 13);
+    REQUIRE(memcmp(buffer, "\x81\xA5hello\xA5world", 13) == 0);
+  }
+
+  SECTION("serializeJson(unsigned char[])") {
+    unsigned char buffer[32];
+    StaticJsonDocument<JSON_OBJECT_SIZE(2)> doc;
+    doc["hello"] = "world";
+
+    size_t n = serializeJson(doc, buffer);
+
+    REQUIRE(n == 17);
+    REQUIRE(memcmp(buffer, "{\"hello\":\"world\"}", n) == 0);
+  }
+
+  SECTION("serializeJson(unsigned char*)") {
+    unsigned char buffer[32];
+    StaticJsonDocument<JSON_OBJECT_SIZE(2)> doc;
+    doc["hello"] = "world";
+
+    size_t n = serializeJson(doc, buffer, sizeof(buffer));
+
+    REQUIRE(n == 17);
+    REQUIRE(memcmp(buffer, "{\"hello\":\"world\"}", n) == 0);
+  }
+
+  SECTION("serializeJsonPretty(unsigned char[])") {
+    unsigned char buffer[32];
+    StaticJsonDocument<JSON_OBJECT_SIZE(2)> doc;
+    doc["hello"] = "world";
+
+    size_t n = serializeJsonPretty(doc, buffer);
+
+    REQUIRE(n == 24);
+  }
+
+  SECTION("serializeJsonPretty(unsigned char*)") {
+    unsigned char buffer[32];
+    StaticJsonDocument<JSON_OBJECT_SIZE(2)> doc;
+    doc["hello"] = "world";
+
+    size_t n = serializeJsonPretty(doc, buffer, sizeof(buffer));
+
+    REQUIRE(n == 24);
+  }
+
   SECTION("JsonVariant") {
     DynamicJsonDocument doc(4096);
 

+ 1 - 1
src/ArduinoJson/Json/JsonSerializer.hpp

@@ -103,7 +103,7 @@ size_t serializeJson(const TSource &source, TDestination &destination) {
 }
 
 template <typename TSource>
-size_t serializeJson(const TSource &source, char *buffer, size_t bufferSize) {
+size_t serializeJson(const TSource &source, void *buffer, size_t bufferSize) {
   return serialize<JsonSerializer>(source, buffer, bufferSize);
 }
 

+ 1 - 1
src/ArduinoJson/Json/PrettyJsonSerializer.hpp

@@ -70,7 +70,7 @@ size_t serializeJsonPretty(const TSource &source, TDestination &destination) {
 }
 
 template <typename TSource>
-size_t serializeJsonPretty(const TSource &source, char *buffer,
+size_t serializeJsonPretty(const TSource &source, void *buffer,
                            size_t bufferSize) {
   return serialize<PrettyJsonSerializer>(source, buffer, bufferSize);
 }

+ 2 - 2
src/ArduinoJson/MsgPack/MsgPackSerializer.hpp

@@ -172,8 +172,8 @@ inline size_t serializeMsgPack(const TSource& source, TDestination& output) {
   return serialize<MsgPackSerializer>(source, output);
 }
 
-template <typename TSource, typename TDestination>
-inline size_t serializeMsgPack(const TSource& source, TDestination* output,
+template <typename TSource>
+inline size_t serializeMsgPack(const TSource& source, void* output,
                                size_t size) {
   return serialize<MsgPackSerializer>(source, output, size);
 }

+ 11 - 5
src/ArduinoJson/Serialization/serialize.hpp

@@ -24,14 +24,20 @@ size_t serialize(const TSource &source, TDestination &destination) {
 }
 
 template <template <typename> class TSerializer, typename TSource>
-size_t serialize(const TSource &source, char *buffer, size_t bufferSize) {
-  StaticStringWriter writer(buffer, bufferSize);
+size_t serialize(const TSource &source, void *buffer, size_t bufferSize) {
+  StaticStringWriter writer(reinterpret_cast<char *>(buffer), bufferSize);
   return doSerialize<TSerializer>(source, writer);
 }
 
-template <template <typename> class TSerializer, typename TSource, size_t N>
-size_t serialize(const TSource &source, char (&buffer)[N]) {
-  StaticStringWriter writer(buffer, N);
+template <template <typename> class TSerializer, typename TSource,
+          typename TChar, size_t N>
+#if defined _MSC_VER && _MSC_VER < 1900
+typename enable_if<sizeof(remove_reference<TChar>::type) == 1, size_t>::type
+#else
+typename enable_if<sizeof(TChar) == 1, size_t>::type
+#endif
+serialize(const TSource &source, TChar (&buffer)[N]) {
+  StaticStringWriter writer(reinterpret_cast<char *>(buffer), N);
   return doSerialize<TSerializer>(source, writer);
 }