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

This commit is contained in:
Benoit Blanchon
2019-12-13 14:15:30 +01:00
parent 062c1c13b5
commit 8bf6f6e09f
6 changed files with 80 additions and 9 deletions

View File

@ -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)
-------

View File

@ -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);

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}