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
32 # A reference to another mentity.
39 # Return `self` as a Json Object.
41 # By default, MEntity references contain only the `full_name` of the Mentity.
42 # You should redefine this method in your client to implement a different behavior.
44 var obj
= new JsonObject
45 obj
["full_name"] = mentity
.full_name
53 # Return `self` as a JsonObject.
55 # By default, every reference to another MEntity is skipped.
56 # Use full_json for a full representation.
57 fun json
: JsonObject do
58 var obj
= new JsonObject
60 obj
["class_name"] = class_name
61 obj
["full_name"] = full_name
62 obj
["mdoc"] = mdoc_or_fallback
63 obj
["visibility"] = visibility
64 var modifiers
= new JsonArray
65 for modifier
in collect_modifiers
do
66 modifiers
.add modifier
68 obj
["modifiers"] = modifiers
72 redef fun serialize_to
(v
) do json
.serialize_to
(v
)
74 # Return `self` as a JsonObject with references.
76 # By default, every reference to another MEntity is replaced by a pointer
77 # to the MEntity::json_id.
78 fun full_json
: JsonObject do
80 obj
["location"] = location
84 # Return `full_json` as a json string.
85 fun to_full_json
: String do return full_json
.to_json
91 # Return `self` as a JsonObject.
92 fun json
: JsonObject do
93 var obj
= new JsonObject
94 obj
["content"] = content
.join
("\n")
95 obj
["location"] = location
99 redef fun serialize_to
(v
) do json
.serialize_to
(v
)
105 # Return `self` as a JsonObject.
106 fun json
: JsonObject do
107 var obj
= new JsonObject
108 obj
["column_end"] = column_end
109 obj
["column_start"] = column_start
110 obj
["line_end"] = line_end
111 obj
["line_start"] = line_start
114 obj
["file"] = file
.filename
119 redef fun serialize_to
(v
) do json
.serialize_to
(v
)
122 redef class MVisibility
125 redef fun serialize_to
(v
) do to_s
.serialize_to
(v
)
130 redef fun full_json
do
133 obj
["ini"] = new JsonObject.from
(ini
.as(not null).to_map
)
135 obj
["root"] = to_mentity_ref
(root
)
136 obj
["mgroups"] = to_mentity_refs
(mgroups
)
144 obj
["is_root"] = is_root
148 redef fun full_json
do
150 obj
["mpackage"] = to_mentity_ref
(mpackage
)
151 obj
["default_mmodule"] = to_mentity_ref
(default_mmodule
)
152 obj
["parent"] = to_mentity_ref
(parent
)
153 obj
["mmodules"] = to_mentity_refs
(mmodules
)
154 obj
["mgroups"] = to_mentity_refs
(in_nesting
.direct_smallers
)
160 redef fun full_json
do
162 obj
["mpackage"] = to_mentity_ref
(mpackage
)
163 obj
["mgroup"] = to_mentity_ref
(mgroup
)
164 obj
["intro_mclasses"] = to_mentity_refs
(intro_mclasses
)
165 obj
["mclassdefs"] = to_mentity_refs
(mclassdefs
)
166 obj
["intro_mclassdefs"] = to_mentity_refs
(collect_intro_mclassdefs
(private_view
))
167 obj
["redef_mclassdefs"] = to_mentity_refs
(collect_redef_mclassdefs
(private_view
))
168 obj
["imports"] = to_mentity_refs
(in_importation
.direct_greaters
)
176 obj
["mparameters"] = new JsonArray.from
(mparameters
)
180 redef fun full_json
do
182 obj
["intro"] = to_mentity_ref
(intro
)
183 obj
["intro_mmodule"] = to_mentity_ref
(intro_mmodule
)
184 obj
["mpackage"] = to_mentity_ref
(intro_mmodule
.mpackage
)
185 obj
["mclassdefs"] = to_mentity_refs
(mclassdefs
)
186 obj
["all_mproperties"] = to_mentity_refs
(collect_accessible_mproperties
(private_view
))
187 obj
["intro_mproperties"] = to_mentity_refs
(collect_intro_mproperties
(private_view
))
188 obj
["redef_mproperties"] = to_mentity_refs
(collect_redef_mproperties
(private_view
))
189 obj
["parents"] = to_mentity_refs
(collect_parents
(private_view
))
194 redef class MClassDef
197 obj
["is_intro"] = is_intro
198 obj
["mparameters"] = new JsonArray.from
(mclass
.mparameters
)
202 redef fun full_json
do
204 obj
["mmodule"] = to_mentity_ref
(mmodule
)
205 obj
["mclass"] = to_mentity_ref
(mclass
)
206 obj
["mpropdefs"] = to_mentity_refs
(mpropdefs
)
207 obj
["intro_mproperties"] = to_mentity_refs
(intro_mproperties
)
208 obj
["intro"] = to_mentity_ref
(mclass
.intro
)
209 obj
["mpackage"] = to_mentity_ref
(mmodule
.mpackage
)
210 obj
["intro_mpropdefs"] = to_mentity_refs
(collect_intro_mpropdefs
(private_view
))
211 obj
["redef_mpropdefs"] = to_mentity_refs
(collect_redef_mpropdefs
(private_view
))
216 redef class MProperty
217 redef fun full_json
do
219 obj
["intro"] = to_mentity_ref
(intro
)
220 obj
["intro_mclassdef"] = to_mentity_ref
(intro_mclassdef
)
221 obj
["mpropdefs"] = to_mentity_refs
(mpropdefs
)
222 obj
["intro_mclass"] = to_mentity_ref
(intro_mclassdef
.mclass
)
223 obj
["mpackage"] = to_mentity_ref
(intro_mclassdef
.mmodule
.mpackage
)
231 obj
["is_init"] = is_init
232 obj
["msignature"] = intro
.msignature
237 redef class MAttribute
240 obj
["static_mtype"] = to_mentity_ref
(intro
.static_mtype
)
245 redef class MVirtualTypeProp
248 obj
["mvirtualtype"] = to_mentity_ref
(mvirtualtype
)
249 obj
["bound"] = to_mentity_ref
(intro
.bound
)
257 obj
["is_intro"] = is_intro
261 redef fun full_json
do
263 obj
["mclassdef"] = to_mentity_ref
(mclassdef
)
264 obj
["mproperty"] = to_mentity_ref
(mproperty
)
265 obj
["intro"] = to_mentity_ref
(mproperty
.intro
)
266 obj
["intro_mclassdef"] = to_mentity_ref
(mproperty
.intro
.mclassdef
)
267 obj
["mmodule"] = to_mentity_ref
(mclassdef
.mmodule
)
268 obj
["mgroup"] = to_mentity_ref
(mclassdef
.mmodule
.mgroup
)
269 obj
["mpackage"] = to_mentity_ref
(mclassdef
.mmodule
.mpackage
)
274 redef class MMethodDef
277 obj
["msignature"] = msignature
282 redef class MAttributeDef
285 obj
["static_mtype"] = to_mentity_ref
(static_mtype
)
290 redef class MVirtualTypeDef
293 obj
["bound"] = to_mentity_ref
(bound
)
294 obj
["is_fixed"] = is_fixed
299 redef class MSignature
301 var obj
= new JsonObject
303 var arr
= new JsonArray
304 for mparam
in mparameters
do arr
.add mparam
306 obj
["return_mtype"] = to_mentity_ref
(return_mtype
)
307 obj
["vararg_rank"] = vararg_rank
312 redef class MParameterType
314 var obj
= new JsonObject
317 obj
["mtype"] = to_mentity_ref
(mclass
.intro
.bound_mtype
.arguments
[rank
])
322 redef class MParameter
324 var obj
= new JsonObject
325 obj
["is_vararg"] = is_vararg
327 obj
["mtype"] = to_mentity_ref
(mtype
)
332 # Create a ref to a `mentity`.
333 fun to_mentity_ref
(mentity
: nullable MEntity): nullable MEntityRef do
334 if mentity
== null then return null
335 return new MEntityRef(mentity
)
338 # Return a collection of `mentities` as a JsonArray of MEntityRefs.
339 fun to_mentity_refs
(mentities
: Collection[MEntity]): JsonArray do
340 var array
= new JsonArray
341 for mentity
in mentities
do array
.add to_mentity_ref
(mentity
)