Adobe Interview Question
Country: India
//Json object using composite pattern
class IJsonObject {
public:
virtual void print() = 0;
};
class IJsonObjects : public IJsonObject {
public:
vector<IJsonObject*> objects;
string str_key;
IJsonObjects() : str_key("") {
}
IJsonObjects(string str) : str_key(str) {
}
void print() {
if (str_key.length() > 0) {
cout << "\"" << str_key << "\" : {";
}
for (auto elem : objects) {
elem->print();
}
if (str_key.length() > 0) {
cout << "}";
}
}
};
class IJsonInt : public IJsonObject {
public:
int val;
string str_key;
IJsonInt(string str, int v) : str_key(str), val(v) {
}
void print() {
cout << "\"" << str_key << "\" : " << val << endl;
}
};
class IJsonStr : public IJsonObject {
public:
string str_val;
string str_key;
IJsonStr(string str, string v) : str_key(str), str_val(v) {
}
void print() {
cout << "\"" << str_key << "\" : \"" << str_val << "\"" << endl;
}
};
void process_json_str() {
cout << "\n\n Json block: " << endl;
IJsonObjects* json_root = new IJsonObjects;
IJsonObject * json_obj1 = new IJsonInt { "version", 10 };
json_root->objects.push_back(json_obj1);
IJsonObject* json_obj2 = new IJsonStr{ "OS", "Windows 10" };
IJsonObject* json_obj3 = new IJsonInt{ "Build number", 6093 };
json_root->objects.push_back(json_obj2);
json_root->objects.push_back(json_obj3);
IJsonObjects* json_subroot = new IJsonObjects{"Customer Details"};
IJsonObject* json_obj4 = new IJsonInt{ "Age", 30 };
IJsonObject* json_obj5 = new IJsonStr{ "name", "Kunal" };
json_subroot->objects.push_back(json_obj4);
json_subroot->objects.push_back(json_obj5);
json_root->objects.push_back(json_subroot);
IJsonObjects* json_subroot2 = new IJsonObjects{ "Contract Details" };
IJsonObject* json_obj6 = new IJsonInt{ "Cost per licence", 15 };
IJsonObject* json_obj7 = new IJsonStr{ "Licence", "3 Years" };
json_subroot2->objects.push_back(json_obj6);
json_subroot2->objects.push_back(json_obj7);
json_subroot->objects.push_back(json_subroot2);
json_root->print();
return;
}
//Json object using composite pattern
class IJsonObject {
public:
virtual void print() = 0;
};
class IJsonObjects : public IJsonObject {
string str_key;
string str_indent;
public:
vector<IJsonObject*> objects;
IJsonObjects() : str_key("") , str_indent("") {
}
IJsonObjects(string str) : str_key(str), str_indent(" ") {
}
void print() {
if (str_key.length() > 0) {
cout << str_indent << "\"" << str_key << "\" : {";
}
for (auto elem : objects) {
cout << str_indent;
elem->print();
}
if (str_key.length() > 0) {
cout << str_indent << "}" << endl;
}
}
};
class IJsonInt : public IJsonObject {
public:
int val;
string str_key;
IJsonInt(string str, int v) : str_key(str), val(v) {
}
void print() {
cout << "\"" << str_key << "\" : " << val << endl;
}
};
class IJsonStr : public IJsonObject {
public:
string str_val;
string str_key;
IJsonStr(string str, string v) : str_key(str), str_val(v) {
}
void print() {
cout << "\"" << str_key << "\" : \"" << str_val << "\"" << endl;
}
};
void process_json_str() {
cout << "\n\n Json block: " << endl;
IJsonObjects* json_root = new IJsonObjects;
IJsonObject * json_obj1 = new IJsonInt { "version", 10 };
json_root->objects.push_back(json_obj1);
IJsonObject* json_obj2 = new IJsonStr{ "OS", "Windows 10" };
IJsonObject* json_obj3 = new IJsonInt{ "Build number", 6093 };
json_root->objects.push_back(json_obj2);
json_root->objects.push_back(json_obj3);
IJsonObjects* json_subroot = new IJsonObjects{"Customer Details"};
IJsonObject* json_obj4 = new IJsonInt{ "Age", 30 };
IJsonObject* json_obj5 = new IJsonStr{ "name", "Kunal" };
json_subroot->objects.push_back(json_obj4);
json_subroot->objects.push_back(json_obj5);
json_root->objects.push_back(json_subroot);
IJsonObjects* json_subroot2 = new IJsonObjects{ "Contract Details" };
IJsonObject* json_obj6 = new IJsonInt{ "Cost per licence", 15 };
IJsonObject* json_obj7 = new IJsonStr{ "Licence", "3 Years" };
json_subroot2->objects.push_back(json_obj6);
json_subroot2->objects.push_back(json_obj7);
json_subroot->objects.push_back(json_subroot2);
json_root->print();
return;
}
//Json object using composite pattern
- Kunal Bansal September 30, 2020class IJsonObject {
public:
virtual void print() = 0;
};
class IJsonObjects : public IJsonObject {
public:
vector<IJsonObject*> objects;
string str_key;
IJsonObjects() : str_key("") {
}
IJsonObjects(string str) : str_key(str) {
}
void print() {
if (str_key.length() > 0) {
cout << "\"" << str_key << "\" : {";
}
for (auto elem : objects) {
elem->print();
}
if (str_key.length() > 0) {
cout << "}";
}
}
};
class IJsonInt : public IJsonObject {
public:
int val;
string str_key;
IJsonInt(string str, int v) : str_key(str), val(v) {
}
void print() {
cout << "\"" << str_key << "\" : " << val << endl;
}
};
class IJsonStr : public IJsonObject {
public:
string str_val;
string str_key;
IJsonStr(string str, string v) : str_key(str), str_val(v) {
}
void print() {
cout << "\"" << str_key << "\" : \"" << str_val << "\"" << endl;
}
};
void process_json_str() {
cout << "\n\n Json block: " << endl;
IJsonObjects* json_root = new IJsonObjects;
IJsonObject * json_obj1 = new IJsonInt { "version", 10 };
json_root->objects.push_back(json_obj1);
IJsonObject* json_obj2 = new IJsonStr{ "OS", "Windows 10" };
IJsonObject* json_obj3 = new IJsonInt{ "Build number", 6093 };
json_root->objects.push_back(json_obj2);
json_root->objects.push_back(json_obj3);
IJsonObjects* json_subroot = new IJsonObjects{"Customer Details"};
IJsonObject* json_obj4 = new IJsonInt{ "Age", 30 };
IJsonObject* json_obj5 = new IJsonStr{ "name", "Kunal" };
json_subroot->objects.push_back(json_obj4);
json_subroot->objects.push_back(json_obj5);
json_root->objects.push_back(json_subroot);
IJsonObjects* json_subroot2 = new IJsonObjects{ "Contract Details" };
IJsonObject* json_obj6 = new IJsonInt{ "Cost per licence", 15 };
IJsonObject* json_obj7 = new IJsonStr{ "Licence", "3 Years" };
json_subroot2->objects.push_back(json_obj6);
json_subroot2->objects.push_back(json_obj7);
json_subroot->objects.push_back(json_subroot2);
json_root->print();
return;
}