| helics 3.6.1

JsonGeneration.hpp
1 /*
2 Copyright (c) 2017-2025,
3 Battelle Memorial Institute; Lawrence Livermore National Security, LLC; Alliance for Sustainable
4 Energy, LLC. See the top-level NOTICE for additional details. All rights reserved.
5 SPDX-License-Identifier: BSD-3-Clause
6 */
7 #pragma once
8
9 #include "nlohmann/json.hpp"
10
11 #include <fmt/format.h>
12 #include <string>
13
14 namespace helics {
15
16 inline std::string generateJsonQuotedString(const std::string& string)
17 {
18 nlohmann::json V = string;
19 return V.dump(-1, ' ', true, nlohmann::json::error_handler_t::hex);
20 }
21
22 enum class JsonErrorCodes : std::int32_t {
23 BAD_REQUEST = 400,
24 FORBIDDEN = 403,
25 NOT_FOUND = 404,
26 METHOD_NOT_ALLOWED = 405,
27 TIMEOUT = 408,
28 DISCONNECTED = 410,
29 INTERNAL_ERROR = 500,
30 NOT_IMPLEMENTED = 501,
31 BAD_GATEWAY = 502,
32 SERVICE_UNAVAILABLE = 503,
33 GATEWAY_TIMEOUT = 504
34 };
36 inline std::string generateJsonErrorResponse(JsonErrorCodes code, const std::string& message)
37 {
38 return fmt::format("{{\n \"error\":{{\n \"code\":{},\n \"message\":{}\n }}\n}}",
39 static_cast<std::int32_t>(code),
40 generateJsonQuotedString(message));
41 }
42
43 } // namespace helics
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::generateJsonErrorResponse
std::string generateJsonErrorResponse(JsonErrorCodes code, const std::string &message)
Definition: JsonGeneration.hpp:36