forked from jenkinsci/analysis-model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonParser.java
52 lines (45 loc) · 1.71 KB
/
JsonParser.java
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
package edu.hm.hafner.analysis.parser;
import java.io.IOException;
import java.io.Reader;
import java.util.Optional;
import java.util.stream.StreamSupport;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import edu.hm.hafner.analysis.ParsingException;
import edu.hm.hafner.analysis.ReaderFactory;
import edu.hm.hafner.analysis.Report;
/**
* Parser report in JSON format as exported by the "Jenkins Warnings Next Generation Plugin".
*
* @author Jeremie Bresson
*/
public class JsonParser extends JsonBaseParser {
private static final long serialVersionUID = -6494117943149352139L;
private static final String ISSUES = "issues";
@Override
public boolean accepts(final ReaderFactory readerFactory) {
return readerFactory.getFileName().endsWith(".json");
}
@Override
public Report parse(final ReaderFactory readerFactory) throws ParsingException {
try (Reader reader = readerFactory.create()) {
JSONObject jsonReport = (JSONObject) new JSONTokener(reader).nextValue();
Report report = new Report();
if (jsonReport.has(ISSUES)) {
JSONArray issues = jsonReport.getJSONArray(ISSUES);
StreamSupport.stream(issues.spliterator(), false)
.filter(o -> o instanceof JSONObject)
.map(o -> convertToIssue((JSONObject) o))
.filter(Optional::isPresent)
.map(Optional::get)
.forEach(report::add);
}
return report;
}
catch (IOException | JSONException e) {
throw new ParsingException(e);
}
}
}