0.5.4
C++ to UML diagram generator based on Clang
Loading...
Searching...
No Matches
generator.cc
Go to the documentation of this file.
1/**
2 * @file src/common/generators/json/generator.cc
3 *
4 * Copyright (c) 2021-2024 Bartek Kryza <bkryza@gmail.com>
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19#include "generator.h"
20
21namespace clanguml::common {
22namespace model {
23using nlohmann::json;
24
25void to_json(nlohmann::json &j, const source_location &sl)
26{
27 j = json{{"file", sl.file_relative()},
28 {"translation_unit", sl.translation_unit()}, {"line", sl.line()},
29 {"column", sl.column()}};
30}
31
32void to_json(nlohmann::json &j, const element &c)
33{
34 j = json{{"id", std::to_string(c.id().value())},
36 {"namespace", c.get_namespace().to_string()}, {"type", c.type_name()},
37 {"display_name",
39
40 if (const auto &comment = c.comment(); comment)
41 j["comment"] = comment.value();
42
43 if (!c.file().empty()) {
44 j["source_location"] =
45 dynamic_cast<const common::model::source_location &>(c);
46 }
47}
48
49void to_json(nlohmann::json &j, const template_parameter &c)
50{
51 j["kind"] = to_string(c.kind());
52 if (const auto &t = c.type(); t)
53 j["type"] = t.value();
54 if (const auto &n = c.name(); n)
55 j["name"] = n.value();
56 if (const auto &d = c.default_value(); d)
57 j["default"] = d.value();
58 j["is_variadic"] = c.is_variadic();
59 j["template_parameters"] = c.template_params();
60}
61
62void to_json(nlohmann::json &j, const relationship &c)
63{
64 j["type"] = to_string(c.type());
65 j["destination"] = std::to_string(c.destination().value());
66 if (!c.multiplicity_source().empty())
67 j["multiplicity_source"] = c.multiplicity_source();
68 if (!c.multiplicity_destination().empty())
69 j["multiplicity_destination"] = c.multiplicity_destination();
70 if (c.access() != access_t::kNone)
71 j["access"] = to_string(c.access());
72 if (!c.label().empty())
73 j["label"] = c.label();
74 if (const auto &comment = c.comment(); comment)
75 j["comment"] = comment.value();
76}
77} // namespace model
78
79namespace generators::json {
80
81std::string render_name(std::string name)
82{
83 util::replace_all(name, "##", "::");
84
85 return name;
86}
87
88} // namespace generators::json
89} // namespace clanguml::common