-
Notifications
You must be signed in to change notification settings - Fork 0
/
JSONDataObject.cpp
57 lines (47 loc) · 1.44 KB
/
JSONDataObject.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Eli Simmonds
// Project 3
// JSONDataObject.cpp
#include "JSONDataObject.hpp"
JSONDataObject::JSONDataObject() {
_listOfDataItems = new std::vector<JSONDataItem *>; // use pointer or physical vector?
}
JSONDataObject::~JSONDataObject() {
for (auto it = _listOfDataItems->begin(); it != _listOfDataItems->end(); it++) {
delete (*it);
}
// std::cout << "JSONDataObject: Destructor called" << std::endl;
delete _listOfDataItems;
}
void JSONDataObject::parseFromJSONstream(std::fstream &stream) {
char c;
if (!(stream >> c) || c != '{')
exit(1);
do {
JSONDataItem *a = new JSONDataItem();
a->parseJSONItem(stream);
_listOfDataItems->push_back(a);
stream >> c;
} while (c != '}');
}
void JSONDataObject::print() {
if (_listOfDataItems->empty())
std::cout << "Vector is empty" << std::endl;
std::cout << "JSONDataObject: print()" << std::endl;
for(auto it = _listOfDataItems->begin(); it!=_listOfDataItems->end(); it++){
(*it)->print();
}
}
std::string JSONDataObject::valueForStringAttribute(std::string s) {
for(auto it = _listOfDataItems->begin(); it!=_listOfDataItems->end(); it++){
if ((*it)->attribute() == s && !(*it)->isNumber())
return (*it)->stringValue();
}
return "unknown";
}
int JSONDataObject::valueForIntegerAttribute(std::string s) {
for(auto it = _listOfDataItems->begin(); it!=_listOfDataItems->end(); it++){
if ((*it)->attribute() == s && (*it)->isNumber())
return (*it)->integerValue();
}
return 0;
}