Skip to content

Commit 2bdcdce

Browse files
committed
json-parser: factor out AbstractTreeDecoder
... as an abstraction for higher-level decoders for various JSON-based tree formats No changes in behavior intended by this commit.
1 parent 8d09775 commit 2bdcdce

File tree

1 file changed

+58
-34
lines changed

1 file changed

+58
-34
lines changed

json-parser.cc

Lines changed: 58 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -32,30 +32,53 @@
3232

3333
namespace pt = boost::property_tree;
3434

35+
/// abstraction for higher-level decoders for various JSON-based tree formats
36+
class AbstractTreeDecoder {
37+
public:
38+
virtual ~AbstractTreeDecoder() { }
39+
40+
/// read the given ptree node, decode, and store the result into def
41+
virtual void readNode(Defect *def, const pt::ptree &node) = 0;
42+
};
43+
44+
/// tree decoder of the native JSON format of csdiff
45+
class SimpleTreeDecoder: public AbstractTreeDecoder {
46+
public:
47+
virtual void readNode(Defect *def, const pt::ptree &node);
48+
49+
private:
50+
KeyEventDigger keDigger;
51+
};
52+
3553
struct JsonParser::Private {
3654
const std::string fileName;
3755
const bool silent;
3856
bool jsonValid;
3957
bool hasError;
58+
AbstractTreeDecoder *decoder;
4059
pt::ptree root;
4160
pt::ptree *defList;
4261
pt::ptree::const_iterator defIter;
4362
int defNumber;
4463
TScanProps scanProps;
45-
KeyEventDigger keDigger;
4664

4765
Private(const std::string &fileName_, bool silent_):
4866
fileName(fileName_),
4967
silent(silent_),
5068
jsonValid(false),
5169
hasError(false),
70+
decoder(0),
5271
defNumber(0)
5372
{
5473
}
5574

75+
~Private()
76+
{
77+
delete this->decoder;
78+
}
79+
5680
void parseError(const std::string &msg, unsigned long line = 0UL);
5781
void dataError(const std::string &msg);
58-
void readNode(Defect *def, const pt::ptree &defNode);
5982
bool readNext(Defect *def);
6083
};
6184

@@ -95,6 +118,7 @@ JsonParser::JsonParser(
95118
read_json(input, d->root);
96119

97120
// get the defect list
121+
d->decoder = new SimpleTreeDecoder;
98122
d->defList = &d->root.get_child("defects");
99123
d->defIter = d->defList->begin();
100124
d->jsonValid = true;
@@ -133,7 +157,38 @@ inline T valueOf(const pt::ptree &node, const char *path, const T &defVal)
133157
return opt.get_value_or(defVal);
134158
}
135159

136-
void JsonParser::Private::readNode(
160+
bool JsonParser::Private::readNext(Defect *def) {
161+
try {
162+
// get the current node and move to the next one
163+
const pt::ptree &defNode = this->defIter->second;
164+
this->defIter++;
165+
this->defNumber++;
166+
167+
// read the current node
168+
this->decoder->readNode(def, defNode);
169+
return true;
170+
}
171+
catch (pt::ptree_error &e) {
172+
this->dataError(e.what());
173+
return false;
174+
}
175+
}
176+
177+
bool JsonParser::getNext(Defect *def) {
178+
if (!d->jsonValid)
179+
return false;
180+
181+
// error recovery loop
182+
for (;;) {
183+
if (d->defList->end() == d->defIter)
184+
return false;
185+
186+
if (d->readNext(def))
187+
return true;
188+
}
189+
}
190+
191+
void SimpleTreeDecoder::readNode(
137192
Defect *def,
138193
const pt::ptree &defNode)
139194
{
@@ -191,34 +246,3 @@ void JsonParser::Private::readNode(
191246
// read annotation if available
192247
def->annotation = valueOf<std::string>(defNode, "annotation", "");
193248
}
194-
195-
bool JsonParser::Private::readNext(Defect *def) {
196-
try {
197-
// get the current node and move to the next one
198-
const pt::ptree &defNode = this->defIter->second;
199-
this->defIter++;
200-
this->defNumber++;
201-
202-
// read the current node
203-
this->readNode(def, defNode);
204-
return true;
205-
}
206-
catch (pt::ptree_error &e) {
207-
this->dataError(e.what());
208-
return false;
209-
}
210-
}
211-
212-
bool JsonParser::getNext(Defect *def) {
213-
if (!d->jsonValid)
214-
return false;
215-
216-
// error recovery loop
217-
for (;;) {
218-
if (d->defList->end() == d->defIter)
219-
return false;
220-
221-
if (d->readNext(def))
222-
return true;
223-
}
224-
}

0 commit comments

Comments
 (0)