Skip to content

Commit 32f1f24

Browse files
committed
update README
1 parent 2a7e04b commit 32f1f24

File tree

1 file changed

+54
-7
lines changed

1 file changed

+54
-7
lines changed

README.rst

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,65 @@ How to use
2727

2828
.. code-block:: python
2929
30-
import cattrs
31-
import json
32-
import sys
30+
from sarif_om import SarifLog, Run, Tool, ToolComponent, Result, Message
31+
32+
33+
sarif_log = SarifLog(
34+
version="2.1.0",
35+
runs=[
36+
Run(
37+
tool=Tool(
38+
driver=ToolComponent(
39+
name="My Static Analyzer",
40+
version="1.0.0",
41+
)
42+
),
43+
results=[
44+
Result(
45+
rule_id="R001",
46+
message=Message(text="Use of uninitialized variable 'x'"),
47+
)
48+
],
49+
)
50+
],
51+
)
52+
53+
print(sarif_log)
54+
55+
To serialize it to plain dict or JSON:
3356

34-
from sarif_om import SarifLog
57+
.. code-block:: python
58+
59+
from sarif_om import to_dict, to_json
3560
61+
print(to_json(sarif_log))
62+
print(to_dict(sarif_log))
3663
37-
with open(sys.argv[1]) as fp:
38-
data = json.load(fp)
64+
To deserialize dict/JSON data to ``sarif_om.SarifLog``:
3965

40-
sarif_log = cattrs.structure(data, SarifLog)
66+
.. code-block:: python
4167
68+
import json
69+
from sarif_om import from_dict, from_json
70+
71+
72+
sarif_log_dict = {
73+
"runs": [
74+
{
75+
"tool": {"driver": {"name": "My Static Analyzer", "version": "1.0.0"}},
76+
"results": [
77+
{
78+
"message": {"text": "Use of uninitialized variable 'x'"},
79+
"ruleId": "R001",
80+
}
81+
],
82+
}
83+
],
84+
"version": "2.1.0",
85+
}
86+
87+
print(from_dict(sarif_log_dict))
88+
print(from_json(json.dumps(sarif_log_dict)))
4289
4390
Generation
4491
==========

0 commit comments

Comments
 (0)