File: //proc/self/root/opt/alt/ruby32/share/gems/doc/json-2.16.0/ri/JSON/unsafe_load-i.ri
U:RDoc::AnyMethod[iI"unsafe_load:ETI"JSON#unsafe_load;TF:publico:RDoc::Markup::Document:@parts[o:RDoc::Markup::Paragraph; [I"DReturns the Ruby objects created by parsing the given +source+.;To:RDoc::Markup::BlankLine o;
; [I"MBEWARE: This method is meant to serialise data from trusted user input, ;TI"Plike from your own database server or clients under your control, it could ;TI"Hbe dangerous to allow untrusted users to pass JSON sources into it.;T@o:RDoc::Markup::List:
@type:BULLET:@items[o:RDoc::Markup::ListItem:@label0; [o;
; [I"@Argument +source+ must be, or be convertible to, a \String:;To;;
;;[
o;;0; [o;
; [I"7If +source+ responds to instance method +to_str+, ;TI"/<tt>source.to_str</tt> becomes the source.;To;;0; [o;
; [I"6If +source+ responds to instance method +to_io+, ;TI"3<tt>source.to_io.read</tt> becomes the source.;To;;0; [o;
; [I"5If +source+ responds to instance method +read+, ;TI"-<tt>source.read</tt> becomes the source.;To;;0; [o;
; [I"SIf both of the following are true, source becomes the \String <tt>'null'</tt>:;To;;
;;[o;;0; [o;
; [I"3Option +allow_blank+ specifies a truthy value.;To;;0; [o;
; [I"MThe source, as defined above, is +nil+ or the empty \String <tt>''</tt>.;To;;0; [o;
; [I",Otherwise, +source+ remains the source.;To;;0; [o;
; [I"KArgument +proc+, if given, must be a \Proc that accepts one argument. ;TI"IIt will be called recursively with each result (depth-first order). ;TI"See details below.;To;;0; [o;
; [I"MArgument +opts+, if given, contains a \Hash of options for the parsing. ;TI"@See {Parsing Options}[#module-JSON-label-Parsing+Options]. ;TI"UThe default options can be changed via method JSON.unsafe_load_default_options=.;T@S:RDoc::Markup::Rule:weighti@o;
; [I"SWhen no +proc+ is given, modifies +source+ as above and returns the result of ;TI"/<tt>parse(source, opts)</tt>; see #parse.;T@o;
; [I"#Source for following examples:;To:RDoc::Markup::Verbatim; [I"source = <<~JSON
;TI" {
;TI" "name": "Dave",
;TI" "age" :40,
;TI" "hats": [
;TI" "Cattleman's",
;TI" "Panama",
;TI" "Tophat"
;TI" ]
;TI" }
;TI"
JSON
;T:@format0o;
; [I"Load a \String:;To;; [I"%ruby = JSON.unsafe_load(source)
;TI"Xruby # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]}
;T;0o;
; [I"Load an \IO object:;To;; [I"require 'stringio'
;TI"5object = JSON.unsafe_load(StringIO.new(source))
;TI"Zobject # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]}
;T;0o;
; [I"Load a \File object:;To;; [
I"path = 't.json'
;TI"File.write(path, source)
;TI"File.open(path) do |file|
;TI" JSON.unsafe_load(file)
;TI"Wend # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]}
;T;0S;;i@o;
; [I"When +proc+ is given:;To;;
;;[ o;;0; [o;
; [I" Modifies +source+ as above.;To;;0; [o;
; [I"AGets the +result+ from calling <tt>parse(source, opts)</tt>.;To;;0; [o;
; [I"-Recursively calls <tt>proc(result)</tt>.;To;;0; [o;
; [I"Returns the final result.;T@o;
; [I"
Example:;To;; [.I"require 'json'
;TI"
;TI"%# Some classes for the example.
;TI"class Base
;TI"" def initialize(attributes)
;TI"" @attributes = attributes
;TI" end
;TI" end
;TI"class User < Base; end
;TI"class Account < Base; end
;TI"class Admin < Base; end
;TI"# The JSON source.
;TI"json = <<-EOF
;TI"{
;TI" "users": [
;TI"N {"type": "User", "username": "jane", "email": "jane@example.com"},
;TI"M {"type": "User", "username": "john", "email": "john@example.com"}
;TI"
],
;TI" "accounts": [
;TI"Q {"account": {"type": "Account", "paid": true, "account_id": "1234"}},
;TI"Q {"account": {"type": "Account", "paid": false, "account_id": "1235"}}
;TI"
],
;TI"8 "admins": {"type": "Admin", "password": "0wn3d"}
;TI"}
;TI" EOF
;TI"# Deserializer method.
;TI"Cdef deserialize_obj(obj, safe_types = %w(User Account Admin))
;TI"- type = obj.is_a?(Hash) && obj["type"]
;TI"I safe_types.include?(type) ? Object.const_get(type).new(obj) : obj
;TI" end
;TI" # Call to JSON.unsafe_load
;TI"/ruby = JSON.unsafe_load(json, proc {|obj|
;TI" case obj
;TI" when Hash
;TI"7 obj.each {|k, v| obj[k] = deserialize_obj v }
;TI" when Array
;TI"+ obj.map! {|v| deserialize_obj v }
;TI" end
;TI" obj
;TI"})
;TI"
pp ruby
;T;0o;
; [I"Output:;To;; [I"{"users"=>
;TI"# [#<User:0x00000000064c4c98
;TI" @attributes=
;TI"P {"type"=>"User", "username"=>"jane", "email"=>"jane@example.com"}>,
;TI"$ #<User:0x00000000064c4bd0
;TI" @attributes=
;TI"Q {"type"=>"User", "username"=>"john", "email"=>"john@example.com"}>],
;TI" "accounts"=>
;TI" [{"account"=>
;TI") #<Account:0x00000000064c4928
;TI"S @attributes={"type"=>"Account", "paid"=>true, "account_id"=>"1234"}>},
;TI" {"account"=>
;TI") #<Account:0x00000000064c4680
;TI"U @attributes={"type"=>"Account", "paid"=>false, "account_id"=>"1235"}>}],
;TI" "admins"=>
;TI"# #<Admin:0x00000000064c41f8
;TI"< @attributes={"type"=>"Admin", "password"=>"0wn3d"}>};T;0:
@fileI"lib/json/common.rb;T:0@omit_headings_from_table_of_contents_below0I"BJSON.unsafe_load(source, proc = nil, options = {}) -> object
;T0[ I"((source, proc = nil, options = nil);T@�FI" JSON;TcRDoc::NormalModule00