Skip to content

Commit 9af290f

Browse files
committed
Add to_json*/from_json* Deprecation Warnings
Addresses the first half of #267 Add deprecation warnings to all `to_json*` and `from_json*` methods to indicate that these will be removed in a future release. All deprecated methods are also moved to be near each other so they will be easier to remove in the future. This causes the tests to display a lot of warnings and the tests and examples using these methods must also be removed / revised once they are gone for good.
1 parent 48b8897 commit 9af290f

File tree

2 files changed

+52
-44
lines changed

2 files changed

+52
-44
lines changed

lib/bitcoin/protocol/block.rb

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -228,18 +228,44 @@ def bip34_block_height(height = nil)
228228
nil
229229
end
230230

231-
# convert to json representation as seen in the block explorer.
232-
# (see also #from_json)
231+
# <b>DEPRECATED:</b> This method will removed in a future release.
233232
def to_json(options = { space: '' }, *_)
233+
warn "[DEPRECATION] Bitcoin::Protocol::Block#to_json is deprecated and will be removed in a future release."
234234
JSON.pretty_generate(to_hash(options), options)
235235
end
236236

237-
# write json representation to a file
238-
# (see also #to_json)
237+
# <b>DEPRECATED:</b> This method will removed in a future release.
239238
def to_json_file(path)
239+
warn "[DEPRECATION] Bitcoin::Protocol::Block#to_json_file is deprecated and will be removed in a future release."
240240
File.open(path, 'wb') { |f| f.print to_json; }
241241
end
242242

243+
# <b>DEPRECATED:</b> This method will removed in a future release.
244+
def self.from_json(json_string)
245+
warn "[DEPRECATION] Bitcoin::Protocol::Block#from_json is deprecated and will be removed in a future release."
246+
from_hash(JSON.parse(json_string))
247+
end
248+
249+
# <b>DEPRECATED:</b> This method will removed in a future release.
250+
def self.binary_from_json(json_string)
251+
warn "[DEPRECATION] Bitcoin::Protocol::Block#binary_from_json is deprecated and will be removed in a future release."
252+
from_json(json_string).to_payload
253+
end
254+
255+
# <b>DEPRECATED:</b> This method will removed in a future release.
256+
def self.from_json_file(path)
257+
warn "[DEPRECATION] Bitcoin::Protocol::Block#from_json_file is deprecated and will be removed in a future release."
258+
from_json(Bitcoin::Protocol.read_binary_file(path))
259+
end
260+
261+
# <b>DEPRECATED:</b> This method will removed in a future release.
262+
def header_to_json(options = { space: '' })
263+
warn "[DEPRECATION] Bitcoin::Protocol::Block#header_to_json is deprecated and will be removed in a future release."
264+
h = to_hash
265+
%w[tx mrkl_tree].each { |k| h.delete(k) }
266+
JSON.pretty_generate(h, options)
267+
end
268+
243269
# parse ruby hash (see also #to_hash)
244270
def self.from_hash(h, do_raise = true)
245271
blk = new(nil)
@@ -266,23 +292,6 @@ def self.binary_from_hash(h)
266292
from_hash(h).to_payload
267293
end
268294

269-
# parse json representation (see also #to_json)
270-
def self.from_json(json_string)
271-
from_hash(JSON.parse(json_string))
272-
end
273-
274-
# convert json representation to raw binary
275-
def self.binary_from_json(json_string)
276-
from_json(json_string).to_payload
277-
end
278-
279-
# convert header to json representation.
280-
def header_to_json(options = { space: '' })
281-
h = to_hash
282-
%w[tx mrkl_tree].each { |k| h.delete(k) }
283-
JSON.pretty_generate(h, options)
284-
end
285-
286295
# block header binary output
287296
def block_header
288297
[
@@ -295,11 +304,6 @@ def self.from_file(path)
295304
new(Bitcoin::Protocol.read_binary_file(path))
296305
end
297306

298-
# read json block from a file
299-
def self.from_json_file(path)
300-
from_json(Bitcoin::Protocol.read_binary_file(path))
301-
end
302-
303307
# get the (statistical) amount of work that was needed to generate this block.
304308
def block_work
305309
target = Bitcoin.decode_compact_bits(@bits).to_i(16)

lib/bitcoin/protocol/tx.rb

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -451,17 +451,36 @@ def to_hash(options = {})
451451
h
452452
end
453453

454-
# generates rawblock json as seen in the block explorer.
454+
# <b>DEPRECATED:</b> This method will removed in a future release.
455455
def to_json(options = { space: '' }, *_a)
456+
warn "[DEPRECATION] Bitcoin::Protocol::Tx#to_json is deprecated and will be removed in a future release."
456457
JSON.pretty_generate(to_hash(options), options)
457458
end
458459

459-
# write json representation to a file
460-
# (see also #to_json)
460+
# <b>DEPRECATED:</b> This method will removed in a future release.
461461
def to_json_file(path)
462+
warn "[DEPRECATION] Bitcoin::Protocol::Tx#to_json_file is deprecated and will be removed in a future release."
462463
File.open(path, 'wb') { |f| f.print to_json; }
463464
end
464465

466+
# <b>DEPRECATED:</b> This method will removed in a future release.
467+
def self.from_json(json_string)
468+
warn "[DEPRECATION] Bitcoin::Protocol::Tx#from_json is deprecated and will be removed in a future release."
469+
from_hash(JSON.parse(json_string))
470+
end
471+
472+
# <b>DEPRECATED:</b> This method will removed in a future release.
473+
def self.binary_from_json(json_string)
474+
warn "[DEPRECATION] Bitcoin::Protocol::Tx#binary_from_json is deprecated and will be removed in a future release."
475+
from_json(json_string).to_payload
476+
end
477+
478+
# <b>DEPRECATED:</b> This method will removed in a future release.
479+
def self.from_json_file(path)
480+
warn "[DEPRECATION] Bitcoin::Protocol::Tx#from_json_file is deprecated and will be removed in a future release."
481+
from_json(Bitcoin::Protocol.read_binary_file(path))
482+
end
483+
465484
# parse ruby hash (see also #to_hash)
466485
def self.from_hash(h, do_raise = true)
467486
tx = new(nil)
@@ -487,26 +506,11 @@ def self.binary_from_hash(h)
487506
tx.to_payload
488507
end
489508

490-
# parse json representation
491-
def self.from_json(json_string)
492-
from_hash(JSON.parse(json_string))
493-
end
494-
495-
# convert json representation to raw binary
496-
def self.binary_from_json(json_string)
497-
from_json(json_string).to_payload
498-
end
499-
500509
# read binary block from a file
501510
def self.from_file(path)
502511
new(Bitcoin::Protocol.read_binary_file(path))
503512
end
504513

505-
# read json block from a file
506-
def self.from_json_file(path)
507-
from_json(Bitcoin::Protocol.read_binary_file(path))
508-
end
509-
510514
def size
511515
payload.bytesize
512516
end

0 commit comments

Comments
 (0)