From 46b33c9dc689195ac27aecd20255ed7e487679b4 Mon Sep 17 00:00:00 2001 From: BurdetteLamar Date: Thu, 16 Nov 2023 14:59:31 -0600 Subject: [PATCH] RDoc for additions --- lib/json/add/bigdecimal.rb | 38 ++++++++++++++++++++++++++++++------ lib/json/add/range.rb | 40 ++++++++++++++++++++++++-------------- 2 files changed, 57 insertions(+), 21 deletions(-) diff --git a/lib/json/add/bigdecimal.rb b/lib/json/add/bigdecimal.rb index 25383f28..ae35c27e 100644 --- a/lib/json/add/bigdecimal.rb +++ b/lib/json/add/bigdecimal.rb @@ -8,16 +8,30 @@ end class BigDecimal - # Import a JSON Marshalled object. - # - # method used for JSON marshalling support. + + # See #as_json. def self.json_create(object) BigDecimal._load object['b'] end - # Marshal the object to JSON. + # Methods BigDecimal#as_json and +BigDecimal.json_create+ may be used + # to serialize and deserialize a \BigDecimal object; + # see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html]. + # + # \Method BigDecimal#as_json serializes +self+, + # returning a 2-element hash representing +self+: + # + # require 'json/add/bigdecimal' + # x = BigDecimal(2).as_json # => {"json_class"=>"BigDecimal", "b"=>"27:0.2e1"} + # y = BigDecimal(2.0, 4).as_json # => {"json_class"=>"BigDecimal", "b"=>"36:0.2e1"} + # z = BigDecimal(Complex(2, 0)).as_json # => {"json_class"=>"BigDecimal", "b"=>"27:0.2e1"} + # + # \Method +JSON.create+ deserializes such a hash, returning a \BigDecimal object: + # + # BigDecimal.json_create(x) # => 0.2e1 + # BigDecimal.json_create(y) # => 0.2e1 + # BigDecimal.json_create(z) # => 0.2e1 # - # method used for JSON marshalling support. def as_json(*) { JSON.create_id => self.class.name, @@ -25,7 +39,19 @@ def as_json(*) } end - # return the JSON value + # Returns a JSON string representing +self+: + # + # require 'json/add/bigdecimal' + # puts BigDecimal(2).to_json + # puts BigDecimal(2.0, 4).to_json + # puts BigDecimal(Complex(2, 0)).to_json + # + # Output: + # + # {"json_class":"BigDecimal","b":"27:0.2e1"} + # {"json_class":"BigDecimal","b":"36:0.2e1"} + # {"json_class":"BigDecimal","b":"27:0.2e1"} + # def to_json(*args) as_json.to_json(*args) end diff --git a/lib/json/add/range.rb b/lib/json/add/range.rb index d1210c56..1fc8dcde 100644 --- a/lib/json/add/range.rb +++ b/lib/json/add/range.rb @@ -5,24 +5,28 @@ class Range - # Returns a new \Range object constructed from object['a'], - # which must be an array of values suitable for a call to Range.new: - # - # require 'json/add/range' - # Range.json_create({"a"=>[1, 4]}) # => 1..4 - # Range.json_create({"a"=>[1, 4, true]}) # => 1...4 - # Range.json_create({"a" => ['a', 'd']}) # => "a".."d" - # + # See #as_json. def self.json_create(object) new(*object['a']) end - # Returns a 2-element hash representing +self+: + # Methods Range#as_json and +Range.json_create+ may be used + # to serialize and deserialize a \Range object; + # see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html]. + # + # \Method Range#as_json serializes +self+, + # returning a 2-element hash representing +self+: # # require 'json/add/range' - # (1..4).as_json # => {"json_class"=>"Range", "a"=>[1, 4, false]} - # (1...4).as_json # => {"json_class"=>"Range", "a"=>[1, 4, true]} - # ('a'..'d').as_json # => {"json_class"=>"Range", "a"=>["a", "d", false]} + # x = (1..4).as_json # => {"json_class"=>"Range", "a"=>[1, 4, false]} + # y = (1...4).as_json # => {"json_class"=>"Range", "a"=>[1, 4, true]} + # z = ('a'..'d').as_json # => {"json_class"=>"Range", "a"=>["a", "d", false]} + # + # \Method +JSON.create+ deserializes such a hash, returning a \Range object: + # + # Range.json_create(x) # => 1..4 + # Range.json_create(y) # => 1...4 + # Range.json_create(z) # => "a".."d" # def as_json(*) { @@ -34,9 +38,15 @@ def as_json(*) # Returns a JSON string representing +self+: # # require 'json/add/range' - # (1..4).to_json # => "{\"json_class\":\"Range\",\"a\":[1,4,false]}" - # (1...4).to_json # => "{\"json_class\":\"Range\",\"a\":[1,4,true]}" - # ('a'..'d').to_json # => "{\"json_class\":\"Range\",\"a\":[\"a\",\"d\",false]}" + # puts (1..4).to_json + # puts (1...4).to_json + # puts ('a'..'d').to_json + # + # Output: + # + # {"json_class":"Range","a":[1,4,false]} + # {"json_class":"Range","a":[1,4,true]} + # {"json_class":"Range","a":["a","d",false]} # def to_json(*args) as_json.to_json(*args)