forked from bblanchon/ArduinoJson
Added serializeMsgPack()
and measureMsgPack()
(closes #358)
This commit is contained in:
@ -73,6 +73,7 @@ add_subdirectory(JsonSerializer)
|
||||
add_subdirectory(JsonVariant)
|
||||
add_subdirectory(JsonWriter)
|
||||
add_subdirectory(Misc)
|
||||
add_subdirectory(MsgPack)
|
||||
add_subdirectory(MsgPackDeserializer)
|
||||
add_subdirectory(MsgPackSerializer)
|
||||
add_subdirectory(Polyfills)
|
||||
add_subdirectory(StaticJsonBuffer)
|
||||
|
@ -38,7 +38,13 @@ TEST_CASE("DeserializationError") {
|
||||
TEST_BOOLIFICATION(NotSupported, true);
|
||||
}
|
||||
|
||||
SECTION("ostream") {
|
||||
SECTION("ostream code") {
|
||||
std::stringstream s;
|
||||
s << DeserializationError(DeserializationError::InvalidInput);
|
||||
REQUIRE(s.str() == "InvalidInput");
|
||||
}
|
||||
|
||||
SECTION("ostream code") {
|
||||
std::stringstream s;
|
||||
s << DeserializationError::InvalidInput;
|
||||
REQUIRE(s.str() == "InvalidInput");
|
||||
|
@ -15,14 +15,14 @@ TEST_CASE("serialize JsonArray to std::string") {
|
||||
std::string json;
|
||||
serializeJson(array, json);
|
||||
|
||||
REQUIRE(std::string("[4,2]") == json);
|
||||
REQUIRE("[4,2]" == json);
|
||||
}
|
||||
|
||||
SECTION("serializeJsonPretty") {
|
||||
std::string json;
|
||||
serializeJsonPretty(array, json);
|
||||
|
||||
REQUIRE(std::string("[\r\n 4,\r\n 2\r\n]") == json);
|
||||
REQUIRE("[\r\n 4,\r\n 2\r\n]" == json);
|
||||
}
|
||||
}
|
||||
|
||||
@ -35,13 +35,13 @@ TEST_CASE("serialize JsonObject to std::string") {
|
||||
std::string json;
|
||||
serializeJson(doc, json);
|
||||
|
||||
REQUIRE(std::string("{\"key\":\"value\"}") == json);
|
||||
REQUIRE("{\"key\":\"value\"}" == json);
|
||||
}
|
||||
|
||||
SECTION("serializeJsonPretty") {
|
||||
std::string json;
|
||||
serializeJsonPretty(doc, json);
|
||||
|
||||
REQUIRE(std::string("{\r\n \"key\": \"value\"\r\n}") == json);
|
||||
REQUIRE("{\r\n \"key\": \"value\"\r\n}" == json);
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,7 @@
|
||||
#include <string>
|
||||
|
||||
#include <ArduinoJson/Json/JsonWriter.hpp>
|
||||
#include <ArduinoJson/Print/DynamicStringBuilder.hpp>
|
||||
#include <ArduinoJson/Serialization/DynamicStringBuilder.hpp>
|
||||
|
||||
using namespace ArduinoJson::Internals;
|
||||
|
||||
|
@ -5,7 +5,7 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
#include <ArduinoJson/Json/JsonWriter.hpp>
|
||||
#include <ArduinoJson/Print/StaticStringBuilder.hpp>
|
||||
#include <ArduinoJson/Serialization/StaticStringBuilder.hpp>
|
||||
|
||||
using namespace ArduinoJson::Internals;
|
||||
|
||||
|
@ -2,7 +2,7 @@
|
||||
# Copyright Benoit Blanchon 2014-2018
|
||||
# MIT License
|
||||
|
||||
add_executable(MsgPackTests
|
||||
add_executable(MsgPackDeserializerTests
|
||||
deserializeArray.cpp
|
||||
deserializeObject.cpp
|
||||
deserializeStaticVariant.cpp
|
||||
@ -15,5 +15,5 @@ add_executable(MsgPackTests
|
||||
std_istream.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(MsgPackTests catch)
|
||||
add_test(MsgPack MsgPackTests)
|
||||
target_link_libraries(MsgPackDeserializerTests catch)
|
||||
add_test(MsgPackDeserializer MsgPackDeserializerTests)
|
@ -5,7 +5,7 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("deserializeMsgPack(JsonArray&)") {
|
||||
TEST_CASE("deserialize MsgPack array") {
|
||||
DynamicJsonDocument doc;
|
||||
|
||||
SECTION("fixarray") {
|
@ -16,7 +16,7 @@ static void check(const char* input, U expected) {
|
||||
REQUIRE(variant.as<T>() == expected);
|
||||
}
|
||||
|
||||
TEST_CASE("deserializeMsgPack(JsonVariant&)") {
|
||||
TEST_CASE("deserialize MsgPack value") {
|
||||
SECTION("nil") {
|
||||
const char* nil = 0; // ArduinoJson uses a string for null
|
||||
check<const char*>("\xc0", nil);
|
14
test/MsgPackSerializer/CMakeLists.txt
Normal file
14
test/MsgPackSerializer/CMakeLists.txt
Normal file
@ -0,0 +1,14 @@
|
||||
# ArduinoJson - arduinojson.org
|
||||
# Copyright Benoit Blanchon 2014-2018
|
||||
# MIT License
|
||||
|
||||
add_executable(MsgPackSerializerTests
|
||||
destination_types.cpp
|
||||
measure.cpp
|
||||
serializeArray.cpp
|
||||
serializeObject.cpp
|
||||
serializeVariant.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(MsgPackSerializerTests catch)
|
||||
add_test(MsgPackSerializer MsgPackSerializerTests)
|
47
test/MsgPackSerializer/destination_types.cpp
Normal file
47
test/MsgPackSerializer/destination_types.cpp
Normal file
@ -0,0 +1,47 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("serialize MsgPack to various destination types") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonObject &object = doc.to<JsonObject>();
|
||||
object["hello"] = "world";
|
||||
const char *expected_result = "\x81\xA5hello\xA5world";
|
||||
const size_t expected_length = 13;
|
||||
|
||||
SECTION("std::string") {
|
||||
std::string result;
|
||||
size_t len = serializeMsgPack(object, result);
|
||||
|
||||
REQUIRE(expected_result == result);
|
||||
REQUIRE(expected_length == len);
|
||||
}
|
||||
|
||||
/* SECTION("std::vector<char>") {
|
||||
std::vector<char> result;
|
||||
size_t len = serializeMsgPack(object, result);
|
||||
|
||||
REQUIRE(std::vector<char>(expected_result, expected_result + 13) ==
|
||||
result);
|
||||
REQUIRE(expected_length == len);
|
||||
} */
|
||||
|
||||
SECTION("char[]") {
|
||||
char result[64];
|
||||
size_t len = serializeMsgPack(object, result);
|
||||
|
||||
REQUIRE(std::string(expected_result) == result);
|
||||
REQUIRE(expected_length == len);
|
||||
}
|
||||
|
||||
SECTION("char*") {
|
||||
char result[64];
|
||||
size_t len = serializeMsgPack(object, result, 64);
|
||||
|
||||
REQUIRE(std::string(expected_result) == result);
|
||||
REQUIRE(expected_length == len);
|
||||
}
|
||||
}
|
14
test/MsgPackSerializer/measure.cpp
Normal file
14
test/MsgPackSerializer/measure.cpp
Normal file
@ -0,0 +1,14 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("measureMsgPack()") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonObject &object = doc.to<JsonObject>();
|
||||
object["hello"] = "world";
|
||||
|
||||
REQUIRE(measureMsgPack(doc) == 13);
|
||||
}
|
60
test/MsgPackSerializer/serializeArray.cpp
Normal file
60
test/MsgPackSerializer/serializeArray.cpp
Normal file
@ -0,0 +1,60 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
static void check(const JsonArray& array, const char* expected_data,
|
||||
size_t expected_len) {
|
||||
std::string expected(expected_data, expected_data + expected_len);
|
||||
std::string actual;
|
||||
size_t len = serializeMsgPack(array, actual);
|
||||
CAPTURE(array);
|
||||
REQUIRE(len == expected_len);
|
||||
REQUIRE(actual == expected);
|
||||
}
|
||||
|
||||
template <size_t N>
|
||||
static void check(const JsonArray& array, const char (&expected_data)[N]) {
|
||||
const size_t expected_len = N - 1;
|
||||
check(array, expected_data, expected_len);
|
||||
}
|
||||
|
||||
// TODO: this function is used by the commented test
|
||||
// static void check(const JsonArray& array, const std::string& expected) {
|
||||
// check(array, expected.data(), expected.length());
|
||||
// }
|
||||
|
||||
TEST_CASE("serialize MsgPack array") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonArray& array = doc.to<JsonArray>();
|
||||
|
||||
SECTION("empty") {
|
||||
check(array, "\x90");
|
||||
}
|
||||
|
||||
SECTION("fixarray") {
|
||||
array.add("hello");
|
||||
array.add("world");
|
||||
|
||||
check(array, "\x92\xA5hello\xA5world");
|
||||
}
|
||||
|
||||
SECTION("array 16") {
|
||||
for (int i = 0; i < 16; i++) array.add(i);
|
||||
|
||||
check(array,
|
||||
"\xDC\x00\x10\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D"
|
||||
"\x0E\x0F");
|
||||
}
|
||||
|
||||
// TODO: this test is too slow
|
||||
// SECTION("array 32") {
|
||||
// const char* nil = 0;
|
||||
// for (int i = 0; i < 65536; i++) array.add(nil);
|
||||
//
|
||||
// check(array,
|
||||
// std::string("\xDD\x00\x01\x00\x00", 5) + std::string(65536, 0xC0));
|
||||
// }
|
||||
}
|
73
test/MsgPackSerializer/serializeObject.cpp
Normal file
73
test/MsgPackSerializer/serializeObject.cpp
Normal file
@ -0,0 +1,73 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <stdio.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
static void check(const JsonObject& object, const char* expected_data,
|
||||
size_t expected_len) {
|
||||
std::string expected(expected_data, expected_data + expected_len);
|
||||
std::string actual;
|
||||
size_t len = serializeMsgPack(object, actual);
|
||||
CAPTURE(object);
|
||||
REQUIRE(len == expected_len);
|
||||
REQUIRE(actual == expected);
|
||||
}
|
||||
|
||||
template <size_t N>
|
||||
static void check(const JsonObject& object, const char (&expected_data)[N]) {
|
||||
const size_t expected_len = N - 1;
|
||||
check(object, expected_data, expected_len);
|
||||
}
|
||||
|
||||
// TODO: used by the commented test
|
||||
// static void check(const JsonObject& object, const std::string& expected) {
|
||||
// check(object, expected.data(), expected.length());
|
||||
//}
|
||||
|
||||
TEST_CASE("serialize MsgPack object") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonObject& object = doc.to<JsonObject>();
|
||||
|
||||
SECTION("empty") {
|
||||
check(object, "\x80");
|
||||
}
|
||||
|
||||
SECTION("fixmap") {
|
||||
object["hello"] = "world";
|
||||
|
||||
check(object, "\x81\xA5hello\xA5world");
|
||||
}
|
||||
|
||||
SECTION("map 16") {
|
||||
for (int i = 0; i < 16; ++i) {
|
||||
char key[16];
|
||||
sprintf(key, "i%X", i);
|
||||
object[key] = i;
|
||||
}
|
||||
|
||||
check(object,
|
||||
"\xDE\x00\x10\xA2i0\x00\xA2i1\x01\xA2i2\x02\xA2i3\x03\xA2i4\x04\xA2i5"
|
||||
"\x05\xA2i6\x06\xA2i7\x07\xA2i8\x08\xA2i9\x09\xA2iA\x0A\xA2iB\x0B\xA2"
|
||||
"iC\x0C\xA2iD\x0D\xA2iE\x0E\xA2iF\x0F");
|
||||
}
|
||||
|
||||
// TODO: improve performance and uncomment
|
||||
// SECTION("map 32") {
|
||||
// std::string expected("\xDF\x00\x01\x00\x00", 5);
|
||||
//
|
||||
// for (int i = 0; i < 65536; ++i) {
|
||||
// char kv[16];
|
||||
// sprintf(kv, "%04x", i);
|
||||
// object[kv] = kv;
|
||||
// expected += '\xA4';
|
||||
// expected += kv;
|
||||
// expected += '\xA4';
|
||||
// expected += kv;
|
||||
// }
|
||||
//
|
||||
// check(object, expected);
|
||||
// }
|
||||
}
|
129
test/MsgPackSerializer/serializeVariant.cpp
Normal file
129
test/MsgPackSerializer/serializeVariant.cpp
Normal file
@ -0,0 +1,129 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
void check(JsonVariant variant, const char* expected_data,
|
||||
size_t expected_len) {
|
||||
std::string expected(expected_data, expected_data + expected_len);
|
||||
std::string actual;
|
||||
size_t len = serializeMsgPack(variant, actual);
|
||||
CAPTURE(variant);
|
||||
REQUIRE(len == expected_len);
|
||||
REQUIRE(actual == expected);
|
||||
}
|
||||
|
||||
template <size_t N>
|
||||
void check(JsonVariant variant, const char (&expected_data)[N]) {
|
||||
const size_t expected_len = N - 1;
|
||||
check(variant, expected_data, expected_len);
|
||||
}
|
||||
|
||||
void check(JsonVariant variant, const std::string& expected) {
|
||||
check(variant, expected.data(), expected.length());
|
||||
}
|
||||
|
||||
TEST_CASE("serialize MsgPack value") {
|
||||
SECTION("undefined") {
|
||||
check(JsonVariant(), "\xC0"); // we represent undefined as nil
|
||||
}
|
||||
|
||||
SECTION("nil") {
|
||||
const char* nil = 0; // ArduinoJson uses a string for null
|
||||
check(nil, "\xC0");
|
||||
}
|
||||
|
||||
SECTION("bool") {
|
||||
check(false, "\xC2");
|
||||
check(true, "\xC3");
|
||||
}
|
||||
|
||||
SECTION("positive fixint") {
|
||||
check(0, "\x00");
|
||||
check(127, "\x7F");
|
||||
}
|
||||
|
||||
SECTION("uint 8") {
|
||||
check(128, "\xCC\x80");
|
||||
check(255, "\xCC\xFF");
|
||||
}
|
||||
|
||||
SECTION("uint 16") {
|
||||
check(256, "\xCD\x01\x00");
|
||||
check(0xFFFF, "\xCD\xFF\xFF");
|
||||
}
|
||||
|
||||
SECTION("uint 32") {
|
||||
check(0x00010000U, "\xCE\x00\x01\x00\x00");
|
||||
check(0x12345678U, "\xCE\x12\x34\x56\x78");
|
||||
check(0xFFFFFFFFU, "\xCE\xFF\xFF\xFF\xFF");
|
||||
}
|
||||
|
||||
#if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
|
||||
SECTION("uint 64") {
|
||||
check(0x0001000000000000U, "\xCF\x00\x01\x00\x00\x00\x00\x00\x00");
|
||||
check(0x123456789ABCDEF0U, "\xCF\x12\x34\x56\x78\x9A\xBC\xDE\xF0");
|
||||
check(0xFFFFFFFFFFFFFFFFU, "\xCF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF");
|
||||
}
|
||||
#endif
|
||||
|
||||
SECTION("negative fixint") {
|
||||
check(-1, "\xFF");
|
||||
check(-32, "\xE0");
|
||||
}
|
||||
|
||||
SECTION("int 8") {
|
||||
check(-33, "\xD0\xDF");
|
||||
check(-128, "\xD0\x80");
|
||||
}
|
||||
|
||||
SECTION("int 16") {
|
||||
check(-129, "\xD1\xFF\x7F");
|
||||
check(-32768, "\xD1\x80\x00");
|
||||
}
|
||||
|
||||
SECTION("int 32") {
|
||||
check(-32769, "\xD2\xFF\xFF\x7F\xFF");
|
||||
check(-2147483647 - 1, "\xD2\x80\x00\x00\x00");
|
||||
}
|
||||
|
||||
#if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
|
||||
SECTION("int 64") {
|
||||
check(int64_t(0xFEDCBA9876543210), "\xD3\xFE\xDC\xBA\x98\x76\x54\x32\x10");
|
||||
}
|
||||
#endif
|
||||
|
||||
SECTION("float 32") {
|
||||
check(1.25, "\xCA\x3F\xA0\x00\x00");
|
||||
}
|
||||
|
||||
SECTION("float 64") {
|
||||
check(3.1415, "\xCB\x40\x09\x21\xCA\xC0\x83\x12\x6F");
|
||||
}
|
||||
|
||||
SECTION("fixstr") {
|
||||
check("", "\xA0");
|
||||
check("hello world hello world hello !",
|
||||
"\xBFhello world hello world hello !");
|
||||
}
|
||||
|
||||
SECTION("str 8") {
|
||||
check("hello world hello world hello !!",
|
||||
"\xD9\x20hello world hello world hello !!");
|
||||
}
|
||||
|
||||
SECTION("str 16") {
|
||||
std::string shortest(256, '?');
|
||||
check(shortest.c_str(), std::string("\xDA\x01\x00", 3) + shortest);
|
||||
|
||||
std::string longest(65535, '?');
|
||||
check(longest.c_str(), std::string("\xDA\xFF\xFF", 3) + longest);
|
||||
}
|
||||
|
||||
SECTION("str 32") {
|
||||
std::string shortest(65536, '?');
|
||||
check(shortest.c_str(), std::string("\xDB\x00\x01\x00\x00", 5) + shortest);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user