@@ -21,37 +21,28 @@ namespace sdk {
21
21
namespace {
22
22
static const std::string TOR_SOCKS5_PREFIX (" socks5://" );
23
23
24
- class gdkrust_json {
25
- public:
26
- explicit gdkrust_json (const nlohmann::json& val)
27
- : gdkrust_json(val.dump())
28
- {
29
- }
30
-
31
- explicit gdkrust_json (GDKRUST_json* json) { m_json = json; }
32
-
33
- explicit gdkrust_json (const std::string& str) { GDKRUST_convert_string_to_json (str.c_str (), &m_json); }
24
+ using json_ptr = std::shared_ptr<GDKRUST_json>;
34
25
35
- static inline nlohmann::json from_serde (GDKRUST_json* json)
36
- {
37
- char * output;
38
- GDKRUST_convert_json_to_string (json, &output);
39
-
40
- auto cppjson = nlohmann::json::parse (output);
41
-
42
- GDKRUST_destroy_json (json);
43
- GDKRUST_destroy_string (output);
26
+ inline json_ptr convert_json (const std::string& v)
27
+ {
28
+ GDKRUST_json* p;
29
+ GDKRUST_convert_string_to_json (v.c_str (), &p);
30
+ return json_ptr (p, GDKRUST_destroy_json);
31
+ }
44
32
45
- return cppjson;
46
- }
33
+ inline json_ptr convert_json (const nlohmann::json& v) { return convert_json (v.dump ()); }
47
34
48
- GDKRUST_json* get () { return m_json; }
35
+ static nlohmann::json convert_serde (GDKRUST_json* json)
36
+ {
37
+ char * output;
38
+ GDKRUST_convert_json_to_string (json, &output);
39
+ GDKRUST_destroy_json (json);
49
40
50
- ~gdkrust_json () { GDKRUST_destroy_json (m_json); }
41
+ auto cppjson = nlohmann::json::parse (output);
42
+ GDKRUST_destroy_string (output);
51
43
52
- private:
53
- GDKRUST_json* m_json;
54
- };
44
+ return cppjson;
45
+ }
55
46
56
47
static void check_code (const int32_t return_code)
57
48
{
@@ -80,7 +71,7 @@ namespace sdk {
80
71
ga_rust::ga_rust (const nlohmann::json& net_params)
81
72
: session_impl(net_params)
82
73
{
83
- GDKRUST_create_session (&m_session, gdkrust_json (m_net_params.get_json ()).get ());
74
+ GDKRUST_create_session (&m_session, convert_json (m_net_params.get_json ()).get ());
84
75
}
85
76
86
77
ga_rust::~ga_rust ()
@@ -127,10 +118,9 @@ namespace sdk {
127
118
nlohmann::json ga_rust::call_session (const std::string& method, const nlohmann::json& input) const
128
119
{
129
120
GDKRUST_json* ret;
130
- auto rustinput = gdkrust_json (input).get ();
131
- int res = GDKRUST_call_session (m_session, method.c_str (), rustinput, &ret);
121
+ int res = GDKRUST_call_session (m_session, method.c_str (), convert_json (input).get (), &ret);
132
122
check_code (res);
133
- return gdkrust_json::from_serde (ret);
123
+ return convert_serde (ret);
134
124
}
135
125
136
126
void ga_rust::connect ()
@@ -285,7 +275,7 @@ namespace sdk {
285
275
void ga_rust::GDKRUST_notif_handler (void * self_context, GDKRUST_json* json)
286
276
{
287
277
// "new" needed because we want that to be on the heap. the notif handler will free it
288
- nlohmann::json* converted_heap = new nlohmann::json (gdkrust_json::from_serde (json));
278
+ nlohmann::json* converted_heap = new nlohmann::json (convert_serde (json));
289
279
GA_json* as_ptr = reinterpret_cast <GA_json*>(converted_heap);
290
280
291
281
ga_rust* self = static_cast <ga_rust*>(self_context);
@@ -613,8 +603,7 @@ namespace sdk {
613
603
614
604
int32_t ga_rust::spv_verify_tx (const nlohmann::json& details)
615
605
{
616
- auto rustinput = gdkrust_json (details).get ();
617
- return GDKRUST_spv_verify_tx (rustinput);
606
+ return GDKRUST_spv_verify_tx (convert_json (details).get ());
618
607
}
619
608
620
609
} // namespace sdk
0 commit comments