Skip to content

Commit

Permalink
Update sample and some class definitions.
Browse files Browse the repository at this point in the history
  • Loading branch information
eyalmolad committed Mar 28, 2020
1 parent adee12e commit 56e7d96
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 32 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
bin
.vs
obj
Debug
*.user
/C++/RapidJSONSample/DataSampleArrayNew.json
/C++/RapidJSONSample/DataSampleNew.json
7 changes: 6 additions & 1 deletion C++/RapidJSONSample/DataSample.json
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
{"id":9,"name":"Bush Somerset Collection Bookcase","category":"Furniture","sales":122.0}
{
"id": 9,
"name": "Bush Somerset Collection Bookcase",
"category": "Furniture",
"sales":122.0
}
21 changes: 20 additions & 1 deletion C++/RapidJSONSample/DataSampleArray.json
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
[{"id":1,"name":"Bush Somerset Collection Bookcase","category":"Furniture","sales":22.0},{"id":2,"name":"Mitel 5320 IP Phone VoIP phone","category":"Technology","sales":907.1519775390625},{"id":3,"name":"Poly String Tie Envelopes","category":"Office Supplies","sales":3.2639999389648439},{"id":9,"name":"Bush Somerset Collection Bookcase","category":"Furniture","sales":122.0},{"id":9,"name":"Bush Somerset Collection Bookcase","category":"Furniture","sales":122.0}]
[
{
"id": 1,
"name": "Bush Somerset Collection Bookcase",
"category": "Furniture",
"sales": 22.0
},
{
"id": 2,
"name": "Mitel 5320 IP Phone VoIP phone",
"category": "Technology",
"sales": 907.1519775390625
},
{
"id": 3,
"name": "Poly String Tie Envelopes",
"category": "Office Supplies",
"sales": 3.2639999389648439
}
]
11 changes: 11 additions & 0 deletions C++/RapidJSONSample/JSONBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ std::string JSONBase::Serialize() const
return "";
}

bool JSONBase::Deserialize(const std::string& s)
{
rapidjson::Document doc;
if (!InitDocument(s, doc))
return false;

Deserialize(doc);

return true;
}

bool JSONBase::DeserializeFromFile(const std::string& filePath)
{
std::ifstream f(filePath);
Expand Down
9 changes: 3 additions & 6 deletions C++/RapidJSONSample/JSONBase.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
#pragma once
#include "JSONIncludes.h"


class JSONBase
{
public:
JSONBase() {}
public:
bool DeserializeFromFile(const std::string& filePath);
bool SerializeToFile(const std::string& filePath);

virtual std::string Serialize() const;

virtual bool Deserialize(const std::string& s) = 0;
virtual bool Deserialize(const std::string& s);
virtual bool Deserialize(const rapidjson::Value& obj) = 0;
virtual bool Serialize(rapidjson::Writer<rapidjson::StringBuffer>* writer) const = 0;
protected:
protected:
bool InitDocument(const std::string & s, rapidjson::Document &doc);
};

Expand Down
13 changes: 1 addition & 12 deletions C++/RapidJSONSample/Product.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,7 @@ namespace JSONModels
Product::~Product()
{}

bool Product::Deserialize(const std::string& s)
{
rapidjson::Document doc;
if (!InitDocument(s, doc))
return false;

Deserialize(doc);

return true;
}


bool Product::Deserialize(const rapidjson::Value & obj)
{
Expand Down Expand Up @@ -50,6 +41,4 @@ namespace JSONModels

return true;
}


}
9 changes: 3 additions & 6 deletions C++/RapidJSONSample/Product.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ namespace JSONModels
class Product : public JSONBase
{
public:
Product();
Product(const Product&) = default;
virtual ~Product();
Product();
virtual ~Product();

virtual bool Deserialize(const std::string& s);
virtual bool Deserialize(const rapidjson::Value& obj);
virtual bool Serialize(rapidjson::Writer<rapidjson::StringBuffer>* writer) const;

Expand All @@ -27,8 +25,7 @@ namespace JSONModels
void Sales(float sales) { _sales = sales; }

int Id() const { return _id; }
void Id(int id) { _id = id; }

void Id(int id) { _id = id; }
private:
std::string _name;
std::string _category;
Expand Down
11 changes: 6 additions & 5 deletions C++/RapidJSONSample/Products.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ namespace JSONModels
{
class Products : public JSONBase
{
public:
Products() {};
public:
virtual ~Products() {};
virtual bool Deserialize(const std::string& s);
virtual bool Deserialize(const rapidjson::Value& obj) { return false; };
virtual bool Serialize(rapidjson::Writer<rapidjson::StringBuffer>* writer) const;
virtual bool Deserialize(const std::string& s);

// Getters/Setters.
std::list<Product>& ProductsList() { return _products; }
public:
virtual bool Deserialize(const rapidjson::Value& obj) { return false; };
virtual bool Serialize(rapidjson::Writer<rapidjson::StringBuffer>* writer) const;
private:
std::list<Product> _products;
};
Expand Down
21 changes: 20 additions & 1 deletion C++/RapidJSONSample/RapidJSONSample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,40 @@
#include "Products.h"

int main()
{
{
// load json
JSONModels::Product product;
product.DeserializeFromFile("DataSample.json");

// print some value.
printf("Name:%s, Sales:%.3f", product.Name().c_str(), product.Sales());

// increase the sales.
float newSales = product.Sales() + 100;
product.Sales(product.Sales());

// save json to new file.
product.SerializeToFile("DataSampleNew.json");

// load json array
JSONModels::Products products;
products.DeserializeFromFile("DataSampleArray.json");

for (std::list<JSONModels::Product>::const_iterator it = products.ProductsList().begin();
it != products.ProductsList().end(); it++)
{
// print some values.
printf("Name:%s, Sales:%.3f", (*it).Name().c_str(), (*it).Sales());
}

// add new product
JSONModels::Product newProduct;
newProduct.Id(101);
newProduct.Name("Global Value Mid-Back Manager's Chair, Gray");
newProduct.Category("Furniture");
newProduct.Sales(213.115f);
products.ProductsList().push_back(product);

// save to new array file.
products.SerializeToFile("DataSampleArrayNew.json");
}

0 comments on commit 56e7d96

Please sign in to comment.