Unverified Commit 83448577 authored by Niels Lohmann's avatar Niels Lohmann Committed by GitHub

Merge pull request #2287 from pfeatherstone/develop

Serialisation macros: increase upper bound on number of member variables
parents 893eda83 35b899e9
This diff is collapsed.
This diff is collapsed.
......@@ -27,6 +27,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <string>
#include <vector>
#include "doctest_compatibility.h"
#include <nlohmann/json.hpp>
......@@ -100,6 +102,133 @@ class person_without_private_data_2
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(person_without_private_data_2, age, name, metadata)
class person_with_private_alphabet
{
public:
bool operator==(const person_with_private_alphabet& other)
{
return a == other.a &&
b == other.b &&
c == other.c &&
d == other.d &&
e == other.e &&
f == other.f &&
g == other.g &&
h == other.h &&
i == other.i &&
j == other.j &&
k == other.k &&
l == other.l &&
m == other.m &&
n == other.n &&
o == other.o &&
p == other.p &&
q == other.q &&
r == other.r &&
s == other.s &&
t == other.t &&
u == other.u &&
v == other.v &&
w == other.w &&
x == other.x &&
y == other.y &&
z == other.z;
}
private:
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
int f = 0;
int g = 0;
int h = 0;
int i = 0;
int j = 0;
int k = 0;
int l = 0;
int m = 0;
int n = 0;
int o = 0;
int p = 0;
int q = 0;
int r = 0;
int s = 0;
int t = 0;
int u = 0;
int v = 0;
int w = 0;
int x = 0;
int y = 0;
int z = 0;
NLOHMANN_DEFINE_TYPE_INTRUSIVE(person_with_private_alphabet, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z)
};
class person_with_public_alphabet
{
public:
bool operator==(const person_with_public_alphabet& other)
{
return a == other.a &&
b == other.b &&
c == other.c &&
d == other.d &&
e == other.e &&
f == other.f &&
g == other.g &&
h == other.h &&
i == other.i &&
j == other.j &&
k == other.k &&
l == other.l &&
m == other.m &&
n == other.n &&
o == other.o &&
p == other.p &&
q == other.q &&
r == other.r &&
s == other.s &&
t == other.t &&
u == other.u &&
v == other.v &&
w == other.w &&
x == other.x &&
y == other.y &&
z == other.z;
}
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
int f = 0;
int g = 0;
int h = 0;
int i = 0;
int j = 0;
int k = 0;
int l = 0;
int m = 0;
int n = 0;
int o = 0;
int p = 0;
int q = 0;
int r = 0;
int s = 0;
int t = 0;
int u = 0;
int v = 0;
int w = 0;
int x = 0;
int y = 0;
int z = 0;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(person_with_public_alphabet, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z)
} // namespace persons
TEST_CASE_TEMPLATE("Serialization/deserialization via NLOHMANN_DEFINE_TYPE_INTRUSIVE", T,
......@@ -128,3 +257,75 @@ TEST_CASE_TEMPLATE("Serialization/deserialization via NLOHMANN_DEFINE_TYPE_INTRU
CHECK_THROWS_WITH_AS(p3 = json(j), "[json.exception.out_of_range.403] key 'age' not found", json::out_of_range);
}
}
TEST_CASE_TEMPLATE("Serialization/deserialization of classes with 26 public/private member variables via NLOHMANN_DEFINE_TYPE_INTRUSIVE and NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE", T,
persons::person_with_private_alphabet,
persons::person_with_public_alphabet)
{
SECTION("alphabet")
{
{
T obj1;
nlohmann::json j = obj1; //via json object
T obj2;
j.get_to(obj2);
bool ok = (obj1 == obj2);
CHECK(ok);
}
{
T obj1;
nlohmann::json j1 = obj1; //via json string
std::string s = j1.dump();
nlohmann::json j2 = nlohmann::json::parse(s);
T obj2;
j2.get_to(obj2);
bool ok = (obj1 == obj2);
CHECK(ok);
}
{
T obj1;
nlohmann::json j1 = obj1; //via msgpack
std::vector<uint8_t> buf = nlohmann::json::to_msgpack(j1);
nlohmann::json j2 = nlohmann::json::from_msgpack(buf);
T obj2;
j2.get_to(obj2);
bool ok = (obj1 == obj2);
CHECK(ok);
}
{
T obj1;
nlohmann::json j1 = obj1; //via bson
std::vector<uint8_t> buf = nlohmann::json::to_bson(j1);
nlohmann::json j2 = nlohmann::json::from_bson(buf);
T obj2;
j2.get_to(obj2);
bool ok = (obj1 == obj2);
CHECK(ok);
}
{
T obj1;
nlohmann::json j1 = obj1; //via cbor
std::vector<uint8_t> buf = nlohmann::json::to_cbor(j1);
nlohmann::json j2 = nlohmann::json::from_cbor(buf);
T obj2;
j2.get_to(obj2);
bool ok = (obj1 == obj2);
CHECK(ok);
}
{
T obj1;
nlohmann::json j1 = obj1; //via ubjson
std::vector<uint8_t> buf = nlohmann::json::to_ubjson(j1);
nlohmann::json j2 = nlohmann::json::from_ubjson(buf);
T obj2;
j2.get_to(obj2);
bool ok = (obj1 == obj2);
CHECK(ok);
}
}
}
#include <cstdlib>
#include <iostream>
#include <sstream>
using namespace std;
void build_code(int max_args)
{
stringstream ss;
ss << "#define NLOHMANN_JSON_EXPAND( x ) x" << endl;
ss << "#define NLOHMANN_JSON_GET_MACRO(";
for (int i = 0 ; i < max_args ; i++)
ss << "_" << i + 1 << ", ";
ss << "NAME,...) NAME" << endl;
ss << "#define NLOHMANN_JSON_PASTE(...) NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_GET_MACRO(__VA_ARGS__, \\" << endl;
for (int i = max_args ; i > 1 ; i--)
ss << "NLOHMANN_JSON_PASTE" << i << ", \\" << endl;
ss << "NLOHMANN_JSON_PASTE1)(__VA_ARGS__))" << endl;
ss << "#define NLOHMANN_JSON_PASTE2(func, v1) func(v1)" << endl;
for (int i = 3 ; i <= max_args ; i++)
{
ss << "#define NLOHMANN_JSON_PASTE" << i << "(func, ";
for (int j = 1 ; j < i -1 ; j++)
ss << "v" << j << ", ";
ss << "v" << i-1 << ") NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE" << i-1 << "(func, ";
for (int j = 2 ; j < i-1 ; j++)
ss << "v" << j << ", ";
ss << "v" << i-1 << ")" << endl;
}
cout << ss.str() << endl;
}
int main(int argc, char** argv)
{
int max_args = 64;
build_code(max_args);
return 0;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment