Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add to_json* / from_json* Deprecation Warnings #281

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 30 additions & 26 deletions lib/bitcoin/protocol/block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,18 +228,44 @@ def bip34_block_height(height = nil)
nil
end

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

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

# <b>DEPRECATED:</b> This method will removed in a future release.
def self.from_json(json_string)
warn "[DEPRECATION] Bitcoin::Protocol::Block#from_json is deprecated and will be removed in a future release."
from_hash(JSON.parse(json_string))
end

# <b>DEPRECATED:</b> This method will removed in a future release.
def self.binary_from_json(json_string)
warn "[DEPRECATION] Bitcoin::Protocol::Block#binary_from_json is deprecated and will be removed in a future release."
from_json(json_string).to_payload
end

# <b>DEPRECATED:</b> This method will removed in a future release.
def self.from_json_file(path)
warn "[DEPRECATION] Bitcoin::Protocol::Block#from_json_file is deprecated and will be removed in a future release."
from_json(Bitcoin::Protocol.read_binary_file(path))
end

# <b>DEPRECATED:</b> This method will removed in a future release.
def header_to_json(options = { space: '' })
warn "[DEPRECATION] Bitcoin::Protocol::Block#header_to_json is deprecated and will be removed in a future release."
h = to_hash
%w[tx mrkl_tree].each { |k| h.delete(k) }
JSON.pretty_generate(h, options)
end

# parse ruby hash (see also #to_hash)
def self.from_hash(h, do_raise = true)
blk = new(nil)
Expand All @@ -266,23 +292,6 @@ def self.binary_from_hash(h)
from_hash(h).to_payload
end

# parse json representation (see also #to_json)
def self.from_json(json_string)
from_hash(JSON.parse(json_string))
end

# convert json representation to raw binary
def self.binary_from_json(json_string)
from_json(json_string).to_payload
end

# convert header to json representation.
def header_to_json(options = { space: '' })
h = to_hash
%w[tx mrkl_tree].each { |k| h.delete(k) }
JSON.pretty_generate(h, options)
end

# block header binary output
def block_header
[
Expand All @@ -295,11 +304,6 @@ def self.from_file(path)
new(Bitcoin::Protocol.read_binary_file(path))
end

# read json block from a file
def self.from_json_file(path)
from_json(Bitcoin::Protocol.read_binary_file(path))
end

# get the (statistical) amount of work that was needed to generate this block.
def block_work
target = Bitcoin.decode_compact_bits(@bits).to_i(16)
Expand Down
40 changes: 22 additions & 18 deletions lib/bitcoin/protocol/tx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -451,17 +451,36 @@ def to_hash(options = {})
h
end

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

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

# <b>DEPRECATED:</b> This method will removed in a future release.
def self.from_json(json_string)
warn "[DEPRECATION] Bitcoin::Protocol::Tx#from_json is deprecated and will be removed in a future release."
from_hash(JSON.parse(json_string))
end

# <b>DEPRECATED:</b> This method will removed in a future release.
def self.binary_from_json(json_string)
warn "[DEPRECATION] Bitcoin::Protocol::Tx#binary_from_json is deprecated and will be removed in a future release."
from_json(json_string).to_payload
end

# <b>DEPRECATED:</b> This method will removed in a future release.
def self.from_json_file(path)
warn "[DEPRECATION] Bitcoin::Protocol::Tx#from_json_file is deprecated and will be removed in a future release."
from_json(Bitcoin::Protocol.read_binary_file(path))
end

# parse ruby hash (see also #to_hash)
def self.from_hash(h, do_raise = true)
tx = new(nil)
Expand All @@ -487,26 +506,11 @@ def self.binary_from_hash(h)
tx.to_payload
end

# parse json representation
def self.from_json(json_string)
from_hash(JSON.parse(json_string))
end

# convert json representation to raw binary
def self.binary_from_json(json_string)
from_json(json_string).to_payload
end

# read binary block from a file
def self.from_file(path)
new(Bitcoin::Protocol.read_binary_file(path))
end

# read json block from a file
def self.from_json_file(path)
from_json(Bitcoin::Protocol.read_binary_file(path))
end

def size
payload.bytesize
end
Expand Down