1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # Make model entities Jsonable.
17 # To avoid cycles, every reference from a MEntity to another is replaced by a
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.
24 # TODO consider serialization module?
27 import model
::model_collect
31 # A reference to another mentity.
38 # Return `self` as a Json Object.
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.
43 var obj
= new JsonObject
44 obj
["full_name"] = mentity
.full_name
52 # Return `self` as a JsonObject.
54 # By default, every reference to another MEntity is skipped.
55 # Use full_json for a full representation.
56 fun json
: JsonObject do
57 var obj
= new JsonObject
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
67 obj
["modifiers"] = modifiers
71 redef fun to_json
do return json
.to_json
73 # Return `self` as a JsonObject with references.
75 # By default, every reference to another MEntity is replaced by a pointer
76 # to the MEntity::json_id.
77 fun full_json
: JsonObject do
79 obj
["location"] = location
83 # Return `full_json` as a json string.
84 fun to_full_json
: String do return full_json
.to_json
90 # Return `self` as a JsonObject.
91 fun json
: JsonObject do
92 var obj
= new JsonObject
93 obj
["content"] = content
.join
("\n")
94 obj
["location"] = location
98 redef fun to_json
do return json
.to_json
104 # Return `self` as a JsonObject.
105 fun json
: JsonObject do
106 var obj
= new JsonObject
107 obj
["column_end"] = column_end
108 obj
["column_start"] = column_start
109 obj
["line_end"] = line_end
110 obj
["line_start"] = line_start
113 obj
["file"] = file
.filename
118 redef fun to_json
do return json
.to_json
121 redef class MVisibility
124 redef fun to_json
do return to_s
.to_json
129 redef fun full_json
do
132 obj
["ini"] = new JsonObject.from
(ini
.as(not null).to_map
)
134 obj
["root"] = to_mentity_ref
(root
)
135 obj
["mgroups"] = to_mentity_refs
(mgroups
)
143 obj
["is_root"] = is_root
147 redef fun full_json
do
149 obj
["mpackage"] = to_mentity_ref
(mpackage
)
150 obj
["default_mmodule"] = to_mentity_ref
(default_mmodule
)
151 obj
["parent"] = to_mentity_ref
(parent
)
152 obj
["mmodules"] = to_mentity_refs
(mmodules
)
153 obj
["mgroups"] = to_mentity_refs
(in_nesting
.direct_smallers
)
159 redef fun full_json
do
161 obj
["mpackage"] = to_mentity_ref
(mpackage
)
162 obj
["mgroup"] = to_mentity_ref
(mgroup
)
163 obj
["intro_mclasses"] = to_mentity_refs
(intro_mclasses
)
164 obj
["mclassdefs"] = to_mentity_refs
(mclassdefs
)
165 obj
["intro_mclassdefs"] = to_mentity_refs
(collect_intro_mclassdefs
(private_view
))
166 obj
["redef_mclassdefs"] = to_mentity_refs
(collect_redef_mclassdefs
(private_view
))
167 obj
["imports"] = to_mentity_refs
(in_importation
.direct_greaters
)
175 obj
["mparameters"] = new JsonArray.from
(mparameters
)
179 redef fun full_json
do
181 obj
["intro"] = to_mentity_ref
(intro
)
182 obj
["intro_mmodule"] = to_mentity_ref
(intro_mmodule
)
183 obj
["mpackage"] = to_mentity_ref
(intro_mmodule
.mpackage
)
184 obj
["mclassdefs"] = to_mentity_refs
(mclassdefs
)
185 obj
["all_mproperties"] = to_mentity_refs
(collect_accessible_mproperties
(private_view
))
186 obj
["intro_mproperties"] = to_mentity_refs
(collect_intro_mproperties
(private_view
))
187 obj
["redef_mproperties"] = to_mentity_refs
(collect_redef_mproperties
(private_view
))
188 obj
["parents"] = to_mentity_refs
(collect_parents
(private_view
))
193 redef class MClassDef
196 obj
["is_intro"] = is_intro
197 obj
["mparameters"] = new JsonArray.from
(mclass
.mparameters
)
201 redef fun full_json
do
203 obj
["mmodule"] = to_mentity_ref
(mmodule
)
204 obj
["mclass"] = to_mentity_ref
(mclass
)
205 obj
["mpropdefs"] = to_mentity_refs
(mpropdefs
)
206 obj
["intro_mproperties"] = to_mentity_refs
(intro_mproperties
)
207 obj
["intro"] = to_mentity_ref
(mclass
.intro
)
208 obj
["mpackage"] = to_mentity_ref
(mmodule
.mpackage
)
209 obj
["intro_mpropdefs"] = to_mentity_refs
(collect_intro_mpropdefs
(private_view
))
210 obj
["redef_mpropdefs"] = to_mentity_refs
(collect_redef_mpropdefs
(private_view
))
215 redef class MProperty
216 redef fun full_json
do
218 obj
["intro"] = to_mentity_ref
(intro
)
219 obj
["intro_mclassdef"] = to_mentity_ref
(intro_mclassdef
)
220 obj
["mpropdefs"] = to_mentity_refs
(mpropdefs
)
221 obj
["intro_mclass"] = to_mentity_ref
(intro_mclassdef
.mclass
)
222 obj
["mpackage"] = to_mentity_ref
(intro_mclassdef
.mmodule
.mpackage
)
230 obj
["is_init"] = is_init
231 obj
["msignature"] = intro
.msignature
236 redef class MAttribute
239 obj
["static_mtype"] = to_mentity_ref
(intro
.static_mtype
)
244 redef class MVirtualTypeProp
247 obj
["mvirtualtype"] = to_mentity_ref
(mvirtualtype
)
248 obj
["bound"] = to_mentity_ref
(intro
.bound
)
256 obj
["is_intro"] = is_intro
260 redef fun full_json
do
262 obj
["mclassdef"] = to_mentity_ref
(mclassdef
)
263 obj
["mproperty"] = to_mentity_ref
(mproperty
)
264 obj
["intro"] = to_mentity_ref
(mproperty
.intro
)
265 obj
["intro_mclassdef"] = to_mentity_ref
(mproperty
.intro
.mclassdef
)
266 obj
["mmodule"] = to_mentity_ref
(mclassdef
.mmodule
)
267 obj
["mgroup"] = to_mentity_ref
(mclassdef
.mmodule
.mgroup
)
268 obj
["mpackage"] = to_mentity_ref
(mclassdef
.mmodule
.mpackage
)
273 redef class MMethodDef
276 obj
["msignature"] = msignature
281 redef class MAttributeDef
284 obj
["static_mtype"] = to_mentity_ref
(static_mtype
)
289 redef class MVirtualTypeDef
292 obj
["bound"] = to_mentity_ref
(bound
)
293 obj
["is_fixed"] = is_fixed
298 redef class MSignature
300 var obj
= new JsonObject
302 var arr
= new JsonArray
303 for mparam
in mparameters
do arr
.add mparam
305 obj
["return_mtype"] = to_mentity_ref
(return_mtype
)
306 obj
["vararg_rank"] = vararg_rank
311 redef class MParameterType
313 var obj
= new JsonObject
316 obj
["mtype"] = to_mentity_ref
(mclass
.intro
.bound_mtype
.arguments
[rank
])
321 redef class MParameter
323 var obj
= new JsonObject
324 obj
["is_vararg"] = is_vararg
326 obj
["mtype"] = to_mentity_ref
(mtype
)
331 # Create a ref to a `mentity`.
332 fun to_mentity_ref
(mentity
: nullable MEntity): nullable MEntityRef do
333 if mentity
== null then return null
334 return new MEntityRef(mentity
)
337 # Return a collection of `mentities` as a JsonArray of MEntityRefs.
338 fun to_mentity_refs
(mentities
: Collection[MEntity]): JsonArray do
339 var array
= new JsonArray
340 for mentity
in mentities
do array
.add to_mentity_ref
(mentity
)