From a55c91934e5146cfafda42d59863fcbe5ede85d7 Mon Sep 17 00:00:00 2001 From: BurdetteLamar Date: Tue, 28 Jul 2020 13:57:16 -0500 Subject: [PATCH] RDoc for JSON.load with proc --- lib/json/common.rb | 76 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 58 insertions(+), 18 deletions(-) diff --git a/lib/json/common.rb b/lib/json/common.rb index f5b45ab1..6ed00b3c 100644 --- a/lib/json/common.rb +++ b/lib/json/common.rb @@ -441,7 +441,12 @@ class << self # See {Parsing Options}[#module-JSON-label-Parsing+Options]. # The default options can be changed via method JSON.load_default_options=. # - # Examples in this section assume prior execution of: + # --- + # + # When no +proc+ is given, modifies +source+ as above and returns the result of + # parse(source, opts); see #parse. + # + # Source for following examples: # source = <<-EOT # { # "name": "Dave", @@ -454,11 +459,6 @@ class << self # } # EOT # - # --- - # - # When +proc+ is not given, modifies +source+ as above and returns the result of - # parse(source, opts); see #parse. - # # Load a \String: # ruby = JSON.load(source) # ruby # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]} @@ -484,25 +484,65 @@ class << self # - Returns the final result. # # Example: - # def mung(obj) - # case obj - # when String - # obj.upcase - # when Integer - # obj * 100 - # else - # obj + # require 'json' + # + # # Some classes for the example. + # class Base + # def initialize(attributes) + # @attributes = attributes # end # end - # new_obj = JSON.load(source, proc {|obj| + # class User < Base; end + # class Account < Base; end + # class Admin < Base; end + # # The JSON source. + # json = <<-EOF + # { + # "users": [ + # {"type": "User", "username": "jane", "email": "jane@example.com"}, + # {"type": "User", "username": "john", "email": "john@example.com"} + # ], + # "accounts": [ + # {"account": {"type": "Account", "paid": true, "account_id": "1234"}}, + # {"account": {"type": "Account", "paid": false, "account_id": "1235"}} + # ], + # "admins": {"type": "Admin", "password": "0wn3d"} + # } + # EOF + # # Deserializer method. + # def deserialize_obj(obj, safe_types = %w(User Account Admin)) + # type = obj.is_a?(Hash) && obj["type"] + # safe_types.include?(type) ? Object.const_get(type).new(obj) : obj + # end + # # Call to JSON.load + # ruby = JSON.load(json, proc {|obj| # case obj # when Hash - # obj.each {|k, v| obj[k] = mung(v) } + # obj.each {|k, v| obj[k] = deserialize_obj v } # when Array - # obj.map! {|v| mung(v) } + # obj.map! {|v| deserialize_obj v } # end # }) - # new_obj # => {"name"=>"DAVE", "age"=>4000, "hats"=>["CATTLEMAN'S", "PANAMA", "TOPHAT"]} + # pp ruby + # Output: + # {"users"=> + # [#"User", "username"=>"jane", "email"=>"jane@example.com"}>, + # #"User", "username"=>"john", "email"=>"john@example.com"}>], + # "accounts"=> + # [{"account"=> + # #"Account", "paid"=>true, "account_id"=>"1234"}>}, + # {"account"=> + # #"Account", "paid"=>false, "account_id"=>"1235"}>}], + # "admins"=> + # #"Admin", "password"=>"0wn3d"}>} + # def load(source, proc = nil, options = {}) opts = load_default_options.merge options if source.respond_to? :to_str