src/model: model_json use MEntity::location
[nit.git] / src / model / model_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 # Make model entities Jsonable.
16 #
17 # To avoid cycles, every reference from a MEntity to another is replaced by a
18 # MEntityRef.
19 #
20 # How subobjects are retrieved using the MEntityRef is the responsability of the
21 # client. Json objects can be returned as this or inflated with concrete objet
22 # rather than the refs.
23 #
24 # TODO consider serialization module?
25 module model_json
26
27 import model::model_collect
28 import json
29 import loader
30
31 # A reference to another mentity.
32 class MEntityRef
33 super MEntity
34
35 # MEntity to link to.
36 var mentity: MEntity
37
38 # Return `self` as a Json Object.
39 #
40 # By default, MEntity references contain only the `full_name` of the Mentity.
41 # You should redefine this method in your client to implement a different behavior.
42 redef fun json do
43 var obj = new JsonObject
44 obj["full_name"] = mentity.full_name
45 return obj
46 end
47 end
48
49 redef class MEntity
50 super Jsonable
51
52 # Return `self` as a JsonObject.
53 #
54 # By default, every reference to another MEntity is replaced by a pointer
55 # to the MEntity::json_id.
56 fun json: JsonObject do
57 var obj = new JsonObject
58 obj["name"] = name
59 obj["class_name"] = class_name
60 obj["full_name"] = full_name
61 obj["mdoc"] = mdoc_or_fallback
62 obj["visibility"] = visibility
63 obj["location"] = location
64 var modifiers = new JsonArray
65 for modifier in collect_modifiers do
66 modifiers.add modifier
67 end
68 obj["modifiers"] = modifiers
69 return obj
70 end
71
72 redef fun to_json do return json.to_json
73 end
74
75 redef class MDoc
76 super Jsonable
77
78 # Return `self` as a JsonObject.
79 fun json: JsonObject do
80 var obj = new JsonObject
81 obj["content"] = content.join("\n")
82 obj["location"] = location
83 return obj
84 end
85
86 redef fun to_json do return json.to_json
87 end
88
89 redef class Location
90 super Jsonable
91
92 # Return `self` as a JsonObject.
93 fun json: JsonObject do
94 var obj = new JsonObject
95 obj["column_end"] = column_end
96 obj["column_start"] = column_start
97 obj["line_end"] = line_end
98 obj["line_start"] = line_start
99 var file = self.file
100 if file != null then
101 obj["file"] = file.filename
102 end
103 return obj
104 end
105
106 redef fun to_json do return json.to_json
107 end
108
109 redef class MVisibility
110 super Jsonable
111
112 redef fun to_json do return to_s.to_json
113 end
114
115 redef class MPackage
116
117 redef fun json do
118 var obj = super
119 if ini != null then
120 obj["ini"] = new JsonObject.from(ini.as(not null).to_map)
121 end
122 obj["root"] = to_mentity_ref(root)
123 obj["mgroups"] = to_mentity_refs(mgroups)
124 return obj
125 end
126 end
127
128 redef class MGroup
129 redef fun json do
130 var obj = super
131 obj["is_root"] = is_root
132 obj["mpackage"] = to_mentity_ref(mpackage)
133 obj["default_mmodule"] = to_mentity_ref(default_mmodule)
134 obj["parent"] = to_mentity_ref(parent)
135 obj["mmodules"] = to_mentity_refs(mmodules)
136 obj["mgroups"] = to_mentity_refs(in_nesting.direct_smallers)
137 return obj
138 end
139 end
140
141 redef class MModule
142 redef fun json do
143 var obj = super
144 obj["mpackage"] = to_mentity_ref(mpackage)
145 obj["mgroup"] = to_mentity_ref(mgroup)
146 obj["intro_mclasses"] = to_mentity_refs(intro_mclasses)
147 obj["mclassdefs"] = to_mentity_refs(mclassdefs)
148 return obj
149 end
150 end
151
152 redef class MClass
153 redef fun json do
154 var obj = super
155 var arr = new JsonArray
156 for mparameter in mparameters do arr.add mparameter
157 obj["mparameters"] = arr
158 obj["intro"] = to_mentity_ref(intro)
159 obj["intro_mmodule"] = to_mentity_ref(intro_mmodule)
160 obj["mpackage"] = to_mentity_ref(intro_mmodule.mpackage)
161 obj["mclassdefs"] = to_mentity_refs(mclassdefs)
162 return obj
163 end
164 end
165
166 redef class MClassDef
167 redef fun json do
168 var obj = super
169 obj["is_intro"] = is_intro
170 var arr = new JsonArray
171 for mparameter in mclass.mparameters do arr.add mparameter
172 obj["mparameters"] = arr
173 obj["mmodule"] = to_mentity_ref(mmodule)
174 obj["mclass"] = to_mentity_ref(mclass)
175 obj["mpropdefs"] = to_mentity_refs(mpropdefs)
176 obj["intro_mproperties"] = to_mentity_refs(intro_mproperties)
177 return obj
178 end
179 end
180
181 redef class MProperty
182 redef fun json do
183 var obj = super
184 obj["intro"] = to_mentity_ref(intro)
185 obj["intro_mclassdef"] = to_mentity_ref(intro_mclassdef)
186 obj["mpropdefs"] = to_mentity_refs(mpropdefs)
187 return obj
188 end
189 end
190
191 redef class MMethod
192 redef fun json do
193 var obj = super
194 obj["is_init"] = is_init
195 obj["msignature"] = intro.msignature
196 return obj
197 end
198 end
199
200 redef class MAttribute
201 redef fun json do
202 var obj = super
203 obj["static_mtype"] = to_mentity_ref(intro.static_mtype)
204 return obj
205 end
206 end
207
208 redef class MVirtualTypeProp
209 redef fun json do
210 var obj = super
211 obj["mvirtualtype"] = to_mentity_ref(mvirtualtype)
212 obj["bound"] = to_mentity_ref(intro.bound)
213 return obj
214 end
215 end
216
217 redef class MPropDef
218 redef fun json do
219 var obj = super
220 obj["is_intro"] = is_intro
221 obj["mclassdef"] = to_mentity_ref(mclassdef)
222 obj["mproperty"] = to_mentity_ref(mproperty)
223 return obj
224 end
225 end
226
227 redef class MMethodDef
228 redef fun json do
229 var obj = super
230 obj["msignature"] = msignature
231 return obj
232 end
233 end
234
235 redef class MAttributeDef
236 redef fun json do
237 var obj = super
238 obj["static_mtype"] = to_mentity_ref(static_mtype)
239 return obj
240 end
241 end
242
243 redef class MVirtualTypeDef
244 redef fun json do
245 var obj = super
246 obj["bound"] = to_mentity_ref(bound)
247 obj["is_fixed"] = is_fixed
248 return obj
249 end
250 end
251
252 redef class MSignature
253 redef fun json do
254 var obj = new JsonObject
255 obj["arity"] = arity
256 var arr = new JsonArray
257 for mparam in mparameters do arr.add mparam
258 obj["mparams"] = arr
259 obj["return_mtype"] = to_mentity_ref(return_mtype)
260 obj["vararg_rank"] = vararg_rank
261 return obj
262 end
263 end
264
265 redef class MParameterType
266 redef fun json do
267 var obj = new JsonObject
268 obj["name"] = name
269 obj["rank"] = rank
270 obj["mtype"] = to_mentity_ref(mclass.intro.bound_mtype.arguments[rank])
271 return obj
272 end
273 end
274
275 redef class MParameter
276 redef fun json do
277 var obj = new JsonObject
278 obj["is_vararg"] = is_vararg
279 obj["name"] = name
280 obj["mtype"] = to_mentity_ref(mtype)
281 return obj
282 end
283 end
284
285 # Create a ref to a `mentity`.
286 fun to_mentity_ref(mentity: nullable MEntity): nullable MEntityRef do
287 if mentity == null then return null
288 return new MEntityRef(mentity)
289 end
290
291 # Return a collection of `mentities` as a JsonArray of MEntityRefs.
292 fun to_mentity_refs(mentities: Collection[MEntity]): JsonArray do
293 var array = new JsonArray
294 for mentity in mentities do array.add to_mentity_ref(mentity)
295 return array
296 end