Blue Brain BioExplorer
jsonUtils.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2018, EPFL/Blue Brain Project
3  * All rights reserved. Do not distribute without permission.
4  * Responsible Author: Daniel Nachbaur <daniel.nachbaur@epfl.ch>
5  *
6  * This file is part of Blue Brain BioExplorer <https://github.com/BlueBrain/BioExplorer>
7  *
8  * This library is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU Lesser General Public License version 3.0 as published
10  * by the Free Software Foundation.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
22 #pragma once
23 
24 #ifdef __GNUC__
25 #pragma GCC diagnostic push
26 #pragma GCC diagnostic ignored "-Wshadow"
27 #endif
28 #include "staticjson/staticjson.hpp"
29 #ifdef __GNUC__
30 #pragma GCC diagnostic pop
31 #endif
32 
33 #include "rapidjson/document.h"
34 #include "rapidjson/writer.h"
35 
36 namespace core
37 {
39 template <class T>
40 std::string buildJsonSchema(const std::string& title)
41 {
42  T obj;
43  return buildJsonSchema(obj, title);
44 }
45 
47 template <class T>
48 std::string buildJsonSchema(T& obj, const std::string& title)
49 {
50  using namespace rapidjson;
51  auto schema = staticjson::export_json_schema(&obj);
52  schema.AddMember(StringRef("title"), StringRef(title.c_str()),
53  schema.GetAllocator());
54 
55  StringBuffer buffer;
56  Writer<StringBuffer> writer(buffer);
57  schema.Accept(writer);
58  return buffer.GetString();
59 }
60 
62 template <class T>
63 rapidjson::Document getRPCParameterSchema(const std::string& paramName,
64  const std::string& paramDescription,
65  T& obj)
66 {
67  using namespace rapidjson;
68  auto schema = staticjson::export_json_schema(&obj);
69  auto& allocator = schema.GetAllocator();
70 
71  schema.AddMember(StringRef("name"), Value(paramName.c_str(), allocator),
72  allocator);
73  schema.AddMember(StringRef("description"),
74  Value(paramDescription.c_str(), allocator), allocator);
75  return schema;
76 };
77 
78 rapidjson::Document _buildJsonRpcSchema(const RpcDescription& desc)
79 {
80  using namespace rapidjson;
81  Document schema(kObjectType);
82  auto& allocator = schema.GetAllocator();
83  schema.AddMember(StringRef("title"), StringRef(desc.methodName.c_str()),
84  allocator);
85  schema.AddMember(StringRef("description"),
86  StringRef(desc.methodDescription.c_str()), allocator);
87  schema.AddMember(StringRef("type"), StringRef("method"), allocator);
88  schema.AddMember(StringRef("async"), desc.type == Execution::async,
89  allocator);
90  return schema;
91 }
92 
98 template <class P, class R>
100  P& obj)
101 {
102  using namespace rapidjson;
103  auto schema = _buildJsonRpcSchema(desc);
104  auto& allocator = schema.GetAllocator();
105 
106  R retVal;
107  auto retSchema = staticjson::export_json_schema(&retVal);
108  schema.AddMember(StringRef("returns"), retSchema, allocator);
109 
110  Value params(kArrayType);
111  auto paramSchema =
112  getRPCParameterSchema<P>(desc.paramName, desc.paramDescription, obj);
113  params.PushBack(paramSchema, allocator);
114  schema.AddMember(StringRef("params"), params, allocator);
115 
116  StringBuffer buffer;
117  Writer<StringBuffer> writer(buffer);
118  schema.Accept(writer);
119  return buffer.GetString();
120 }
121 
122 template <class P, class R>
124 {
125  P obj;
126  return buildJsonRpcSchemaRequest<P, R>(desc, obj);
127 }
128 
134 template <class R>
136  R& retVal)
137 {
138  using namespace rapidjson;
139  auto schema = _buildJsonRpcSchema(desc);
140  auto& allocator = schema.GetAllocator();
141 
142  auto retSchema = staticjson::export_json_schema(&retVal);
143  schema.AddMember(StringRef("returns"), retSchema, allocator);
144 
145  Value params(kArrayType);
146  schema.AddMember(StringRef("params"), params, allocator);
147 
148  StringBuffer buffer;
149  Writer<StringBuffer> writer(buffer);
150  schema.Accept(writer);
151  return buffer.GetString();
152 }
153 
154 template <class R>
156 {
157  R retVal;
158  return buildJsonRpcSchemaRequestReturnOnly<R>(desc, retVal);
159 }
160 
166 template <class P>
168  P& obj)
169 {
170  using namespace rapidjson;
171  auto schema = _buildJsonRpcSchema(desc);
172  auto& allocator = schema.GetAllocator();
173 
174  Value params(kArrayType);
175  auto paramSchema =
176  getRPCParameterSchema<P>(desc.paramName, desc.paramDescription, obj);
177  params.PushBack(paramSchema, allocator);
178  schema.AddMember(StringRef("params"), params, allocator);
179 
180  StringBuffer buffer;
181  Writer<StringBuffer> writer(buffer);
182  schema.Accept(writer);
183  return buffer.GetString();
184 }
185 
186 template <class P>
188 {
189  P obj;
190  return buildJsonRpcSchemaNotify<P>(desc, obj);
191 }
192 
194 std::string buildJsonRpcSchemaNotify(const RpcDescription& desc)
195 {
196  using namespace rapidjson;
197  auto schema = _buildJsonRpcSchema(desc);
198  auto& allocator = schema.GetAllocator();
199 
200  schema.AddMember(StringRef("returns"), Value(kNullType), allocator);
201 
202  Value params(kArrayType);
203  schema.AddMember(StringRef("params"), params, allocator);
204 
205  StringBuffer buffer;
206  Writer<StringBuffer> writer(buffer);
207  schema.Accept(writer);
208  return buffer.GetString();
209 }
210 } // namespace core
rapidjson::Document _buildJsonRpcSchema(const RpcDescription &desc)
Definition: jsonUtils.h:78
std::string buildJsonRpcSchemaNotify(const RpcParameterDescription &desc, P &obj)
Definition: jsonUtils.h:167
rapidjson::Document getRPCParameterSchema(const std::string &paramName, const std::string &paramDescription, T &obj)
Definition: jsonUtils.h:63
std::string buildJsonRpcSchemaRequestReturnOnly(const RpcDescription &desc, R &retVal)
Definition: jsonUtils.h:135
std::string buildJsonSchema(std::vector< std::pair< std::string, PropertyMap >> &objs, const std::string &title)
std::string buildJsonRpcSchemaRequest(const RpcParameterDescription &desc, P &obj)
Definition: jsonUtils.h:99
Execution type
Definition: Types.h:369
std::string methodName
Definition: Types.h:367
std::string methodDescription
Definition: Types.h:368
std::string paramDescription
Definition: Types.h:392