fc4b101b67ccd3b9942d91006e58d1c7dacb5879
[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 var modifiers = new JsonArray
63 for modifier in collect_modifiers do
64 modifiers.add modifier
65 end
66 obj["modifiers"] = modifiers
67 return obj
68 end
69
70 redef fun to_json do return json.to_json
71 end
72
73 redef class MDoc
74 super Jsonable
75
76 # Return `self` as a JsonObject.
77 fun json: JsonObject do
78 var obj = new JsonObject
79 obj["content"] = content.join("\n")
80 obj["location"] = location
81 return obj
82 end
83
84 redef fun to_json do return json.to_json
85 end
86
87 redef class Location
88 super Jsonable
89
90 # Return `self` as a JsonObject.
91 fun json: JsonObject do
92 var obj = new JsonObject
93 obj["column_end"] = column_end
94 obj["column_start"] = column_start
95 obj["line_end"] = line_end
96 obj["line_start"] = line_start
97 var file = self.file
98 if file != null then
99 obj["file"] = file.filename
100 end
101 return obj
102 end
103
104 redef fun to_json do return json.to_json
105 end
106
107 redef class MVisibility
108 super Jsonable
109
110 redef fun to_json do return to_s.to_json
111 end
112
113 redef class MPackage
114
115 redef fun json do
116 var obj = super
117 obj["visibility"] = public_visibility
118 if ini != null then
119 obj["ini"] = new JsonObject.from(ini.as(not null).to_map)
120 end
121 obj["root"] = to_mentity_ref(root)
122 obj["mgroups"] = to_mentity_refs(mgroups)
123 return obj
124 end
125 end
126
127 redef class MGroup
128 redef fun json do
129 var obj = super
130 obj["visibility"] = public_visibility
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["location"] = location
145 obj["visibility"] = public_visibility
146 obj["mpackage"] = to_mentity_ref(mpackage)
147 obj["mgroup"] = to_mentity_ref(mgroup)
148 obj["intro_mclasses"] = to_mentity_refs(intro_mclasses)
149 obj["mclassdefs"] = to_mentity_refs(mclassdefs)
150 return obj
151 end
152 end
153
154 redef class MClass
155 redef fun json do
156 var obj = super
157 obj["visibility"] = visibility
158 var arr = new JsonArray
159 for mparameter in mparameters do arr.add mparameter
160 obj["mparameters"] = arr
161 obj["intro"] = to_mentity_ref(intro)
162 obj["intro_mmodule"] = to_mentity_ref(intro_mmodule)
163 obj["mpackage"] = to_mentity_ref(intro_mmodule.mpackage)
164 obj["mclassdefs"] = to_mentity_refs(mclassdefs)
165 return obj
166 end
167 end
168
169 redef class MClassDef
170 redef fun json do
171 var obj = super
172 obj["visibility"] = mclass.visibility
173 obj["location"] = location
174 obj["is_intro"] = is_intro
175 var arr = new JsonArray
176 for mparameter in mclass.mparameters do arr.add mparameter
177 obj["mparameters"] = arr
178 obj["mmodule"] = to_mentity_ref(mmodule)
179 obj["mclass"] = to_mentity_ref(mclass)
180 obj["mpropdefs"] = to_mentity_refs(mpropdefs)
181 obj["intro_mproperties"] = to_mentity_refs(intro_mproperties)
182 return obj
183 end
184 end
185
186 redef class MProperty
187 redef fun json do
188 var obj = super
189 obj["visibility"] = visibility
190 obj["intro"] = to_mentity_ref(intro)
191 obj["intro_mclassdef"] = to_mentity_ref(intro_mclassdef)
192 obj["mpropdefs"] = to_mentity_refs(mpropdefs)
193 return obj
194 end
195 end
196
197 redef class MMethod
198 redef fun json do
199 var obj = super
200 obj["is_init"] = is_init
201 obj["msignature"] = intro.msignature
202 return obj
203 end
204 end
205
206 redef class MAttribute
207 redef fun json do
208 var obj = super
209 obj["static_mtype"] = to_mentity_ref(intro.static_mtype)
210 return obj
211 end
212 end
213
214 redef class MVirtualTypeProp
215 redef fun json do
216 var obj = super
217 obj["mvirtualtype"] = to_mentity_ref(mvirtualtype)
218 obj["bound"] = to_mentity_ref(intro.bound)
219 return obj
220 end
221 end
222
223 redef class MPropDef
224 redef fun json do
225 var obj = super
226 obj["visibility"] = mproperty.visibility
227 obj["location"] = location
228 obj["is_intro"] = is_intro
229 obj["mclassdef"] = to_mentity_ref(mclassdef)
230 obj["mproperty"] = to_mentity_ref(mproperty)
231 return obj
232 end
233 end
234
235 redef class MMethodDef
236 redef fun json do
237 var obj = super
238 obj["msignature"] = msignature
239 return obj
240 end
241 end
242
243 redef class MAttributeDef
244 redef fun json do
245 var obj = super
246 obj["static_mtype"] = to_mentity_ref(static_mtype)
247 return obj
248 end
249 end
250
251 redef class MVirtualTypeDef
252 redef fun json do
253 var obj = super
254 obj["bound"] = to_mentity_ref(bound)
255 obj["is_fixed"] = is_fixed
256 return obj
257 end
258 end
259
260 redef class MSignature
261 redef fun json do
262 var obj = new JsonObject
263 obj["arity"] = arity
264 var arr = new JsonArray
265 for mparam in mparameters do arr.add mparam
266 obj["mparams"] = arr
267 obj["return_mtype"] = to_mentity_ref(return_mtype)
268 obj["vararg_rank"] = vararg_rank
269 return obj
270 end
271 end
272
273 redef class MParameterType
274 redef fun json do
275 var obj = new JsonObject
276 obj["name"] = name
277 obj["rank"] = rank
278 obj["mtype"] = to_mentity_ref(mclass.intro.bound_mtype.arguments[rank])
279 return obj
280 end
281 end
282
283 redef class MParameter
284 redef fun json do
285 var obj = new JsonObject
286 obj["is_vararg"] = is_vararg
287 obj["name"] = name
288 obj["mtype"] = to_mentity_ref(mtype)
289 return obj
290 end
291 end
292
293 # Create a ref to a `mentity`.
294 fun to_mentity_ref(mentity: nullable MEntity): nullable MEntityRef do
295 if mentity == null then return null
296 return new MEntityRef(mentity)
297 end
298
299 # Return a collection of `mentities` as a JsonArray of MEntityRefs.
300 fun to_mentity_refs(mentities: Collection[MEntity]): JsonArray do
301 var array = new JsonArray
302 for mentity in mentities do array.add to_mentity_ref(mentity)
303 return array
304 end