cebc7beb36bafca7defd81435e3256800c82a215
[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 var modifiers = new JsonArray
64 for modifier in collect_modifiers do
65 modifiers.add modifier
66 end
67 obj["modifiers"] = modifiers
68 return obj
69 end
70
71 redef fun to_json do return json.to_json
72 end
73
74 redef class MDoc
75 super Jsonable
76
77 # Return `self` as a JsonObject.
78 fun json: JsonObject do
79 var obj = new JsonObject
80 obj["content"] = content.join("\n")
81 obj["location"] = location
82 return obj
83 end
84
85 redef fun to_json do return json.to_json
86 end
87
88 redef class Location
89 super Jsonable
90
91 # Return `self` as a JsonObject.
92 fun json: JsonObject do
93 var obj = new JsonObject
94 obj["column_end"] = column_end
95 obj["column_start"] = column_start
96 obj["line_end"] = line_end
97 obj["line_start"] = line_start
98 var file = self.file
99 if file != null then
100 obj["file"] = file.filename
101 end
102 return obj
103 end
104
105 redef fun to_json do return json.to_json
106 end
107
108 redef class MVisibility
109 super Jsonable
110
111 redef fun to_json do return to_s.to_json
112 end
113
114 redef class MPackage
115
116 redef fun json do
117 var obj = super
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["is_root"] = is_root
131 obj["mpackage"] = to_mentity_ref(mpackage)
132 obj["default_mmodule"] = to_mentity_ref(default_mmodule)
133 obj["parent"] = to_mentity_ref(parent)
134 obj["mmodules"] = to_mentity_refs(mmodules)
135 obj["mgroups"] = to_mentity_refs(in_nesting.direct_smallers)
136 return obj
137 end
138 end
139
140 redef class MModule
141 redef fun json do
142 var obj = super
143 obj["location"] = location
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["location"] = location
170 obj["is_intro"] = is_intro
171 var arr = new JsonArray
172 for mparameter in mclass.mparameters do arr.add mparameter
173 obj["mparameters"] = arr
174 obj["mmodule"] = to_mentity_ref(mmodule)
175 obj["mclass"] = to_mentity_ref(mclass)
176 obj["mpropdefs"] = to_mentity_refs(mpropdefs)
177 obj["intro_mproperties"] = to_mentity_refs(intro_mproperties)
178 return obj
179 end
180 end
181
182 redef class MProperty
183 redef fun json do
184 var obj = super
185 obj["intro"] = to_mentity_ref(intro)
186 obj["intro_mclassdef"] = to_mentity_ref(intro_mclassdef)
187 obj["mpropdefs"] = to_mentity_refs(mpropdefs)
188 return obj
189 end
190 end
191
192 redef class MMethod
193 redef fun json do
194 var obj = super
195 obj["is_init"] = is_init
196 obj["msignature"] = intro.msignature
197 return obj
198 end
199 end
200
201 redef class MAttribute
202 redef fun json do
203 var obj = super
204 obj["static_mtype"] = to_mentity_ref(intro.static_mtype)
205 return obj
206 end
207 end
208
209 redef class MVirtualTypeProp
210 redef fun json do
211 var obj = super
212 obj["mvirtualtype"] = to_mentity_ref(mvirtualtype)
213 obj["bound"] = to_mentity_ref(intro.bound)
214 return obj
215 end
216 end
217
218 redef class MPropDef
219 redef fun json do
220 var obj = super
221 obj["location"] = location
222 obj["is_intro"] = is_intro
223 obj["mclassdef"] = to_mentity_ref(mclassdef)
224 obj["mproperty"] = to_mentity_ref(mproperty)
225 return obj
226 end
227 end
228
229 redef class MMethodDef
230 redef fun json do
231 var obj = super
232 obj["msignature"] = msignature
233 return obj
234 end
235 end
236
237 redef class MAttributeDef
238 redef fun json do
239 var obj = super
240 obj["static_mtype"] = to_mentity_ref(static_mtype)
241 return obj
242 end
243 end
244
245 redef class MVirtualTypeDef
246 redef fun json do
247 var obj = super
248 obj["bound"] = to_mentity_ref(bound)
249 obj["is_fixed"] = is_fixed
250 return obj
251 end
252 end
253
254 redef class MSignature
255 redef fun json do
256 var obj = new JsonObject
257 obj["arity"] = arity
258 var arr = new JsonArray
259 for mparam in mparameters do arr.add mparam
260 obj["mparams"] = arr
261 obj["return_mtype"] = to_mentity_ref(return_mtype)
262 obj["vararg_rank"] = vararg_rank
263 return obj
264 end
265 end
266
267 redef class MParameterType
268 redef fun json do
269 var obj = new JsonObject
270 obj["name"] = name
271 obj["rank"] = rank
272 obj["mtype"] = to_mentity_ref(mclass.intro.bound_mtype.arguments[rank])
273 return obj
274 end
275 end
276
277 redef class MParameter
278 redef fun json do
279 var obj = new JsonObject
280 obj["is_vararg"] = is_vararg
281 obj["name"] = name
282 obj["mtype"] = to_mentity_ref(mtype)
283 return obj
284 end
285 end
286
287 # Create a ref to a `mentity`.
288 fun to_mentity_ref(mentity: nullable MEntity): nullable MEntityRef do
289 if mentity == null then return null
290 return new MEntityRef(mentity)
291 end
292
293 # Return a collection of `mentities` as a JsonArray of MEntityRefs.
294 fun to_mentity_refs(mentities: Collection[MEntity]): JsonArray do
295 var array = new JsonArray
296 for mentity in mentities do array.add to_mentity_ref(mentity)
297 return array
298 end