@@ -47,58 +47,88 @@ std::string to_json(const ptree& tree)
47
47
return json_stream.str ();
48
48
}
49
49
50
- std::string to_json (const ptree& tree, uint32_t id)
50
+ std::string to_json (const ptree& tree, uint32_t id, bool /* rpc */ )
51
51
{
52
52
ptree result_tree;
53
53
result_tree.add_child (" result" , tree);
54
54
result_tree.put (" id" , id);
55
55
return to_json (result_tree);
56
56
}
57
57
58
- std::string to_json (uint64_t height, uint32_t id)
58
+ std::string to_json (uint64_t height, uint32_t id, bool rpc )
59
59
{
60
+ if (!rpc)
61
+ return to_json (property_tree (height, id));
62
+
60
63
ptree tree;
61
64
tree.put (" result" , height);
62
65
tree.put (" id" , id);
63
66
return to_json (tree);
64
-
65
- // TODO: The bc::property_tree call works fine, but the format is
66
- // different than expected for json_rpc so eventually we need to
67
- // separate out property_tree and json_rpc::property_tree, or
68
- // something along the lines to make this a clear distinction.
69
- // // return to_json(property_tree(height, id));
70
67
}
71
68
72
- std::string to_json (const code& code, uint32_t id)
69
+ std::string to_json (const code& code, uint32_t id, bool rpc )
73
70
{
71
+ if (!rpc)
72
+ return to_json (property_tree (code, id));
73
+
74
74
ptree tree;
75
75
ptree error_tree;
76
76
error_tree.put (" code" , code.value ());
77
77
error_tree.put (" message" , code.message ());
78
78
tree.add_child (" error" , error_tree);
79
79
tree.put (" id" , id);
80
80
return to_json (tree);
81
- // // return to_json(property_tree(code, id));
82
81
}
83
82
84
- std::string to_json (const chain::header& header, uint32_t id)
83
+ std::string to_json (const hash_digest& hash, uint32_t id, bool rpc)
84
+ {
85
+ ptree tree;
86
+ if (rpc)
87
+ tree.put (" result" , encode_hash (hash));
88
+ else
89
+ tree = property_tree (hash);
90
+ tree.put (" id" , id);
91
+ return to_json (tree);
92
+ }
93
+
94
+ std::string to_json (const chain::header& header, uint32_t id, bool rpc)
85
95
{
86
- return to_json (property_tree (config::header (header)), id);
96
+ if (!rpc)
97
+ return to_json (property_tree (config::header (header)), id, rpc);
98
+
99
+ auto hex = [](uint32_t value)
100
+ {
101
+ std::stringstream hex_value;
102
+ hex_value << std::setfill (' 0' ) << std::setw (sizeof (uint32_t ) * 2 );
103
+ hex_value << std::hex << value;
104
+ return hex_value.str ();
105
+ };
106
+
107
+ ptree tree;
108
+ tree.put (" hash" , encode_hash (header.hash ()));
109
+ tree.put (" version" , header.version ());
110
+ tree.put (" versionHex" , hex (header.version ()));
111
+ tree.put (" merkleroot" , encode_hash (header.merkle ()));
112
+ tree.put (" time" , header.timestamp ());
113
+ tree.put (" nonce" , header.nonce ());
114
+ tree.put (" bits" , hex (header.bits ()));
115
+ return to_json (tree, id, rpc);
87
116
}
88
117
89
- std::string to_json (const chain::block& block, uint32_t id)
118
+ std::string to_json (const chain::block& block, uint32_t id, bool rpc )
90
119
{
91
- return to_json (property_tree (block, true ), id);
120
+ return to_json (property_tree (block, true ), id, rpc );
92
121
}
93
122
94
- std::string to_json (const chain::block& block, uint32_t , uint32_t id)
123
+ std::string to_json (const chain::block& block, uint32_t , uint32_t id, bool rpc )
95
124
{
96
- return to_json (property_tree (config::header (block.header ())), id);
125
+ return to_json (property_tree (config::header (block.header ())), id, rpc );
97
126
}
98
127
99
- std::string to_json (const chain::transaction& transaction, uint32_t id)
128
+ std::string to_json (const chain::transaction& transaction, uint32_t id,
129
+ bool rpc)
100
130
{
101
- return to_json (property_tree (config::transaction (transaction), true ), id);
131
+ return to_json (property_tree (config::transaction (transaction), true ), id, rpc );
102
132
}
103
133
104
134
} // namespace http
0 commit comments