99import json
1010from typing import TYPE_CHECKING
1111
12+ from pylint .interfaces import UNDEFINED
13+ from pylint .message import Message
1214from pylint .reporters .base_reporter import BaseReporter
15+ from pylint .typing import MessageLocationTuple
1316
1417if TYPE_CHECKING :
1518 from pylint .lint .pylinter import PyLinter
@@ -24,24 +27,47 @@ class JSONReporter(BaseReporter):
2427
2528 def display_messages (self , layout : Section | None ) -> None :
2629 """Launch layouts display."""
27- json_dumpable = [
28- {
29- "type" : msg .category ,
30- "module" : msg .module ,
31- "obj" : msg .obj ,
32- "line" : msg .line ,
33- "column" : msg .column ,
34- "endLine" : msg .end_line ,
35- "endColumn" : msg .end_column ,
36- "path" : msg .path ,
37- "symbol" : msg .symbol ,
38- "message" : msg .msg or "" ,
39- "message-id" : msg .msg_id ,
40- }
41- for msg in self .messages
42- ]
30+ json_dumpable = [self .serialize (message ) for message in self .messages ]
4331 print (json .dumps (json_dumpable , indent = 4 ), file = self .out )
4432
33+ @staticmethod
34+ def serialize (message : Message ) -> dict [str , int | str | None ]:
35+ # TODO (3.0) add abspath and confidence
36+ return {
37+ "type" : message .category ,
38+ "module" : message .module ,
39+ "obj" : message .obj ,
40+ "line" : message .line ,
41+ "column" : message .column ,
42+ "endLine" : message .end_line ,
43+ "endColumn" : message .end_column ,
44+ "path" : message .path ,
45+ "symbol" : message .symbol ,
46+ "message" : message .msg or "" ,
47+ "message-id" : message .msg_id ,
48+ }
49+
50+ @staticmethod
51+ def deserialize (message_as_json : dict ) -> Message :
52+ return Message (
53+ msg_id = message_as_json ["message-id" ],
54+ symbol = message_as_json ["symbol" ],
55+ msg = message_as_json ["message" ],
56+ location = MessageLocationTuple (
57+ # TODO (3.0) abspath is not available in json export
58+ abspath = message_as_json ["path" ],
59+ path = message_as_json ["path" ],
60+ module = message_as_json ["module" ],
61+ obj = message_as_json ["obj" ],
62+ line = message_as_json ["line" ],
63+ column = message_as_json ["column" ],
64+ end_line = message_as_json ["endLine" ],
65+ end_column = message_as_json ["endColumn" ],
66+ ),
67+ # TODO (3.0) confidence is not available in json export
68+ confidence = UNDEFINED ,
69+ )
70+
4571 def display_reports (self , layout : Section ) -> None :
4672 """Don't do anything in this reporter."""
4773
0 commit comments