eb40af4402d839918d85dfeb7fc0bebb20e2b486
[nit.git] / src / doc / commands / commands_json.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Translate command results to json
16 module commands_json
17
18 import commands::commands_model
19 import commands::commands_graph
20 import commands::commands_usage
21 import commands::commands_catalog
22
23 import templates::templates_json
24 import catalog::catalog_json
25
26 redef class DocCommand
27 # Return a JSON Serializable representation of `self` results
28 fun to_json: nullable Serializable is abstract
29 end
30
31 # Message handling
32
33 redef class CmdMessage
34 # Return a JSON Serializable representation of `self`
35 fun to_json: nullable Serializable do
36 var obj = new JsonObject
37 obj["status"] = class_name
38 obj["message"] = to_s
39 return obj
40 end
41 end
42
43 redef class CmdEntity
44 redef fun to_json do return mentity
45 end
46
47 redef class CmdList
48 redef fun to_json do
49 var obj = new JsonObject
50 obj["results"] = results
51 obj["page"] = page
52 obj["count"] = count
53 obj["limit"] = limit
54 obj["max"] = max
55 return obj
56 end
57 end
58
59 redef class CmdEntityList
60 redef fun to_json do return super
61 end
62
63 # Model commands
64
65 redef class CmdComment
66 redef fun to_json do
67 var obj = new JsonObject
68 var render = self.render_comment
69 if render != null then
70 obj["documentation"] = render.write_to_string
71 end
72 return obj
73 end
74 end
75
76 redef class CmdCode
77 redef fun to_json do
78 var obj = new JsonObject
79 var node = self.node
80 if node != null then
81 obj["location"] = node.location
82 end
83 var output = render
84 if output != null then
85 obj["code"] = output.write_to_string
86 end
87 return obj
88 end
89 end
90
91 redef class CmdGraph
92 redef fun to_json do
93 var obj = new JsonObject
94 var output = render
95 if output != null then
96 obj["graph"] = output.write_to_string
97 end
98 return obj
99 end
100 end
101
102 redef class CmdMetadata
103 redef fun to_json do return metadata
104 end
105
106 # CmdCatalog
107
108 redef class CmdCatalogStats
109 redef fun to_json do return stats
110 end
111
112 redef class CmdCatalogTags
113 redef fun to_json do return packages_count_by_tags
114 end
115
116 redef class CmdCatalogTag
117 redef fun to_json do
118 var obj = super.as(JsonObject)
119 obj["tag"] = tag
120 return obj
121 end
122 end
123
124 redef class CmdCatalogPerson
125 redef fun to_json do return person
126 end
127
128 redef class CmdCatalogMaintaining
129 redef fun to_json do
130 var obj = new JsonObject
131 obj["person"] = person
132 obj["results"] = results
133 obj["page"] = page
134 obj["count"] = count
135 obj["limit"] = limit
136 obj["max"] = max
137 return obj
138 end
139 end
140
141 redef class CmdCatalogContributing
142 redef fun to_json do
143 var obj = new JsonObject
144 obj["person"] = person
145 obj["results"] = results
146 obj["page"] = page
147 obj["count"] = count
148 obj["limit"] = limit
149 obj["max"] = max
150 return obj
151 end
152 end