Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/viordash/rapidjsonwrapper
Wrap RapidJSON to be used as objects of predefined classes
https://github.com/viordash/rapidjsonwrapper
cpp-json json-cpp rapidjson
Last synced: about 3 hours ago
JSON representation
Wrap RapidJSON to be used as objects of predefined classes
- Host: GitHub
- URL: https://github.com/viordash/rapidjsonwrapper
- Owner: viordash
- License: mit
- Created: 2022-06-19T16:14:37.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2023-03-19T14:13:17.000Z (almost 2 years ago)
- Last Synced: 2024-11-12T13:56:01.705Z (about 1 month ago)
- Topics: cpp-json, json-cpp, rapidjson
- Language: C++
- Homepage:
- Size: 2.49 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# RapidJSONWrapper
[![CMake](https://github.com/viordash/RapidJSONWrapper/actions/workflows/cmake.yml/badge.svg?branch=master)](https://github.com/viordash/RapidJSONWrapper/actions/workflows/cmake.yml)Wrap RapidJSON to be used as objects of predefined class
Objects are represented as DTOs with json serialization/deserialization support:
class UserDto : public JsonObject {
public:
JsonValue Name;
JsonCommonValue Role;
UserDto(char *name = {}, TUserRole role = {})
: Name(this, "name", name),
Role(this, "role", role){};
};
class GoodsDto : public JsonObject {
public:
JsonValue Id;
JsonValue Created;
JsonValue Group;
JsonValue Name;
JsonValue Price;
JsonValue Quantity;
JsonCommonValue Deleted;
JsonCommonValue StoreName;
GoodsDto(int id = {}, uint32_t created = {}, char *group = {}, char *name = {}, float price = {}, double quantity = {}, bool deleted = {}, char *storeName = {})
: Id(this, "Id", id),
Created(this, "Created", created),
Group(this, "Group", group),
Name(this, "Name", name),
Price(this, "Price", price),
Quantity(this, "Quantity", quantity),
Deleted(this, "Deleted", deleted),
StoreName(this, "StoreName", storeName){};
};
class GoodsList : public JsonObjectsArray {
public:
bool Validate(JsonObject *item) override { return item->Validate(); }
JsonObject *CreateItem() override { return new GoodsDto(); }
};
class OrderDto : public JsonObject {
public:
JsonValue Supplier;
JsonCommonValue DateTime;
JsonValue Goods;
JsonValue User;
GoodsList goodsList;
UserDto userDto;
OrderDto(char *supplier = {}, uint32_t dateTime = {}, char *userName = {}, TUserRole userRole = {})
: Supplier(this, "supplier", supplier),
DateTime(this, "dateTime", dateTime),
Goods(this, "goods", &goodsList),
userDto(userName, userRole),
User(this, "user", &userDto){};
};sample code (from tests):
TEST(JsonObjectTestsGroup, JsonObject_Complex_TryParse_Test) {
OrderDto order;
CHECK(order.TryStringParse("{\"supplier\":\"Dell\",\"dateTime\":1657058000,\"goods\":[{\"Id\":1,\"Created\":1657052789,\"Group\":\"Keyboards\",\"Name\":\"K1-100\",\"Price\":58."
"25,\"Quantity\":48.2,\"Deleted\":false,\"StoreName\":\"\"},{\"Id\":3,\"Created\":1657054789,\"Group\":\"Keyboards\",\"Name\":\"K3-100\",\"Price\":"
"258.25,\"Quantity\":548.2,\"Deleted\":false,\"StoreName\":\"\"},{\"Id\":4,\"Created\":1657055789,\"Group\":\"Keyboards\",\"Name\":\"K4-100\","
"\"Price\":358.25,\"Quantity\":648.2,\"Deleted\":false,\"StoreName\":\"\"}],\"user\":{\"name\":\"Joe Doe\",\"role\":1}}"));
CHECK_EQUAL(order.goodsList.Size(), 3);
CHECK_EQUAL(order.goodsList.Item(0)->Created.Value, 1657052789);
STRCMP_EQUAL(order.goodsList.Item(2)->Name.Value, "K4-100");
STRCMP_EQUAL(order.userDto.Name.Value, "Joe Doe");
}
TEST(JsonObjectTestsGroup, JsonObject_Complex_WriteTo_Test) {
OrderDto orderDto("Dell", 1657058000, "Joe Doe", TUserRole::uViewer);
orderDto.goodsList.Add(new GoodsDto(1, 1657052789, "Keyboards", "K1-100", 58.25, 48.2));
orderDto.goodsList.Add(new GoodsDto(2, 1657053789, "Keyboards", "K2-100", 158.25, 448.2));
orderDto.goodsList.Add(new GoodsDto(3, 1657054789, "Keyboards", "K3-100", 258.25, 548.2));
orderDto.goodsList.Add(new GoodsDto(4, 1657055789, "Keyboards", "K4-100", 358.25, 648.2));
rapidjson::Document doc;
doc.SetObject();
orderDto.WriteToDoc(&doc);
rapidjson::StringBuffer buffer;
rapidjson::Writer writer(buffer);
doc.Accept(writer);
const char *jsonStr = buffer.GetString();
STRCMP_EQUAL(jsonStr, "{\"supplier\":\"Dell\",\"dateTime\":1657058000,\"goods\":[{\"Id\":1,\"Created\":1657052789,\"Group\":\"Keyboards\",\"Name\":\"K1-100\",\"Price\":58."
"25,\"Quantity\":48.2,\"Deleted\":false,\"StoreName\":null},{\"Id\":2,\"Created\":1657053789,\"Group\":\"Keyboards\",\"Name\":\"K2-100\",\"Price\":158."
"25,\"Quantity\":448.2,\"Deleted\":false,\"StoreName\":null},{\"Id\":3,\"Created\":1657054789,\"Group\":\"Keyboards\",\"Name\":\"K3-100\",\"Price\":"
"258.25,\"Quantity\":548.2,\"Deleted\":false,\"StoreName\":null},{\"Id\":4,\"Created\":1657055789,\"Group\":\"Keyboards\",\"Name\":\"K4-100\","
"\"Price\":358.25,\"Quantity\":648.2,\"Deleted\":false,\"StoreName\":null}],\"user\":{\"name\":\"Joe Doe\",\"role\":1}}");
}