Branch data Line data Source code
1 : : // Copyright (c) 2026 M. A. Chatterjee
2 : : // SPDX-License-Identifier: BSD-2-Clause
3 : :
4 : : #include "triepack/json.hpp"
5 : :
6 : : extern "C" {
7 : : #include "triepack/triepack_json.h"
8 : : }
9 : :
10 : : #include <cstring>
11 : :
12 : : namespace triepack
13 : : {
14 : :
15 : : // ---------------------------------------------------------------------------
16 : : // Json
17 : : // ---------------------------------------------------------------------------
18 : :
19 : 7 : Json::Json() : handle_(nullptr) {}
20 : :
21 : 8 : Json::~Json()
22 : : {
23 : 8 : tp_json_close(&handle_);
24 : 8 : }
25 : :
26 : 1 : Json::Json(Json &&other) noexcept : handle_(nullptr)
27 : : {
28 : 1 : auto *tmp = other.handle_;
29 : 1 : other.handle_ = nullptr;
30 : 1 : handle_ = tmp;
31 : 1 : }
32 : :
33 : 2 : Json &Json::operator=(Json &&other) noexcept
34 : : {
35 [ + + ]: 2 : if (this != &other) {
36 : 1 : tp_json_close(&handle_);
37 : 1 : auto *tmp = other.handle_;
38 : 1 : other.handle_ = nullptr;
39 : 1 : handle_ = tmp;
40 : : }
41 : 2 : return *this;
42 : : }
43 : :
44 : 4 : int Json::encode(const char *json_str, const uint8_t **out_data, size_t *out_size)
45 : : {
46 [ + + + + : 4 : if (!json_str || !out_data || !out_size)
+ + ]
47 : 3 : return TP_ERR_INVALID_PARAM;
48 : 1 : uint8_t *buf = nullptr;
49 [ + - ]: 1 : tp_result rc = tp_json_encode(json_str, std::strlen(json_str), &buf, out_size);
50 : 1 : *out_data = buf;
51 : 1 : return rc;
52 : : }
53 : :
54 : 3 : int Json::decode(const uint8_t *data, size_t size, const char **out_json)
55 : : {
56 [ + + + + ]: 3 : if (!data || !out_json)
57 : 2 : return TP_ERR_INVALID_PARAM;
58 : 1 : char *str = nullptr;
59 : 1 : size_t len = 0;
60 [ + - ]: 1 : tp_result rc = tp_json_decode(data, size, &str, &len);
61 : 1 : *out_json = str;
62 : 1 : return rc;
63 : : }
64 : :
65 : 2 : tp_json *Json::handle() const
66 : : {
67 : 2 : return handle_;
68 : : }
69 : :
70 : : } // namespace triepack
|