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
28 import json
::serialization_write
35 private fun core_serialize_base
(v
: Serializer) do
36 v
.serialize_attribute
("name", name
)
37 var mdoc
= mdoc_or_fallback
39 v
.serialize_attribute
("synopsis", mdoc
.synopsis
)
43 redef fun core_serialize_to
(v
) do
44 core_serialize_base
(v
)
46 v
.serialize_attribute
("namespace", json_namespace
)
47 v
.serialize_attribute
("class_name", class_name
)
48 v
.serialize_attribute
("full_name", full_name
)
49 v
.serialize_attribute
("visibility", visibility
.to_s
)
50 var mdoc
= mdoc_or_fallback
52 v
.serialize_attribute
("html_synopsis", mdoc
.html_synopsis
.write_to_string
)
55 var modifiers
= collect_modifiers
56 if modifiers
.not_empty
then
57 v
.serialize_attribute
("modifiers", modifiers
)
61 # Return `self.full_name` as an object that can be serialized to json.
62 fun json_namespace
: JsonNamespace is abstract
64 # Return a new MEntityRef to `self`.
65 fun to_json_ref
: MEntityRef do return new MEntityRef(self)
71 redef fun core_serialize_to
(v
) do
72 var doc
= html_documentation
.write_to_string
.trim
73 if not doc
.is_empty
then
74 v
.serialize_attribute
("html_documentation", doc
)
79 redef class nitc
::Location
82 redef fun core_serialize_to
(v
) do
83 v
.serialize_attribute
("column_end", column_end
)
84 v
.serialize_attribute
("column_start", column_start
)
85 v
.serialize_attribute
("line_end", line_end
)
86 v
.serialize_attribute
("line_start", line_start
)
89 v
.serialize_attribute
("file", file
.filename
)
95 redef fun core_serialize_to
(v
) do
98 var metadata
= self.metadata
99 if metadata
.license
!= null then
100 v
.serialize_attribute
("license", metadata
.license
)
102 if metadata
.maintainers
.not_empty
then
103 v
.serialize_attribute
("maintainer", metadata
.maintainers
.first
)
105 if metadata
.tags
.not_empty
then
106 v
.serialize_attribute
("tags", metadata
.tags
)
110 redef fun json_namespace
do
111 var ns
= new JsonNamespace
118 redef fun json_namespace
do
119 var ns
= new JsonNamespace
120 if parent
!= null then
121 ns
.prepend parent
.as(not null).json_namespace
130 redef fun json_namespace
do
131 var ns
= new JsonNamespace
132 if mgroup
!= null then
133 ns
.add_all mgroup
.as(not null).mpackage
.json_namespace
140 private fun ns_for
(visibility
: MVisibility): JsonNamespace do
141 if visibility
<= private_visibility
then return json_namespace
142 var mgroup
= self.mgroup
143 if mgroup
== null then return json_namespace
144 return mgroup
.mpackage
.json_namespace
149 redef fun core_serialize_to
(v
) do
152 if mparameters
.not_empty
then
153 v
.serialize_attribute
("mparameters", mparameters
)
157 redef fun json_namespace
do
158 var ns
= new JsonNamespace
159 ns
.add_all intro_mmodule
.ns_for
(visibility
)
166 redef class MClassDef
167 redef fun core_serialize_to
(v
) do
170 v
.serialize_attribute
("is_intro", true)
172 if mclass
.mparameters
.not_empty
then
173 v
.serialize_attribute
("mparameters", mclass
.mparameters
)
177 redef fun json_namespace
do
178 var ns
= new JsonNamespace
180 ns
.add_all mmodule
.ns_for
(mclass
.visibility
)
182 ns
.add mclass
.to_json_ref
183 else if mclass
.intro_mmodule
.mpackage
!= mmodule
.mpackage
then
184 ns
.add_all mmodule
.json_namespace
186 ns
.add_all mclass
.json_namespace
187 else if mclass
.visibility
> private_visibility
then
188 ns
.add_all mmodule
.json_namespace
190 ns
.add mclass
.to_json_ref
192 ns
.add_all mmodule
.json_namespace
194 ns
.add mclass
.intro_mmodule
.to_json_ref
196 ns
.add mclass
.to_json_ref
202 redef class MProperty
203 redef fun json_namespace
do
204 var ns
= new JsonNamespace
205 if intro_mclassdef
.is_intro
then
206 ns
.add_all intro_mclassdef
.mmodule
.ns_for
(visibility
)
208 ns
.add intro_mclassdef
.mclass
.to_json_ref
212 ns
.add_all intro_mclassdef
.mmodule
.json_namespace
214 ns
.add intro_mclassdef
.mclass
.to_json_ref
223 redef fun core_serialize_to
(v
) do
226 v
.serialize_attribute
("is_init", true)
228 v
.serialize_attribute
("msignature", intro
.msignature
)
232 redef class MAttribute
233 redef fun core_serialize_to
(v
) do
235 v
.serialize_attribute
("static_mtype", intro
.static_mtype
)
239 redef class MVirtualTypeProp
240 redef fun core_serialize_to
(v
) do
242 v
.serialize_attribute
("bound", intro
.bound
)
247 redef fun core_serialize_to
(v
) do
250 v
.serialize_attribute
("is_intro", true)
254 redef fun json_namespace
do
255 var res
= new JsonNamespace
256 res
.add_all mclassdef
.json_namespace
259 if mclassdef
.mclass
== mproperty
.intro_mclassdef
.mclass
then
262 if mclassdef
.mmodule
.mpackage
!= mproperty
.intro_mclassdef
.mmodule
.mpackage
then
263 res
.add_all mproperty
.intro_mclassdef
.mmodule
.ns_for
(mproperty
.visibility
)
265 else if mproperty
.visibility
<= private_visibility
then
266 if mclassdef
.mmodule
.namespace_for
(mclassdef
.mclass
.visibility
) != mproperty
.intro_mclassdef
.mmodule
.mpackage
then
268 res
.add mproperty
.intro_mclassdef
.mmodule
.to_json_ref
272 if mclassdef
.mclass
!= mproperty
.intro_mclassdef
.mclass
then
273 res
.add mproperty
.intro_mclassdef
.to_json_ref
282 redef class MMethodDef
283 redef fun core_serialize_to
(v
) do
285 v
.serialize_attribute
("msignature", msignature
)
289 redef class MAttributeDef
290 redef fun core_serialize_to
(v
) do
292 v
.serialize_attribute
("static_mtype", static_mtype
)
296 redef class MVirtualTypeDef
297 redef fun core_serialize_to
(v
) do
299 v
.serialize_attribute
("bound", bound
)
304 redef fun core_serialize_to
(v
) do
305 v
.serialize_attribute
("name", name
)
307 var mdoc
= mdoc_or_fallback
309 v
.serialize_attribute
("synopsis", mdoc
.synopsis
)
310 v
.serialize_attribute
("html_synopsis", mdoc
.html_synopsis
.write_to_string
)
316 redef class MSignature
317 redef fun core_serialize_to
(v
) do
318 v
.serialize_attribute
("arity", arity
)
319 v
.serialize_attribute
("mparameters", mparameters
)
320 v
.serialize_attribute
("return_mtype", return_mtype
)
324 redef class MParameterType
325 redef fun core_serialize_to
(v
) do
326 v
.serialize_attribute
("name", name
)
327 v
.serialize_attribute
("mtype", mclass
.intro
.bound_mtype
.arguments
[rank
])
331 redef class MParameter
332 redef fun core_serialize_to
(v
) do
333 v
.serialize_attribute
("is_vararg", is_vararg
)
334 v
.serialize_attribute
("name", name
)
335 v
.serialize_attribute
("mtype", mtype
)
339 # Fullname representation that can be used to build decorated links
341 # * MPackage: `mpackage_name`
342 # * MGroup: `(mpackage_name::)mgroup_name`
344 super Array[nullable JsonRef]
347 redef fun to_s
do return self.join
("")
348 redef fun serialize_to
(v
) do to_a
.serialize_to
(v
)
351 # Something that goes in a JsonNamespace
354 # * a `RefToken` for tokens like `::`, `>` and `$`
355 # * a `MEntityRef` for references to mentities
360 # A reference to another mentity.
364 # MEntity to link to.
367 redef fun core_serialize_to
(v
) do
368 mentity
.core_serialize_base
(v
)
372 # A namespace token representation
374 # Used for namespace tokens like `::`, `>` and `$`
381 redef class MPackageMetadata
384 redef fun core_serialize_to
(v
) do
386 v
.serialize_attribute
("license", license
)
387 v
.serialize_attribute
("maintainers", maintainers
)
388 v
.serialize_attribute
("contributors", contributors
)
389 v
.serialize_attribute
("tags", tags
)
390 v
.serialize_attribute
("tryit", tryit
)
391 v
.serialize_attribute
("apk", apk
)
392 v
.serialize_attribute
("homepage", homepage
)
393 v
.serialize_attribute
("browse", browse
)
394 v
.serialize_attribute
("git", git
)
395 v
.serialize_attribute
("issues", issues
)
396 v
.serialize_attribute
("first_date", first_date
)
397 v
.serialize_attribute
("last_date", last_date
)
402 redef class CatalogStats
405 redef fun core_serialize_to
(v
) do
407 v
.serialize_attribute
("packages", packages
)
408 v
.serialize_attribute
("maintainers", maintainers
)
409 v
.serialize_attribute
("contributors", contributors
)
410 v
.serialize_attribute
("tags", tags
)
411 v
.serialize_attribute
("modules", modules
)
412 v
.serialize_attribute
("classes", classes
)
413 v
.serialize_attribute
("methods", methods
)
414 v
.serialize_attribute
("loc", loc
)
418 # MPackage statistics for the catalog
419 redef class MPackageStats
422 redef fun core_serialize_to
(v
) do
424 v
.serialize_attribute
("mmodules", mmodules
)
425 v
.serialize_attribute
("mclasses", mclasses
)
426 v
.serialize_attribute
("mmethods", mmethods
)
427 v
.serialize_attribute
("loc", loc
)
428 v
.serialize_attribute
("errors", errors
)
429 v
.serialize_attribute
("warnings", warnings
)
430 v
.serialize_attribute
("warnings_per_kloc", warnings_per_kloc
)
431 v
.serialize_attribute
("documentation_score", documentation_score
)
432 v
.serialize_attribute
("commits", commits
)
433 v
.serialize_attribute
("score", score
)
440 redef fun core_serialize_to
(v
) do
442 v
.serialize_attribute
("name", name
)
443 v
.serialize_attribute
("email", email
)
444 v
.serialize_attribute
("gravatar", gravatar
)