-
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathOutputSerializer.java
More file actions
48 lines (39 loc) · 1.51 KB
/
OutputSerializer.java
File metadata and controls
48 lines (39 loc) · 1.51 KB
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
package analyzer;
import com.google.gson.*;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.TreeMap;
/**
* Serializer to convert the analyzer output to JSON.
*
* @see <a href="https://exercism.org/docs/building/tooling/analyzers/interface">The analyzer interface in the Exercism documentation</a>
*/
class OutputSerializer {
private static final Gson GSON = new GsonBuilder()
.registerTypeAdapter(Comment.class, new CommentJsonSerializer())
.setPrettyPrinting()
.create();
static String serialize(Output.Analysis analysis) {
return GSON.toJson(analysis);
}
static String serialize(Output.Tags tags) {
return GSON.toJson(tags);
}
private static class CommentJsonSerializer implements JsonSerializer<Comment> {
@Override
public JsonElement serialize(Comment comment, Type type, JsonSerializationContext jsonSerializationContext) {
var json = new JsonObject();
json.addProperty("comment", comment.getKey());
json.add("params", serializeParameters(comment.getParameters()));
if (comment.getType() != null) {
json.addProperty("type", comment.getType().name().toLowerCase());
}
return json;
}
private static JsonElement serializeParameters(Map<String, String> parameters) {
var json = new JsonObject();
new TreeMap<>(parameters).forEach(json::addProperty);
return json;
}
}
}