@@ -57,16 +57,60 @@ std::string to_json(const ptree& tree, uint32_t id)
57
57
58
58
std::string to_json (uint64_t height, uint32_t id)
59
59
{
60
- ptree tree;
61
- tree.put (" result" , height);
60
+ return to_json (property_tree (height, id));
61
+ }
62
+
63
+ std::string to_json (const code& code, uint32_t id)
64
+ {
65
+ return to_json (property_tree (code, id));
66
+ }
67
+
68
+ std::string to_json (const hash_digest& hash, uint32_t id)
69
+ {
70
+ auto tree = property_tree (hash);
62
71
tree.put (" id" , id);
63
72
return to_json (tree);
73
+ }
64
74
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));
75
+ std::string to_json (const chain::header& header, uint32_t id)
76
+ {
77
+ return to_json (property_tree (config::header (header)), id);
78
+ }
79
+
80
+ std::string to_json (const chain::block& block, uint32_t id)
81
+ {
82
+ return to_json (property_tree (block, true ), id);
83
+ }
84
+
85
+ std::string to_json (const chain::block& block, uint32_t , uint32_t id)
86
+ {
87
+ return to_json (property_tree (config::header (block.header ())), id);
88
+ }
89
+
90
+ std::string to_json (const chain::transaction& transaction, uint32_t id)
91
+ {
92
+ return to_json (property_tree (config::transaction (transaction), true ), id);
93
+ }
94
+
95
+ // Object to JSON rpc converters.
96
+ // -----------------------------------------------------------------------------
97
+
98
+ namespace rpc {
99
+
100
+ std::string to_json (const ptree& tree, uint32_t id)
101
+ {
102
+ ptree result_tree;
103
+ result_tree.add_child (" result" , tree);
104
+ result_tree.put (" id" , id);
105
+ return http::to_json (result_tree);
106
+ }
107
+
108
+ std::string to_json (uint64_t height, uint32_t id)
109
+ {
110
+ ptree tree;
111
+ tree.put (" result" , height);
112
+ tree.put (" id" , id);
113
+ return http::to_json (tree);
70
114
}
71
115
72
116
std::string to_json (const code& code, uint32_t id)
@@ -77,13 +121,36 @@ std::string to_json(const code& code, uint32_t id)
77
121
error_tree.put (" message" , code.message ());
78
122
tree.add_child (" error" , error_tree);
79
123
tree.put (" id" , id);
80
- return to_json (tree);
81
- // // return to_json(property_tree(code, id));
124
+ return http::to_json (tree);
125
+ }
126
+
127
+ std::string to_json (const hash_digest& hash, uint32_t id)
128
+ {
129
+ ptree tree;
130
+ tree.put (" result" , encode_hash (hash));
131
+ tree.put (" id" , id);
132
+ return http::to_json (tree);
82
133
}
83
134
84
135
std::string to_json (const chain::header& header, uint32_t id)
85
136
{
86
- return to_json (property_tree (config::header (header)), id);
137
+ auto hex = [](uint32_t value)
138
+ {
139
+ std::stringstream hex_value;
140
+ hex_value << std::setfill (' 0' ) << std::setw (sizeof (uint32_t ) * 2 );
141
+ hex_value << std::hex << value;
142
+ return hex_value.str ();
143
+ };
144
+
145
+ ptree tree;
146
+ tree.put (" hash" , encode_hash (header.hash ()));
147
+ tree.put (" version" , header.version ());
148
+ tree.put (" versionHex" , hex (header.version ()));
149
+ tree.put (" merkleroot" , encode_hash (header.merkle_root ()));
150
+ tree.put (" time" , header.timestamp ());
151
+ tree.put (" nonce" , header.nonce ());
152
+ tree.put (" bits" , hex (header.bits ()));
153
+ return to_json (tree, id);
87
154
}
88
155
89
156
std::string to_json (const chain::block& block, uint32_t id)
@@ -93,14 +160,17 @@ std::string to_json(const chain::block& block, uint32_t id)
93
160
94
161
std::string to_json (const chain::block& block, uint32_t , uint32_t id)
95
162
{
96
- return to_json (property_tree ( config::header ( block.header ()) ), id);
163
+ return to_json (block.header (), id);
97
164
}
98
165
99
166
std::string to_json (const chain::transaction& transaction, uint32_t id)
100
167
{
101
168
return to_json (property_tree (config::transaction (transaction), true ), id);
102
169
}
103
170
171
+ } // namespace rpc
172
+
173
+
104
174
} // namespace http
105
175
} // namespace protocol
106
176
} // namespace libbitcoin
0 commit comments