2 * Copyright (c) 2020, The OpenThread Authors.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. Neither the name of the copyright holder nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
31 * This file includes JSON formatter definition for RESTful HTTP server.
34 #ifndef OTBR_REST_JSON_HPP_
35 #define OTBR_REST_JSON_HPP_
37 #include "openthread/link.h"
38 #include "openthread/thread_ftd.h"
40 #include "rest/types.hpp"
41 #include "utils/hex.hpp"
47 * The functions within this namespace provides a tranformation from an object/string/number to a serialized Json
54 * This method formats an integer to a Json number and serialize it to a string.
56 * @param[in] aNumber an integer need to be format.
58 * @returns a string serlialized by a Json number.
61 std::string Number2JsonString(const uint32_t &aNumber);
64 * This method formats a Bytes array to a Json string and serialize it to a string.
66 * @param[in] aBytes A Bytes array representing a hex number.
68 * @returns A string serlialized by a Json string.
71 std::string Bytes2HexJsonString(const uint8_t *aBytes, uint8_t aLength);
74 * This method formats a C string to a Json string and serialize it to a string.
76 * @param[in] aCString A char pointer pointing to a C string.
78 * @returns A string serlialized by a Json string.
81 std::string CString2JsonString(const char *aCString);
84 * This method formats a string to a Json string and serialize it to a string.
86 * @param[in] aString A string.
88 * @returns a string serlialized by a Json string.
91 std::string String2JsonString(const std::string &aString);
94 * This method formats a Node object to a Json object and serialize it to a string.
96 * @param[in] aNode A Node object.
98 * @returns a string serlialized by a Json object.
101 std::string Node2JsonString(const NodeInfo &aNode);
104 * This method formats a vector including serveral Diagnostic object to a Json array and serialize it to a string.
106 * @param[in] aDiagSet A vector including serveral Diagnostic object.
108 * @returns A string serlialized by a Json array.
111 std::string Diag2JsonString(const std::vector<std::vector<otNetworkDiagTlv>> &aDiagSet);
114 * This method formats an Ipv6Address to a Json string and serialize it to a string.
116 * @param[in] aAddress An Ip6Address object.
118 * @returns A string serlialized by a Json string.
121 std::string IpAddr2JsonString(const otIp6Address &aAddress);
124 * This method formats a LinkModeConfig object to a Json object and serialize it to a string.
126 * @param[in] aMode A LinkModeConfig object.
128 * @returns A string serlialized by a Json object.
131 std::string Mode2JsonString(const otLinkModeConfig &aMode);
134 * This method formats a Connectivity object to a Json object and serialize it to a string.
136 * @param[in] aConnectivity A Connectivity object.
138 * @returns A string serlialized by a Json object.
141 std::string Connectivity2JsonString(const otNetworkDiagConnectivity &aConnectivity);
144 * This method formats a Route object to a Json object and serialize it to a string.
146 * @param[in] aRoute A Route object.
148 * @returns A string serlialized by a Json object.
151 std::string Route2JsonString(const otNetworkDiagRoute &aRoute);
154 * This method formats a RouteData object to a Json object and serialize it to a string.
156 * @param[in] aRouteData A RouteData object.
158 * @returns A string serlialized by a Json object.
161 std::string RouteData2JsonString(const otNetworkDiagRouteData &aRouteData);
164 * This method formats a LeaderData object to a Json object and serialize it to a string.
166 * @param[in] aLeaderData A LeaderData object.
168 * @returns A string serlialized by a Json object.
171 std::string LeaderData2JsonString(const otLeaderData &aLeaderData);
174 * This method formats a MacCounters object to a Json object and serialize it to a string.
176 * @param[in] aMacCounters A MacCounters object.
178 * @returns A string serlialized by a Json object.
181 std::string MacCounters2JsonString(const otNetworkDiagMacCounters &aMacCounters);
184 * This method formats a ChildEntry object to a Json object and serialize it to a string.
186 * @param[in] aChildEntry A ChildEntry object.
188 * @returns A string serlialized by a Json object.
191 std::string ChildTableEntry2JsonString(const otNetworkDiagChildEntry &aChildEntry);
194 * This method formats an error code and an error message to a Json object and serialize it to a string.
196 * @param[in] aErrorCode An enum HttpStatusCode such as '404'.
197 * @param[in] aErrorMessage Error message such as '404 Not Found'.
199 * @returns A string serlialized by a Json object.
202 std::string Error2JsonString(HttpStatusCode aErrorCode, std::string aErrorMessage);
209 #endif // OTBR_REST_JSON_HPP_