viordash / RapidJSONWrapper

Wrap RapidJSON to be used as objects of predefined classes

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

RapidJSONWrapper

CMake

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<char *> Name;
	JsonCommonValue<uint32_t> Role;

	UserDto(char *name = {}, TUserRole role = {})
		: Name(this, "name", name), 
		  Role(this, "role", role){};
};

class GoodsDto : public JsonObject {
  public:
	JsonValue<int> Id;
	JsonValue<uint32_t> Created;
	JsonValue<char *> Group;
	JsonValue<char *> Name;
	JsonValue<float> Price;
	JsonValue<double> Quantity;
	JsonCommonValue<bool> Deleted;
	JsonCommonValue<char *> 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<char *> Supplier;
	JsonCommonValue<uint32_t> DateTime;
	JsonValue<JsonArrayBase *> Goods;
	JsonValue<JsonObject *> 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<GoodsDto *>(0)->Created.Value, 1657052789);
		STRCMP_EQUAL(order.goodsList.Item<GoodsDto *>(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<rapidjson::StringBuffer> 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}}");
}

About

Wrap RapidJSON to be used as objects of predefined classes

License:MIT License


Languages

Language:C++ 66.9%Language:Makefile 13.4%Language:Shell 11.9%Language:C 3.7%Language:M4 2.1%Language:CMake 1.0%Language:Ruby 0.6%Language:PowerShell 0.3%Language:Batchfile 0.1%Language:sed 0.0%