Let's say I have a REST service which returns a JSON object with the following scheme:
{
"id" : 1
"name" : "Peter"
"age" : 25
}
I have an application written in C++ which receives this object and deserializes for later use. I need a data structure to store the object.
I can implement that using either a struct:
struct person
{
int id;
string name;
int age;
};
and having a helper function to initialize the struct:
// Returns a person deserialized from a JSON object
person fromJson(JsonObject obj);
// Usage:
auto personInfo = fromJson(/* JSON object from REST service */);
Or having a class with getters, setters and a constructor which takes a JsonObject as a parameter and does the deserialization by itself:
class person
{
public:
// Deserialized a JSON object and initializes the private fields
person(JsonObject obj);
int id() const;
string name() const;
int age() const;
void setId(int id);
void setName(string name);
void setAge(int age);
private:
int _id;
int _name;
int _age;
};
// Usage:
person personInfo{ /* JSON object from REST service */ };
Given that the data will be stored on a client machine, displayed, possibly edited then sent back to the REST service, which implementation would be more suitable to use? Since I would use the data structure only to store data(using the setters/getters shouldn't trigger anything else than setting/getting the value) I can't really think of any benefits of one over another other than personal preference.
Aucun commentaire:
Enregistrer un commentaire