Fortunately, C++ offers no simple way of blurring the line between network communication content and the source code of your program. Something like all those terrible reflection-based JSON libraries out there cannot happen to C++. Coincidentally, while I cannot speak for C#, reflection-based JSON libraries in Java are a huge violation of best practices outlined in Joshua Bloch's renowned Effective Java book. See Item 53, Prefer interfaces to reflection:
As a rule, objects should not be accessed reflectively in normal
applications at runtime
In C++, we do not need such a guideline because there is no reflection. That's mostly a good thing.
Now, that's not to say that you have to write your own JSON parser, of course. While JSON parsing is not part of the standard library, 3rd-party alternatives exist. For example, have a look at JSON for Modern C++.
Whatever library you use, you will have to explicitly read from and write to your class members in one way or the other.
That is, if you have the following class (which more or less matches the C# example you linked):
struct Product
{
std::string name;
int price;
std::vector<std::string> sizes;
};
Then with the aforementioned library, you'd have to turn a Product
object into JSON like this:
json j = {
{ "name", product.name }, // explicit relationship between "name" and "name"
{ "price", product.price }, // explicit relationship between "price" and "price"
{ "sizes", product.sizes } // explicit relationship between "sizes" and "sizes"
};
std::cout << j << "\n";
And reading:
json j;
std::cin >> j;
Product product;
product.name = j["name"]; // explicit relationship between "name" and "name"
product.price = j["price"]; // explicit relationship between "price" and "price"
product.sizes = j["sizes"]; // explicit relationship between "sizes" and "sizes"
Such an approach is also called non-intrusive serialisation/deserialisation. An intrusive approach, on the other hand, with member or friend
functions, would be the only way to go if you need access to private member variables.
Things to keep in mind:
- Run-time reflection is dangerous and error-prone in languages which support it.
- C++ has no run-time reflection.
- C++ encourages you to cleanly separate two concerns: (1) Parsing of JSON, (2) Serialisation/deserialisation of a class.
- Free 3rd-party libraries for JSON parsing exist.