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 Serializable.
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
29 import json
::serialization_write
36 private fun core_serialize_base
(v
: Serializer) do
37 v
.serialize_attribute
("name", name
)
38 var mdoc
= mdoc_or_fallback
40 v
.serialize_attribute
("synopsis", mdoc
.synopsis
)
44 redef fun core_serialize_to
(v
) do
45 core_serialize_base
(v
)
47 v
.serialize_attribute
("namespace", json_namespace
)
48 v
.serialize_attribute
("class_name", class_name
)
49 v
.serialize_attribute
("full_name", full_name
)
50 v
.serialize_attribute
("visibility", visibility
.to_s
)
51 var mdoc
= mdoc_or_fallback
53 v
.serialize_attribute
("html_synopsis", mdoc
.html_synopsis
.write_to_string
)
56 var modifiers
= collect_modifiers
57 if modifiers
.not_empty
then
58 v
.serialize_attribute
("modifiers", modifiers
)
62 # Return `self.full_name` as an object that can be serialized to json.
63 fun json_namespace
: JsonNamespace is abstract
65 # Return a new MEntityRef to `self`.
66 fun to_json_ref
: MEntityRef do return new MEntityRef(self)
72 redef fun core_serialize_to
(v
) do
73 var doc
= html_documentation
.write_to_string
.trim
74 if not doc
.is_empty
then
75 v
.serialize_attribute
("html_documentation", doc
)
80 redef class nitc
::Location
83 redef fun core_serialize_to
(v
) do
84 v
.serialize_attribute
("column_end", column_end
)
85 v
.serialize_attribute
("column_start", column_start
)
86 v
.serialize_attribute
("line_end", line_end
)
87 v
.serialize_attribute
("line_start", line_start
)
90 v
.serialize_attribute
("file", file
.filename
)
96 redef fun core_serialize_to
(v
) do
99 var metadata
= self.metadata
100 if metadata
.license
!= null then
101 v
.serialize_attribute
("license", metadata
.license
)
103 if metadata
.maintainers
.not_empty
then
104 v
.serialize_attribute
("maintainer", metadata
.maintainers
.first
)
106 if metadata
.tags
.not_empty
then
107 v
.serialize_attribute
("tags", metadata
.tags
)
111 redef fun json_namespace
do
112 var ns
= new JsonNamespace
119 redef fun json_namespace
do
120 var ns
= new JsonNamespace
121 if parent
!= null then
122 ns
.prepend parent
.as(not null).json_namespace
131 redef fun json_namespace
do
132 var ns
= new JsonNamespace
133 if mgroup
!= null then
134 ns
.add_all mgroup
.as(not null).mpackage
.json_namespace
141 private fun ns_for
(visibility
: MVisibility): JsonNamespace do
142 if visibility
<= private_visibility
then return json_namespace
143 var mgroup
= self.mgroup
144 if mgroup
== null then return json_namespace
145 return mgroup
.mpackage
.json_namespace
150 redef fun core_serialize_to
(v
) do
153 if mparameters
.not_empty
then
154 v
.serialize_attribute
("mparameters", mparameters
)
158 redef fun json_namespace
do
159 var ns
= new JsonNamespace
160 ns
.add_all intro_mmodule
.ns_for
(visibility
)
167 redef class MClassDef
168 redef fun core_serialize_to
(v
) do
171 v
.serialize_attribute
("is_intro", true)
173 if mclass
.mparameters
.not_empty
then
174 v
.serialize_attribute
("mparameters", mclass
.mparameters
)
178 redef fun json_namespace
do
179 var ns
= new JsonNamespace
181 ns
.add_all mmodule
.ns_for
(mclass
.visibility
)
183 ns
.add mclass
.to_json_ref
184 else if mclass
.intro_mmodule
.mpackage
!= mmodule
.mpackage
then
185 ns
.add_all mmodule
.json_namespace
187 ns
.add_all mclass
.json_namespace
188 else if mclass
.visibility
> private_visibility
then
189 ns
.add_all mmodule
.json_namespace
191 ns
.add mclass
.to_json_ref
193 ns
.add_all mmodule
.json_namespace
195 ns
.add mclass
.intro_mmodule
.to_json_ref
197 ns
.add mclass
.to_json_ref
203 redef class MProperty
204 redef fun json_namespace
do
205 var ns
= new JsonNamespace
206 if intro_mclassdef
.is_intro
then
207 ns
.add_all intro_mclassdef
.mmodule
.ns_for
(visibility
)
209 ns
.add intro_mclassdef
.mclass
.to_json_ref
213 ns
.add_all intro_mclassdef
.mmodule
.json_namespace
215 ns
.add intro_mclassdef
.mclass
.to_json_ref
224 redef fun core_serialize_to
(v
) do
227 v
.serialize_attribute
("is_init", true)
229 v
.serialize_attribute
("msignature", intro
.msignature
)
233 redef class MAttribute
234 redef fun core_serialize_to
(v
) do
236 v
.serialize_attribute
("static_mtype", intro
.static_mtype
)
240 redef class MVirtualTypeProp
241 redef fun core_serialize_to
(v
) do
243 v
.serialize_attribute
("bound", intro
.bound
)
248 redef fun core_serialize_to
(v
) do
251 v
.serialize_attribute
("is_intro", true)
255 redef fun json_namespace
do
256 var res
= new JsonNamespace
257 res
.add_all mclassdef
.json_namespace
260 if mclassdef
.mclass
== mproperty
.intro_mclassdef
.mclass
then
263 if mclassdef
.mmodule
.mpackage
!= mproperty
.intro_mclassdef
.mmodule
.mpackage
then
264 res
.add_all mproperty
.intro_mclassdef
.mmodule
.ns_for
(mproperty
.visibility
)
266 else if mproperty
.visibility
<= private_visibility
then
267 if mclassdef
.mmodule
.namespace_for
(mclassdef
.mclass
.visibility
) != mproperty
.intro_mclassdef
.mmodule
.mpackage
then
269 res
.add mproperty
.intro_mclassdef
.mmodule
.to_json_ref
273 if mclassdef
.mclass
!= mproperty
.intro_mclassdef
.mclass
then
274 res
.add mproperty
.intro_mclassdef
.to_json_ref
283 redef class MMethodDef
284 redef fun core_serialize_to
(v
) do
286 v
.serialize_attribute
("msignature", msignature
)
290 redef class MAttributeDef
291 redef fun core_serialize_to
(v
) do
293 v
.serialize_attribute
("static_mtype", static_mtype
)
297 redef class MVirtualTypeDef
298 redef fun core_serialize_to
(v
) do
300 v
.serialize_attribute
("bound", bound
)
305 redef fun core_serialize_to
(v
) do
306 v
.serialize_attribute
("name", name
)
308 var mdoc
= mdoc_or_fallback
310 v
.serialize_attribute
("synopsis", mdoc
.synopsis
)
311 v
.serialize_attribute
("html_synopsis", mdoc
.html_synopsis
.write_to_string
)
317 redef class MSignature
318 redef fun core_serialize_to
(v
) do
319 v
.serialize_attribute
("arity", arity
)
320 v
.serialize_attribute
("mparameters", mparameters
)
321 v
.serialize_attribute
("return_mtype", return_mtype
)
325 redef class MParameterType
326 redef fun core_serialize_to
(v
) do
327 v
.serialize_attribute
("name", name
)
328 v
.serialize_attribute
("mtype", mclass
.intro
.bound_mtype
.arguments
[rank
])
332 redef class MParameter
333 redef fun core_serialize_to
(v
) do
334 v
.serialize_attribute
("is_vararg", is_vararg
)
335 v
.serialize_attribute
("name", name
)
336 v
.serialize_attribute
("mtype", mtype
)
340 # Fullname representation that can be used to build decorated links
342 # * MPackage: `mpackage_name`
343 # * MGroup: `(mpackage_name::)mgroup_name`
345 super Array[nullable JsonRef]
348 redef fun to_s
do return self.join
("")
349 redef fun serialize_to
(v
) do to_a
.serialize_to
(v
)
352 # Something that goes in a JsonNamespace
355 # * a `RefToken` for tokens like `::`, `>` and `$`
356 # * a `MEntityRef` for references to mentities
361 # A reference to another mentity.
365 # MEntity to link to.
368 redef fun core_serialize_to
(v
) do
369 mentity
.core_serialize_base
(v
)
373 # A namespace token representation
375 # Used for namespace tokens like `::`, `>` and `$`