Skip to content

Commit

Permalink
Fix any, embedded object, array, and struct serialization (#94)
Browse files Browse the repository at this point in the history
* add embedded object serialization tests (failing)

* Add empty object tests (failing)

* test for deserializing null field object (failing)

* Fix embedded object deserialization

* Fix serializing empty objects on nlohmann

* Fix nullptr_t handling in `any`

* Add test for de/serializing struct embedded in `object`

* Remove extraneous `get()` from `any`

* fix compiler errors and warnings on gcc

* add `any = dap::null` assignment test

* Remove extraneous template
  • Loading branch information
nikitalita authored Feb 16, 2023
1 parent d904114 commit 315ffff
Show file tree
Hide file tree
Showing 6 changed files with 273 additions and 13 deletions.
13 changes: 13 additions & 0 deletions include/dap/any.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ namespace dap {

template <typename T>
struct TypeOf;
class Deserializer;
class Serializer;

// any provides a type-safe container for values of any of dap type (boolean,
// integer, number, array, variant, any, null, dap-structs).
Expand All @@ -47,6 +49,7 @@ class any {
inline any& operator=(any&& rhs) noexcept;
template <typename T>
inline any& operator=(const T& val);
inline any& operator=(const std::nullptr_t& val);

// get() returns the contained value of the type T.
// If the any does not contain a value of type T, then get() will assert.
Expand All @@ -58,6 +61,9 @@ class any {
inline bool is() const;

private:
friend class Deserializer;
friend class Serializer;

static inline void* alignUp(void* val, size_t alignment);
inline void alloc(size_t size, size_t align);
inline void free();
Expand Down Expand Up @@ -142,8 +148,15 @@ any& any::operator=(const T& val) {
return *this;
}

any& any::operator=(const std::nullptr_t&) {
reset();
return *this;
}

template <typename T>
T& any::get() const {
static_assert(!std::is_same<T, std::nullptr_t>(),
"Cannot get nullptr from 'any'.");
assert(is<T>());
return *reinterpret_cast<T*>(value);
}
Expand Down
10 changes: 10 additions & 0 deletions include/dap/serialization.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,18 @@ class Serializer {

// deserialize() encodes the given string.
inline bool serialize(const char* v);
protected:
static inline const TypeInfo* get_any_type(const any&);
static inline const void* get_any_val(const any&);
};

inline const TypeInfo* Serializer::get_any_type(const any& a){
return a.type;
}
const void* Serializer::get_any_val(const any& a) {
return a.value;
}

template <typename T, typename>
bool Serializer::serialize(const T& object) {
return TypeOf<T>::type()->serialize(this, &object);
Expand Down
47 changes: 37 additions & 10 deletions src/any_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ namespace {
template <typename T>
struct TestValue {};

template <>
struct TestValue<dap::null> {
static const dap::null value;
};
template <>
struct TestValue<dap::integer> {
static const dap::integer value;
Expand All @@ -67,6 +71,7 @@ struct TestValue<dap::AnyTestObject> {
static const dap::AnyTestObject value;
};

const dap::null TestValue<dap::null>::value = nullptr;
const dap::integer TestValue<dap::integer>::value = 20;
const dap::boolean TestValue<dap::boolean>::value = true;
const dap::number TestValue<dap::number>::value = 123.45;
Expand Down Expand Up @@ -152,7 +157,24 @@ TEST(Any, TestObject) {
template <typename T>
class AnyT : public ::testing::Test {
protected:
void check(const dap::any& any, const T& expect) {
template <typename T0,
typename = std::enable_if<std::is_same<T, T0>::value &&
!std::is_same<T0, dap::null>::value>>
void check_val(const dap::any& any, const T0& expect) {
ASSERT_EQ(any.is<T>(), any.is<T0>());
ASSERT_EQ(any.get<T>(), expect);
}

// Special case for Null assignment, as we can assign nullptr_t to any but
// can't `get()` it
template <typename = dap::null>
void check_val(const dap::any& any, const dap::null& expect) {
ASSERT_EQ(nullptr, expect);
ASSERT_TRUE(any.is<dap::null>());
}

void check_type(const dap::any& any) {
ASSERT_EQ(any.is<dap::null>(), (std::is_same<T, dap::null>::value));
ASSERT_EQ(any.is<dap::integer>(), (std::is_same<T, dap::integer>::value));
ASSERT_EQ(any.is<dap::boolean>(), (std::is_same<T, dap::boolean>::value));
ASSERT_EQ(any.is<dap::number>(), (std::is_same<T, dap::number>::value));
Expand All @@ -161,36 +183,38 @@ class AnyT : public ::testing::Test {
(std::is_same<T, dap::array<dap::string>>::value));
ASSERT_EQ(any.is<dap::AnyTestObject>(),
(std::is_same<T, dap::AnyTestObject>::value));

ASSERT_EQ(any.get<T>(), expect);
}
};
TYPED_TEST_SUITE_P(AnyT);

TYPED_TEST_P(AnyT, CopyConstruct) {
auto val = TestValue<TypeParam>::value;
dap::any any(val);
this->check(any, val);
this->check_type(any);
this->check_val(any, val);
}

TYPED_TEST_P(AnyT, MoveConstruct) {
auto val = TestValue<TypeParam>::value;
dap::any any(std::move(val));
this->check(any, val);
this->check_type(any);
this->check_val(any, val);
}

TYPED_TEST_P(AnyT, Assign) {
auto val = TestValue<TypeParam>::value;
dap::any any;
any = val;
this->check(any, val);
this->check_type(any);
this->check_val(any, val);
}

TYPED_TEST_P(AnyT, MoveAssign) {
auto val = TestValue<TypeParam>::value;
dap::any any;
any = std::move(val);
this->check(any, val);
this->check_type(any);
this->check_val(any, val);
}

TYPED_TEST_P(AnyT, RepeatedAssign) {
Expand All @@ -199,7 +223,8 @@ TYPED_TEST_P(AnyT, RepeatedAssign) {
dap::any any;
any = str;
any = val;
this->check(any, val);
this->check_type(any);
this->check_val(any, val);
}

TYPED_TEST_P(AnyT, RepeatedMoveAssign) {
Expand All @@ -208,7 +233,8 @@ TYPED_TEST_P(AnyT, RepeatedMoveAssign) {
dap::any any;
any = std::move(str);
any = std::move(val);
this->check(any, val);
this->check_type(any);
this->check_val(any, val);
}

REGISTER_TYPED_TEST_SUITE_P(AnyT,
Expand All @@ -219,7 +245,8 @@ REGISTER_TYPED_TEST_SUITE_P(AnyT,
RepeatedAssign,
RepeatedMoveAssign);

using AnyTypes = ::testing::Types<dap::integer,
using AnyTypes = ::testing::Types<dap::null,
dap::integer,
dap::boolean,
dap::number,
dap::string,
Expand Down
170 changes: 168 additions & 2 deletions src/json_serializer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,60 @@ struct JSONObjectNoFields {};

DAP_STRUCT_TYPEINFO(JSONObjectNoFields, "json-object-no-fields");

struct SimpleJSONTestObject {
boolean b;
integer i;
};
DAP_STRUCT_TYPEINFO(SimpleJSONTestObject,
"simple-json-test-object",
DAP_FIELD(b, "b"),
DAP_FIELD(i, "i"));

} // namespace dap

TEST(JSONSerializer, SerializeDeserialize) {
class JSONSerializer : public testing::Test {
protected:
static dap::object GetSimpleObject() {
return dap::object({{"one", dap::integer(1)},
{"two", dap::number(2)},
{"three", dap::string("three")},
{"four", dap::boolean(true)}});
}
void TEST_SIMPLE_OBJECT(const dap::object& obj) {
NESTED_TEST_FAILED = true;
auto ref_obj = GetSimpleObject();
ASSERT_EQ(obj.size(), ref_obj.size());
ASSERT_TRUE(obj.at("one").is<dap::integer>());
ASSERT_TRUE(obj.at("two").is<dap::number>());
ASSERT_TRUE(obj.at("three").is<dap::string>());
ASSERT_TRUE(obj.at("four").is<dap::boolean>());

ASSERT_EQ(ref_obj.at("one").get<dap::integer>(),
obj.at("one").get<dap::integer>());
ASSERT_EQ(ref_obj.at("two").get<dap::number>(),
obj.at("two").get<dap::number>());
ASSERT_EQ(ref_obj.at("three").get<dap::string>(),
obj.at("three").get<dap::string>());
ASSERT_EQ(ref_obj.at("four").get<dap::boolean>(),
obj.at("four").get<dap::boolean>());
NESTED_TEST_FAILED = false;
}
template <typename T>
void TEST_SERIALIZING_DESERIALIZING(const T& encoded, T& decoded) {
NESTED_TEST_FAILED = true;
dap::json::Serializer s;
ASSERT_TRUE(s.serialize(encoded));
dap::json::Deserializer d(s.dump());
ASSERT_TRUE(d.deserialize(&decoded));
NESTED_TEST_FAILED = false;
}
bool NESTED_TEST_FAILED = false;
#define _ASSERT_PASS(NESTED_TEST) \
NESTED_TEST; \
ASSERT_FALSE(NESTED_TEST_FAILED);
};

TEST_F(JSONSerializer, SerializeDeserialize) {
dap::JSONTestObject encoded;
encoded.b = true;
encoded.i = 32;
Expand Down Expand Up @@ -92,9 +143,124 @@ TEST(JSONSerializer, SerializeDeserialize) {
ASSERT_EQ(encoded.inner.i, decoded.inner.i);
}

TEST(JSONSerializer, SerializeObjectNoFields) {
TEST_F(JSONSerializer, SerializeObjectNoFields) {
dap::JSONObjectNoFields obj;
dap::json::Serializer s;
ASSERT_TRUE(s.serialize(obj));
ASSERT_EQ(s.dump(), "{}");
}

TEST_F(JSONSerializer, SerializeDeserializeObject) {
dap::object encoded = GetSimpleObject();
dap::object decoded;
_ASSERT_PASS(TEST_SERIALIZING_DESERIALIZING(encoded, decoded));
_ASSERT_PASS(TEST_SIMPLE_OBJECT(decoded));
}

TEST_F(JSONSerializer, SerializeDeserializeEmbeddedObject) {
dap::object encoded;
dap::object decoded;
// object nested inside object
dap::object encoded_embed_obj = GetSimpleObject();
dap::object decoded_embed_obj;
encoded["embed_obj"] = encoded_embed_obj;
_ASSERT_PASS(TEST_SERIALIZING_DESERIALIZING(encoded, decoded));
ASSERT_TRUE(decoded["embed_obj"].is<dap::object>());
decoded_embed_obj = decoded["embed_obj"].get<dap::object>();
_ASSERT_PASS(TEST_SIMPLE_OBJECT(decoded_embed_obj));
}

TEST_F(JSONSerializer, SerializeDeserializeEmbeddedStruct) {
dap::object encoded;
dap::object decoded;
// object nested inside object
dap::SimpleJSONTestObject encoded_embed_struct;
encoded_embed_struct.b = true;
encoded_embed_struct.i = 50;
encoded["embed_struct"] = encoded_embed_struct;

dap::object decoded_embed_obj;
_ASSERT_PASS(TEST_SERIALIZING_DESERIALIZING(encoded, decoded));
ASSERT_TRUE(decoded["embed_struct"].is<dap::object>());
decoded_embed_obj = decoded["embed_struct"].get<dap::object>();
ASSERT_TRUE(decoded_embed_obj.at("b").is<dap::boolean>());
ASSERT_TRUE(decoded_embed_obj.at("i").is<dap::integer>());

ASSERT_EQ(encoded_embed_struct.b, decoded_embed_obj["b"].get<dap::boolean>());
ASSERT_EQ(encoded_embed_struct.i, decoded_embed_obj["i"].get<dap::integer>());
}

TEST_F(JSONSerializer, SerializeDeserializeEmbeddedIntArray) {
dap::object encoded;
dap::object decoded;
// array nested inside object
dap::array<dap::integer> encoded_embed_arr = {1, 2, 3, 4};
dap::array<dap::any> decoded_embed_arr;

encoded["embed_arr"] = encoded_embed_arr;

_ASSERT_PASS(TEST_SERIALIZING_DESERIALIZING(encoded, decoded));
// TODO: Deserializing array should infer basic member types
ASSERT_TRUE(decoded["embed_arr"].is<dap::array<dap::any>>());
decoded_embed_arr = decoded["embed_arr"].get<dap::array<dap::any>>();
ASSERT_EQ(encoded_embed_arr.size(), decoded_embed_arr.size());
for (std::size_t i = 0; i < decoded_embed_arr.size(); i++) {
ASSERT_TRUE(decoded_embed_arr[i].is<dap::integer>());
ASSERT_EQ(encoded_embed_arr[i], decoded_embed_arr[i].get<dap::integer>());
}
}

TEST_F(JSONSerializer, SerializeDeserializeEmbeddedObjectArray) {
dap::object encoded;
dap::object decoded;

dap::array<dap::object> encoded_embed_arr = {GetSimpleObject(),
GetSimpleObject()};
dap::array<dap::any> decoded_embed_arr;

encoded["embed_arr"] = encoded_embed_arr;

_ASSERT_PASS(TEST_SERIALIZING_DESERIALIZING(encoded, decoded));
// TODO: Deserializing array should infer basic member types
ASSERT_TRUE(decoded["embed_arr"].is<dap::array<dap::any>>());
decoded_embed_arr = decoded["embed_arr"].get<dap::array<dap::any>>();
ASSERT_EQ(encoded_embed_arr.size(), decoded_embed_arr.size());
for (std::size_t i = 0; i < decoded_embed_arr.size(); i++) {
ASSERT_TRUE(decoded_embed_arr[i].is<dap::object>());
_ASSERT_PASS(TEST_SIMPLE_OBJECT(decoded_embed_arr[i].get<dap::object>()));
}
}

TEST_F(JSONSerializer, DeserializeSerializeEmptyObject) {
auto empty_obj = "{}";
dap::object decoded;
dap::json::Deserializer d(empty_obj);
ASSERT_TRUE(d.deserialize(&decoded));
dap::json::Serializer s;
ASSERT_TRUE(s.serialize(decoded));
ASSERT_EQ(s.dump(), empty_obj);
}

TEST_F(JSONSerializer, SerializeDeserializeEmbeddedEmptyObject) {
dap::object encoded_empty_obj;
dap::object encoded = {{"empty_obj", encoded_empty_obj}};
dap::object decoded;

_ASSERT_PASS(TEST_SERIALIZING_DESERIALIZING(encoded, decoded));
ASSERT_TRUE(decoded["empty_obj"].is<dap::object>());
dap::object decoded_empty_obj = decoded["empty_obj"].get<dap::object>();
ASSERT_EQ(encoded_empty_obj.size(), decoded_empty_obj.size());
}

TEST_F(JSONSerializer, SerializeDeserializeObjectWithNulledField) {
auto thing = dap::any(dap::null());
dap::object encoded;
encoded["nulled_field"] = dap::null();
dap::json::Serializer s;
ASSERT_TRUE(s.serialize(encoded));
dap::object decoded;
auto dump = s.dump();
dap::json::Deserializer d(dump);
ASSERT_TRUE(d.deserialize(&decoded));
ASSERT_TRUE(encoded["nulled_field"].is<dap::null>());
}
Loading

0 comments on commit 315ffff

Please sign in to comment.