1181 lines
52 KiB
HTML
1181 lines
52 KiB
HTML
|
<?xml version="1.0" encoding="UTF-8"?>
|
|||
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
|
|||
|
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
|||
|
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
|||
|
<head>
|
|||
|
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
|
|||
|
|
|||
|
<title>Module: JSON</title>
|
|||
|
|
|||
|
<link rel="stylesheet" href="./rdoc.css" type="text/css" media="screen" />
|
|||
|
|
|||
|
<script src="./js/jquery.js" type="text/javascript" charset="utf-8"></script>
|
|||
|
<script src="./js/thickbox-compressed.js" type="text/javascript" charset="utf-8"></script>
|
|||
|
<script src="./js/quicksearch.js" type="text/javascript" charset="utf-8"></script>
|
|||
|
<script src="./js/darkfish.js" type="text/javascript" charset="utf-8"></script>
|
|||
|
|
|||
|
</head>
|
|||
|
<body id="top" class="module">
|
|||
|
|
|||
|
<div id="metadata">
|
|||
|
<div id="home-metadata">
|
|||
|
<div id="home-section" class="section">
|
|||
|
<h3 class="section-header">
|
|||
|
<a href="./index.html">Home</a>
|
|||
|
<a href="./index.html#classes">Classes</a>
|
|||
|
<a href="./index.html#methods">Methods</a>
|
|||
|
</h3>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
|
|||
|
<div id="file-metadata">
|
|||
|
<div id="file-list-section" class="section">
|
|||
|
<h3 class="section-header">In Files</h3>
|
|||
|
<div class="section-body">
|
|||
|
<ul>
|
|||
|
|
|||
|
<li><a href="./lib/json_rb.html?TB_iframe=true&height=550&width=785"
|
|||
|
class="thickbox" title="lib/json.rb">lib/json.rb</a></li>
|
|||
|
|
|||
|
<li><a href="./lib/json/common_rb.html?TB_iframe=true&height=550&width=785"
|
|||
|
class="thickbox" title="lib/json/common.rb">lib/json/common.rb</a></li>
|
|||
|
|
|||
|
<li><a href="./lib/json/ext_rb.html?TB_iframe=true&height=550&width=785"
|
|||
|
class="thickbox" title="lib/json/ext.rb">lib/json/ext.rb</a></li>
|
|||
|
|
|||
|
<li><a href="./lib/json/pure_rb.html?TB_iframe=true&height=550&width=785"
|
|||
|
class="thickbox" title="lib/json/pure.rb">lib/json/pure.rb</a></li>
|
|||
|
|
|||
|
<li><a href="./lib/json/version_rb.html?TB_iframe=true&height=550&width=785"
|
|||
|
class="thickbox" title="lib/json/version.rb">lib/json/version.rb</a></li>
|
|||
|
|
|||
|
<li><a href="./lib/json/generic_object_rb.html?TB_iframe=true&height=550&width=785"
|
|||
|
class="thickbox" title="lib/json/generic_object.rb">lib/json/generic_object.rb</a></li>
|
|||
|
|
|||
|
<li><a href="./lib/json/pure/generator_rb.html?TB_iframe=true&height=550&width=785"
|
|||
|
class="thickbox" title="lib/json/pure/generator.rb">lib/json/pure/generator.rb</a></li>
|
|||
|
|
|||
|
<li><a href="./lib/json/pure/parser_rb.html?TB_iframe=true&height=550&width=785"
|
|||
|
class="thickbox" title="lib/json/pure/parser.rb">lib/json/pure/parser.rb</a></li>
|
|||
|
|
|||
|
</ul>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
</div>
|
|||
|
|
|||
|
<div id="class-metadata">
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<!-- Namespace Contents -->
|
|||
|
<div id="namespace-list-section" class="section">
|
|||
|
<h3 class="section-header">Namespace</h3>
|
|||
|
<ul class="link-list">
|
|||
|
|
|||
|
<li><span class="type">MODULE</span> <a href="JSON/Ext.html">JSON::Ext</a></li>
|
|||
|
|
|||
|
<li><span class="type">MODULE</span> <a href="JSON/Pure.html">JSON::Pure</a></li>
|
|||
|
|
|||
|
<li><span class="type">CLASS</span> <a href="JSON/CircularDatastructure.html">JSON::CircularDatastructure</a></li>
|
|||
|
|
|||
|
<li><span class="type">CLASS</span> <a href="JSON/GeneratorError.html">JSON::GeneratorError</a></li>
|
|||
|
|
|||
|
<li><span class="type">CLASS</span> <a href="JSON/GenericObject.html">JSON::GenericObject</a></li>
|
|||
|
|
|||
|
<li><span class="type">CLASS</span> <a href="JSON/JSONError.html">JSON::JSONError</a></li>
|
|||
|
|
|||
|
<li><span class="type">CLASS</span> <a href="JSON/MissingUnicodeSupport.html">JSON::MissingUnicodeSupport</a></li>
|
|||
|
|
|||
|
<li><span class="type">CLASS</span> <a href="JSON/NestingError.html">JSON::NestingError</a></li>
|
|||
|
|
|||
|
<li><span class="type">CLASS</span> <a href="JSON/ParserError.html">JSON::ParserError</a></li>
|
|||
|
|
|||
|
<li><span class="type">CLASS</span> <a href="JSON/GeneratorError.html">JSON::UnparserError</a></li>
|
|||
|
|
|||
|
</ul>
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<!-- Method Quickref -->
|
|||
|
<div id="method-list-section" class="section">
|
|||
|
<h3 class="section-header">Methods</h3>
|
|||
|
<ul class="link-list">
|
|||
|
|
|||
|
<li><a href="#method-c-5B-5D">::[]</a></li>
|
|||
|
|
|||
|
<li><a href="#method-c-const_defined_in-3F">::const_defined_in?</a></li>
|
|||
|
|
|||
|
<li><a href="#method-c-iconv">::iconv</a></li>
|
|||
|
|
|||
|
<li><a href="#method-c-restore">::restore</a></li>
|
|||
|
|
|||
|
<li><a href="#method-c-valid_utf8-3F">::valid_utf8?</a></li>
|
|||
|
|
|||
|
<li><a href="#method-i-dump">#dump</a></li>
|
|||
|
|
|||
|
<li><a href="#method-i-fast_generate">#fast_generate</a></li>
|
|||
|
|
|||
|
<li><a href="#method-i-generate">#generate</a></li>
|
|||
|
|
|||
|
<li><a href="#method-i-load">#load</a></li>
|
|||
|
|
|||
|
<li><a href="#method-i-parse">#parse</a></li>
|
|||
|
|
|||
|
<li><a href="#method-i-parse-21">#parse!</a></li>
|
|||
|
|
|||
|
<li><a href="#method-i-pretty_generate">#pretty_generate</a></li>
|
|||
|
|
|||
|
<li><a href="#method-i-recurse_proc">#recurse_proc</a></li>
|
|||
|
|
|||
|
</ul>
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
</div>
|
|||
|
|
|||
|
<div id="project-metadata">
|
|||
|
|
|||
|
|
|||
|
<div id="fileindex-section" class="section project-section">
|
|||
|
<h3 class="section-header">Files</h3>
|
|||
|
<ul>
|
|||
|
|
|||
|
<li class="file"><a href="./README_rdoc.html">README.rdoc</a></li>
|
|||
|
|
|||
|
</ul>
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
<div id="classindex-section" class="section project-section">
|
|||
|
<h3 class="section-header">Class/Module Index
|
|||
|
<span class="search-toggle"><img src="./images/find.png"
|
|||
|
height="16" width="16" alt="[+]"
|
|||
|
title="show/hide quicksearch" /></span></h3>
|
|||
|
<form action="#" method="get" accept-charset="utf-8" class="initially-hidden">
|
|||
|
<fieldset>
|
|||
|
<legend>Quicksearch</legend>
|
|||
|
<input type="text" name="quicksearch" value=""
|
|||
|
class="quicksearch-field" />
|
|||
|
</fieldset>
|
|||
|
</form>
|
|||
|
|
|||
|
<ul class="link-list">
|
|||
|
|
|||
|
<li><a href="./JSON.html">JSON</a></li>
|
|||
|
|
|||
|
<li><a href="./JSON/Ext.html">JSON::Ext</a></li>
|
|||
|
|
|||
|
<li><a href="./JSON/GeneratorError.html">JSON::GeneratorError</a></li>
|
|||
|
|
|||
|
<li><a href="./JSON/GenericObject.html">JSON::GenericObject</a></li>
|
|||
|
|
|||
|
<li><a href="./JSON/JSONError.html">JSON::JSONError</a></li>
|
|||
|
|
|||
|
<li><a href="./JSON/MissingUnicodeSupport.html">JSON::MissingUnicodeSupport</a></li>
|
|||
|
|
|||
|
<li><a href="./JSON/NestingError.html">JSON::NestingError</a></li>
|
|||
|
|
|||
|
<li><a href="./JSON/ParserError.html">JSON::ParserError</a></li>
|
|||
|
|
|||
|
<li><a href="./JSON/Pure.html">JSON::Pure</a></li>
|
|||
|
|
|||
|
<li><a href="./JSON/Pure/Generator.html">JSON::Pure::Generator</a></li>
|
|||
|
|
|||
|
<li><a href="./JSON/Pure/Generator/GeneratorMethods.html">JSON::Pure::Generator::GeneratorMethods</a></li>
|
|||
|
|
|||
|
<li><a href="./JSON/Pure/Generator/GeneratorMethods/Array.html">JSON::Pure::Generator::GeneratorMethods::Array</a></li>
|
|||
|
|
|||
|
<li><a href="./JSON/Pure/Generator/GeneratorMethods/FalseClass.html">JSON::Pure::Generator::GeneratorMethods::FalseClass</a></li>
|
|||
|
|
|||
|
<li><a href="./JSON/Pure/Generator/GeneratorMethods/Float.html">JSON::Pure::Generator::GeneratorMethods::Float</a></li>
|
|||
|
|
|||
|
<li><a href="./JSON/Pure/Generator/GeneratorMethods/Hash.html">JSON::Pure::Generator::GeneratorMethods::Hash</a></li>
|
|||
|
|
|||
|
<li><a href="./JSON/Pure/Generator/GeneratorMethods/Integer.html">JSON::Pure::Generator::GeneratorMethods::Integer</a></li>
|
|||
|
|
|||
|
<li><a href="./JSON/Pure/Generator/GeneratorMethods/NilClass.html">JSON::Pure::Generator::GeneratorMethods::NilClass</a></li>
|
|||
|
|
|||
|
<li><a href="./JSON/Pure/Generator/GeneratorMethods/Object.html">JSON::Pure::Generator::GeneratorMethods::Object</a></li>
|
|||
|
|
|||
|
<li><a href="./JSON/Pure/Generator/GeneratorMethods/String.html">JSON::Pure::Generator::GeneratorMethods::String</a></li>
|
|||
|
|
|||
|
<li><a href="./JSON/Pure/Generator/GeneratorMethods/String/Extend.html">JSON::Pure::Generator::GeneratorMethods::String::Extend</a></li>
|
|||
|
|
|||
|
<li><a href="./JSON/Pure/Generator/GeneratorMethods/TrueClass.html">JSON::Pure::Generator::GeneratorMethods::TrueClass</a></li>
|
|||
|
|
|||
|
<li><a href="./JSON/Pure/Generator/State.html">JSON::Pure::Generator::State</a></li>
|
|||
|
|
|||
|
<li><a href="./JSON/Pure/Parser.html">JSON::Pure::Parser</a></li>
|
|||
|
|
|||
|
<li><a href="./JSON/GeneratorError.html">JSON::UnparserError</a></li>
|
|||
|
|
|||
|
<li><a href="./BigDecimal.html">BigDecimal</a></li>
|
|||
|
|
|||
|
<li><a href="./Class.html">Class</a></li>
|
|||
|
|
|||
|
<li><a href="./Complex.html">Complex</a></li>
|
|||
|
|
|||
|
<li><a href="./Date.html">Date</a></li>
|
|||
|
|
|||
|
<li><a href="./DateTime.html">DateTime</a></li>
|
|||
|
|
|||
|
<li><a href="./Exception.html">Exception</a></li>
|
|||
|
|
|||
|
<li><a href="./Kernel.html">Kernel</a></li>
|
|||
|
|
|||
|
<li><a href="./OpenStruct.html">OpenStruct</a></li>
|
|||
|
|
|||
|
<li><a href="./Range.html">Range</a></li>
|
|||
|
|
|||
|
<li><a href="./Rational.html">Rational</a></li>
|
|||
|
|
|||
|
<li><a href="./Regexp.html">Regexp</a></li>
|
|||
|
|
|||
|
<li><a href="./Struct.html">Struct</a></li>
|
|||
|
|
|||
|
<li><a href="./Symbol.html">Symbol</a></li>
|
|||
|
|
|||
|
<li><a href="./Time.html">Time</a></li>
|
|||
|
|
|||
|
</ul>
|
|||
|
<div id="no-class-search-results" style="display: none;">No matching classes.</div>
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
|
|||
|
<div id="documentation">
|
|||
|
<h1 class="module">JSON</h1>
|
|||
|
|
|||
|
<div id="description" class="description">
|
|||
|
|
|||
|
<h1>JavaScript Object Notation (<a href="JSON.html">JSON</a>)</h1>
|
|||
|
|
|||
|
<p><a href="JSON.html">JSON</a> is a lightweight data-interchange format. It
|
|||
|
is easy for us humans to read and write. Plus, equally simple for machines
|
|||
|
to generate or parse. <a href="JSON.html">JSON</a> is completely language
|
|||
|
agnostic, making it the ideal interchange format.</p>
|
|||
|
|
|||
|
<p>Built on two universally available structures:</p>
|
|||
|
|
|||
|
<pre>1. A collection of name/value pairs. Often referred to as an _object_, hash table, record, struct, keyed list, or associative array.
|
|||
|
2. An ordered list of values. More commonly called an _array_, vector, sequence or list.</pre>
|
|||
|
|
|||
|
<p>To read more about <a href="JSON.html">JSON</a> visit: <a
|
|||
|
href="http://json.org">json.org</a></p>
|
|||
|
|
|||
|
<h2>Parsing <a href="JSON.html">JSON</a></h2>
|
|||
|
|
|||
|
<p>To parse a <a href="JSON.html">JSON</a> string received by another
|
|||
|
application or generated within your existing application:</p>
|
|||
|
|
|||
|
<pre>require 'json'
|
|||
|
|
|||
|
my_hash = JSON.parse('{"hello": "goodbye"}')
|
|||
|
puts my_hash["hello"] => "goodbye"</pre>
|
|||
|
|
|||
|
<p>Notice the extra quotes <tt>''</tt> around the hash notation. Ruby expects
|
|||
|
the argument to be a string and can’t convert objects like a hash or array.</p>
|
|||
|
|
|||
|
<p>Ruby converts your string into a hash</p>
|
|||
|
|
|||
|
<h2>Generating <a href="JSON.html">JSON</a></h2>
|
|||
|
|
|||
|
<p>Creating a <a href="JSON.html">JSON</a> string for communication or
|
|||
|
serialization is just as simple.</p>
|
|||
|
|
|||
|
<pre>require 'json'
|
|||
|
|
|||
|
my_hash = {:hello => "goodbye"}
|
|||
|
puts JSON.generate(my_hash) => "{\"hello\":\"goodbye\"}"</pre>
|
|||
|
|
|||
|
<p>Or an alternative way:</p>
|
|||
|
|
|||
|
<pre>require 'json'
|
|||
|
puts {:hello => "goodbye"}.to_json => "{\"hello\":\"goodbye\"}"</pre>
|
|||
|
|
|||
|
<p><tt>JSON.generate</tt> only allows objects or arrays to be converted to <a
|
|||
|
href="JSON.html">JSON</a> syntax. <tt>to_json</tt>, however, accepts many
|
|||
|
Ruby classes even though it acts only as a method for serialization:</p>
|
|||
|
|
|||
|
<pre>require 'json'
|
|||
|
|
|||
|
1.to_json => "1"</pre>
|
|||
|
|
|||
|
</div><!-- description -->
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<div id="5Buntitled-5D" class="documentation-section">
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<!-- Constants -->
|
|||
|
<div id="constants-list" class="section">
|
|||
|
<h3 class="section-header">Constants</h3>
|
|||
|
<dl>
|
|||
|
|
|||
|
<dt><a name="Infinity">Infinity</a></dt>
|
|||
|
|
|||
|
<dd class="description"></dd>
|
|||
|
|
|||
|
|
|||
|
<dt><a name="JSON_LOADED">JSON_LOADED</a></dt>
|
|||
|
|
|||
|
<dd class="description"></dd>
|
|||
|
|
|||
|
|
|||
|
<dt><a name="MinusInfinity">MinusInfinity</a></dt>
|
|||
|
|
|||
|
<dd class="description"></dd>
|
|||
|
|
|||
|
|
|||
|
<dt><a name="NaN">NaN</a></dt>
|
|||
|
|
|||
|
<dd class="description"></dd>
|
|||
|
|
|||
|
|
|||
|
<dt><a name="UnparserError">UnparserError</a></dt>
|
|||
|
|
|||
|
<dd class="description"><p>For backwards compatibility</p></dd>
|
|||
|
|
|||
|
|
|||
|
<dt><a name="VERSION">VERSION</a></dt>
|
|||
|
|
|||
|
<dd class="description"><p><a href="JSON.html">JSON</a> version</p></dd>
|
|||
|
|
|||
|
|
|||
|
</dl>
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<!-- Attributes -->
|
|||
|
<div id="attribute-method-details" class="method-section section">
|
|||
|
<h3 class="section-header">Attributes</h3>
|
|||
|
|
|||
|
|
|||
|
<div id="create_id-attribute-method" class="method-detail">
|
|||
|
<a name="create_id"></a>
|
|||
|
|
|||
|
<a name="create_id="></a>
|
|||
|
|
|||
|
<div class="method-heading attribute-method-heading">
|
|||
|
<span class="method-name">create_id</span><span
|
|||
|
class="attribute-access-type">[RW]</span>
|
|||
|
</div>
|
|||
|
|
|||
|
<div class="method-description">
|
|||
|
|
|||
|
<p>This is create identifier, which is used to decide if the
|
|||
|
<em>json_create</em> hook of a class should be called. It defaults to
|
|||
|
‘json_class’.</p>
|
|||
|
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
|
|||
|
<div id="dump_default_options-attribute-method" class="method-detail">
|
|||
|
<a name="dump_default_options"></a>
|
|||
|
|
|||
|
<a name="dump_default_options="></a>
|
|||
|
|
|||
|
<div class="method-heading attribute-method-heading">
|
|||
|
<span class="method-name">dump_default_options</span><span
|
|||
|
class="attribute-access-type">[RW]</span>
|
|||
|
</div>
|
|||
|
|
|||
|
<div class="method-description">
|
|||
|
|
|||
|
<p>The global default options for the <a
|
|||
|
href="JSON.html#method-i-dump">JSON.dump</a> method:</p>
|
|||
|
|
|||
|
<pre>:max_nesting: false
|
|||
|
:allow_nan: true
|
|||
|
:quirks_mode: true</pre>
|
|||
|
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
|
|||
|
<div id="generator-attribute-method" class="method-detail">
|
|||
|
<a name="generator"></a>
|
|||
|
|
|||
|
<div class="method-heading attribute-method-heading">
|
|||
|
<span class="method-name">generator</span><span
|
|||
|
class="attribute-access-type">[R]</span>
|
|||
|
</div>
|
|||
|
|
|||
|
<div class="method-description">
|
|||
|
|
|||
|
<p>Returns the <a href="JSON.html">JSON</a> generator module that is used by
|
|||
|
<a href="JSON.html">JSON</a>. This is either JSON::Ext::Generator or <a
|
|||
|
href="JSON/Pure/Generator.html">JSON::Pure::Generator</a>.</p>
|
|||
|
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
|
|||
|
<div id="load_default_options-attribute-method" class="method-detail">
|
|||
|
<a name="load_default_options"></a>
|
|||
|
|
|||
|
<a name="load_default_options="></a>
|
|||
|
|
|||
|
<div class="method-heading attribute-method-heading">
|
|||
|
<span class="method-name">load_default_options</span><span
|
|||
|
class="attribute-access-type">[RW]</span>
|
|||
|
</div>
|
|||
|
|
|||
|
<div class="method-description">
|
|||
|
|
|||
|
<p>The global default options for the <a
|
|||
|
href="JSON.html#method-i-load">JSON.load</a> method:</p>
|
|||
|
|
|||
|
<pre>:max_nesting: false
|
|||
|
:allow_nan: true
|
|||
|
:quirks_mode: true</pre>
|
|||
|
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
|
|||
|
<div id="parser-attribute-method" class="method-detail">
|
|||
|
<a name="parser"></a>
|
|||
|
|
|||
|
<div class="method-heading attribute-method-heading">
|
|||
|
<span class="method-name">parser</span><span
|
|||
|
class="attribute-access-type">[R]</span>
|
|||
|
</div>
|
|||
|
|
|||
|
<div class="method-description">
|
|||
|
|
|||
|
<p>Returns the <a href="JSON.html">JSON</a> parser class that is used by <a
|
|||
|
href="JSON.html">JSON</a>. This is either JSON::Ext::Parser or <a
|
|||
|
href="JSON/Pure/Parser.html">JSON::Pure::Parser</a>.</p>
|
|||
|
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
|
|||
|
<div id="state-attribute-method" class="method-detail">
|
|||
|
<a name="state"></a>
|
|||
|
|
|||
|
<a name="state="></a>
|
|||
|
|
|||
|
<div class="method-heading attribute-method-heading">
|
|||
|
<span class="method-name">state</span><span
|
|||
|
class="attribute-access-type">[RW]</span>
|
|||
|
</div>
|
|||
|
|
|||
|
<div class="method-description">
|
|||
|
|
|||
|
<p>Returns the <a href="JSON.html">JSON</a> generator state class that is used
|
|||
|
by <a href="JSON.html">JSON</a>. This is either JSON::Ext::Generator::State
|
|||
|
or <a
|
|||
|
href="JSON/Pure/Generator/State.html">JSON::Pure::Generator::State</a>.</p>
|
|||
|
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
|
|||
|
</div><!-- attribute-method-details -->
|
|||
|
|
|||
|
|
|||
|
<!-- Methods -->
|
|||
|
|
|||
|
<div id="public-class-method-details" class="method-section section">
|
|||
|
<h3 class="section-header">Public Class Methods</h3>
|
|||
|
|
|||
|
|
|||
|
<div id="5B-5D-method" class="method-detail ">
|
|||
|
<a name="method-c-5B-5D"></a>
|
|||
|
|
|||
|
|
|||
|
<div class="method-heading">
|
|||
|
<span class="method-name">[]</span><span
|
|||
|
class="method-args">(object, opts = {})</span>
|
|||
|
<span class="method-click-advice">click to toggle source</span>
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
<div class="method-description">
|
|||
|
|
|||
|
<p>If <em>object</em> is string-like, parse the string and return the parsed
|
|||
|
result as a Ruby data structure. Otherwise generate a <a
|
|||
|
href="JSON.html">JSON</a> text from the Ruby data structure object and
|
|||
|
return it.</p>
|
|||
|
|
|||
|
<p>The <em>opts</em> argument is passed through to generate/parse
|
|||
|
respectively. See generate and parse for their documentation.</p>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<div class="method-source-code" id="5B-5D-source">
|
|||
|
<pre>
|
|||
|
<span class="ruby-comment"># File lib/json/common.rb, line 12</span>
|
|||
|
<span class="ruby-keyword">def</span> <span class="ruby-operator">[]</span>(<span class="ruby-identifier">object</span>, <span class="ruby-identifier">opts</span> = {})
|
|||
|
<span class="ruby-keyword">if</span> <span class="ruby-identifier">object</span>.<span class="ruby-identifier">respond_to?</span> <span class="ruby-value">:to_str</span>
|
|||
|
<span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">object</span>.<span class="ruby-identifier">to_str</span>, <span class="ruby-identifier">opts</span>)
|
|||
|
<span class="ruby-keyword">else</span>
|
|||
|
<span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">object</span>, <span class="ruby-identifier">opts</span>)
|
|||
|
<span class="ruby-keyword">end</span>
|
|||
|
<span class="ruby-keyword">end</span></pre>
|
|||
|
</div><!-- 5B-5D-source -->
|
|||
|
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
</div><!-- 5B-5D-method -->
|
|||
|
|
|||
|
|
|||
|
<div id="const_defined_in-3F-method" class="method-detail ">
|
|||
|
<a name="method-c-const_defined_in-3F"></a>
|
|||
|
|
|||
|
|
|||
|
<div class="method-heading">
|
|||
|
<span class="method-name">const_defined_in?</span><span
|
|||
|
class="method-args">(modul, constant)</span>
|
|||
|
<span class="method-click-advice">click to toggle source</span>
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
<div class="method-description">
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<div class="method-source-code" id="const_defined_in-3F-source">
|
|||
|
<pre>
|
|||
|
<span class="ruby-comment"># File lib/json/common.rb, line 429</span>
|
|||
|
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">const_defined_in?</span>(<span class="ruby-identifier">modul</span>, <span class="ruby-identifier">constant</span>)
|
|||
|
<span class="ruby-identifier">modul</span>.<span class="ruby-identifier">const_defined?</span>(<span class="ruby-identifier">constant</span>)
|
|||
|
<span class="ruby-keyword">end</span></pre>
|
|||
|
</div><!-- const_defined_in-3F-source -->
|
|||
|
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
</div><!-- const_defined_in-3F-method -->
|
|||
|
|
|||
|
|
|||
|
<div id="iconv-method" class="method-detail ">
|
|||
|
<a name="method-c-iconv"></a>
|
|||
|
|
|||
|
|
|||
|
<div class="method-heading">
|
|||
|
<span class="method-name">iconv</span><span
|
|||
|
class="method-args">(to, from, string)</span>
|
|||
|
<span class="method-click-advice">click to toggle source</span>
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
<div class="method-description">
|
|||
|
|
|||
|
<p>Encodes string using Ruby’s <em>String.encode</em></p>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<div class="method-source-code" id="iconv-source">
|
|||
|
<pre>
|
|||
|
<span class="ruby-comment"># File lib/json/common.rb, line 417</span>
|
|||
|
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">iconv</span>(<span class="ruby-identifier">to</span>, <span class="ruby-identifier">from</span>, <span class="ruby-identifier">string</span>)
|
|||
|
<span class="ruby-identifier">string</span>.<span class="ruby-identifier">encode</span>(<span class="ruby-identifier">to</span>, <span class="ruby-identifier">from</span>)
|
|||
|
<span class="ruby-keyword">end</span></pre>
|
|||
|
</div><!-- iconv-source -->
|
|||
|
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
</div><!-- iconv-method -->
|
|||
|
|
|||
|
|
|||
|
<div id="restore-method" class="method-detail method-alias">
|
|||
|
<a name="method-c-restore"></a>
|
|||
|
|
|||
|
|
|||
|
<div class="method-heading">
|
|||
|
<span class="method-name">restore</span><span
|
|||
|
class="method-args">(source, proc = nil, options = {})</span>
|
|||
|
<span class="method-click-advice">click to toggle source</span>
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
<div class="method-description">
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<div class="aliases">
|
|||
|
Alias for: <a href="JSON.html#method-i-load">load</a>
|
|||
|
</div>
|
|||
|
|
|||
|
</div><!-- restore-method -->
|
|||
|
|
|||
|
|
|||
|
<div id="valid_utf8-3F-method" class="method-detail ">
|
|||
|
<a name="method-c-valid_utf8-3F"></a>
|
|||
|
|
|||
|
|
|||
|
<div class="method-heading">
|
|||
|
<span class="method-name">valid_utf8?</span><span
|
|||
|
class="method-args">(string)</span>
|
|||
|
<span class="method-click-advice">click to toggle source</span>
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
<div class="method-description">
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<div class="method-source-code" id="valid_utf8-3F-source">
|
|||
|
<pre>
|
|||
|
<span class="ruby-comment"># File lib/json/pure/generator.rb, line 74</span>
|
|||
|
<span class="ruby-keyword">def</span> <span class="ruby-identifier">valid_utf8?</span>(<span class="ruby-identifier">string</span>)
|
|||
|
<span class="ruby-identifier">encoding</span> = <span class="ruby-identifier">string</span>.<span class="ruby-identifier">encoding</span>
|
|||
|
(<span class="ruby-identifier">encoding</span> <span class="ruby-operator">==</span> <span class="ruby-constant">Encoding</span><span class="ruby-operator">::</span><span class="ruby-constant">UTF_8</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">encoding</span> <span class="ruby-operator">==</span> <span class="ruby-constant">Encoding</span><span class="ruby-operator">::</span><span class="ruby-constant">ASCII</span>) <span class="ruby-operator">&&</span>
|
|||
|
<span class="ruby-identifier">string</span>.<span class="ruby-identifier">valid_encoding?</span>
|
|||
|
<span class="ruby-keyword">end</span></pre>
|
|||
|
</div><!-- valid_utf8-3F-source -->
|
|||
|
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
</div><!-- valid_utf8-3F-method -->
|
|||
|
|
|||
|
|
|||
|
</div><!-- public-class-method-details -->
|
|||
|
|
|||
|
<div id="public-instance-method-details" class="method-section section">
|
|||
|
<h3 class="section-header">Public Instance Methods</h3>
|
|||
|
|
|||
|
|
|||
|
<div id="dump-method" class="method-detail ">
|
|||
|
<a name="method-i-dump"></a>
|
|||
|
|
|||
|
|
|||
|
<div class="method-heading">
|
|||
|
<span class="method-name">dump</span><span
|
|||
|
class="method-args">(obj, anIO = nil, limit = nil)</span>
|
|||
|
<span class="method-click-advice">click to toggle source</span>
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
<div class="method-description">
|
|||
|
|
|||
|
<p>Dumps <em>obj</em> as a <a href="JSON.html">JSON</a> string, i.e. calls
|
|||
|
generate on the object and returns the result.</p>
|
|||
|
|
|||
|
<p>If anIO (an IO-like object or an object that responds to the write method)
|
|||
|
was given, the resulting <a href="JSON.html">JSON</a> is written to it.</p>
|
|||
|
|
|||
|
<p>If the number of nested arrays or objects exceeds <em>limit</em>, an
|
|||
|
ArgumentError exception is raised. This argument is similar (but not
|
|||
|
exactly the same!) to the <em>limit</em> argument in Marshal.dump.</p>
|
|||
|
|
|||
|
<p>The default options for the generator can be changed via the <a
|
|||
|
href="JSON.html#attribute-c-dump_default_options">dump_default_options</a>
|
|||
|
method.</p>
|
|||
|
|
|||
|
<p>This method is part of the implementation of the load/dump interface of
|
|||
|
Marshal and YAML.</p>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<div class="method-source-code" id="dump-source">
|
|||
|
<pre>
|
|||
|
<span class="ruby-comment"># File lib/json/common.rb, line 384</span>
|
|||
|
<span class="ruby-keyword">def</span> <span class="ruby-identifier">dump</span>(<span class="ruby-identifier">obj</span>, <span class="ruby-identifier">anIO</span> = <span class="ruby-keyword">nil</span>, <span class="ruby-identifier">limit</span> = <span class="ruby-keyword">nil</span>)
|
|||
|
<span class="ruby-keyword">if</span> <span class="ruby-identifier">anIO</span> <span class="ruby-keyword">and</span> <span class="ruby-identifier">limit</span>.<span class="ruby-identifier">nil?</span>
|
|||
|
<span class="ruby-identifier">anIO</span> = <span class="ruby-identifier">anIO</span>.<span class="ruby-identifier">to_io</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">anIO</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-value">:to_io</span>)
|
|||
|
<span class="ruby-keyword">unless</span> <span class="ruby-identifier">anIO</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-value">:write</span>)
|
|||
|
<span class="ruby-identifier">limit</span> = <span class="ruby-identifier">anIO</span>
|
|||
|
<span class="ruby-identifier">anIO</span> = <span class="ruby-keyword">nil</span>
|
|||
|
<span class="ruby-keyword">end</span>
|
|||
|
<span class="ruby-keyword">end</span>
|
|||
|
<span class="ruby-identifier">opts</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">dump_default_options</span>
|
|||
|
<span class="ruby-identifier">limit</span> <span class="ruby-keyword">and</span> <span class="ruby-identifier">opts</span>.<span class="ruby-identifier">update</span>(<span class="ruby-value">:max_nesting</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">limit</span>)
|
|||
|
<span class="ruby-identifier">result</span> = <span class="ruby-identifier">generate</span>(<span class="ruby-identifier">obj</span>, <span class="ruby-identifier">opts</span>)
|
|||
|
<span class="ruby-keyword">if</span> <span class="ruby-identifier">anIO</span>
|
|||
|
<span class="ruby-identifier">anIO</span>.<span class="ruby-identifier">write</span> <span class="ruby-identifier">result</span>
|
|||
|
<span class="ruby-identifier">anIO</span>
|
|||
|
<span class="ruby-keyword">else</span>
|
|||
|
<span class="ruby-identifier">result</span>
|
|||
|
<span class="ruby-keyword">end</span>
|
|||
|
<span class="ruby-keyword">rescue</span> <span class="ruby-constant">JSON</span><span class="ruby-operator">::</span><span class="ruby-constant">NestingError</span>
|
|||
|
<span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">"exceed depth limit"</span>
|
|||
|
<span class="ruby-keyword">end</span></pre>
|
|||
|
</div><!-- dump-source -->
|
|||
|
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
</div><!-- dump-method -->
|
|||
|
|
|||
|
|
|||
|
<div id="fast_generate-method" class="method-detail ">
|
|||
|
<a name="method-i-fast_generate"></a>
|
|||
|
|
|||
|
|
|||
|
<div class="method-heading">
|
|||
|
<span class="method-name">fast_generate</span><span
|
|||
|
class="method-args">(obj, opts = nil)</span>
|
|||
|
<span class="method-click-advice">click to toggle source</span>
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
<div class="method-description">
|
|||
|
|
|||
|
<p>Generate a <a href="JSON.html">JSON</a> document from the Ruby data
|
|||
|
structure <em>obj</em> and return it. This method disables the checks for
|
|||
|
circles in Ruby objects.</p>
|
|||
|
|
|||
|
<p><b>WARNING</b>: Be careful not to pass any Ruby data structures with
|
|||
|
circles as <em>obj</em> argument because this will cause <a
|
|||
|
href="JSON.html">JSON</a> to go into an infinite loop.</p>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<div class="method-source-code" id="fast_generate-source">
|
|||
|
<pre>
|
|||
|
<span class="ruby-comment"># File lib/json/common.rb, line 238</span>
|
|||
|
<span class="ruby-keyword">def</span> <span class="ruby-identifier">fast_generate</span>(<span class="ruby-identifier">obj</span>, <span class="ruby-identifier">opts</span> = <span class="ruby-keyword">nil</span>)
|
|||
|
<span class="ruby-keyword">if</span> <span class="ruby-constant">State</span> <span class="ruby-operator">===</span> <span class="ruby-identifier">opts</span>
|
|||
|
<span class="ruby-identifier">state</span>, <span class="ruby-identifier">opts</span> = <span class="ruby-identifier">opts</span>, <span class="ruby-keyword">nil</span>
|
|||
|
<span class="ruby-keyword">else</span>
|
|||
|
<span class="ruby-identifier">state</span> = <span class="ruby-constant">FAST_STATE_PROTOTYPE</span>.<span class="ruby-identifier">dup</span>
|
|||
|
<span class="ruby-keyword">end</span>
|
|||
|
<span class="ruby-keyword">if</span> <span class="ruby-identifier">opts</span>
|
|||
|
<span class="ruby-keyword">if</span> <span class="ruby-identifier">opts</span>.<span class="ruby-identifier">respond_to?</span> <span class="ruby-value">:to_hash</span>
|
|||
|
<span class="ruby-identifier">opts</span> = <span class="ruby-identifier">opts</span>.<span class="ruby-identifier">to_hash</span>
|
|||
|
<span class="ruby-keyword">elsif</span> <span class="ruby-identifier">opts</span>.<span class="ruby-identifier">respond_to?</span> <span class="ruby-value">:to_h</span>
|
|||
|
<span class="ruby-identifier">opts</span> = <span class="ruby-identifier">opts</span>.<span class="ruby-identifier">to_h</span>
|
|||
|
<span class="ruby-keyword">else</span>
|
|||
|
<span class="ruby-identifier">raise</span> <span class="ruby-constant">TypeError</span>, <span class="ruby-node">"can't convert #{opts.class} into Hash"</span>
|
|||
|
<span class="ruby-keyword">end</span>
|
|||
|
<span class="ruby-identifier">state</span>.<span class="ruby-identifier">configure</span>(<span class="ruby-identifier">opts</span>)
|
|||
|
<span class="ruby-keyword">end</span>
|
|||
|
<span class="ruby-identifier">state</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">obj</span>)
|
|||
|
<span class="ruby-keyword">end</span></pre>
|
|||
|
</div><!-- fast_generate-source -->
|
|||
|
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
</div><!-- fast_generate-method -->
|
|||
|
|
|||
|
|
|||
|
<div id="generate-method" class="method-detail ">
|
|||
|
<a name="method-i-generate"></a>
|
|||
|
|
|||
|
|
|||
|
<div class="method-heading">
|
|||
|
<span class="method-name">generate</span><span
|
|||
|
class="method-args">(obj, opts = nil)</span>
|
|||
|
<span class="method-click-advice">click to toggle source</span>
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
<div class="method-description">
|
|||
|
|
|||
|
<p>Generate a <a href="JSON.html">JSON</a> document from the Ruby data
|
|||
|
structure <em>obj</em> and return it. <em>state</em> is * a JSON::State
|
|||
|
object,</p>
|
|||
|
<ul><li>
|
|||
|
<p>or a Hash like object (responding to to_hash),</p>
|
|||
|
</li><li>
|
|||
|
<p>an object convertible into a hash by a to_h method,</p>
|
|||
|
</li></ul>
|
|||
|
|
|||
|
<p>that is used as or to configure a State object.</p>
|
|||
|
|
|||
|
<p>It defaults to a state object, that creates the shortest possible <a
|
|||
|
href="JSON.html">JSON</a> text in one line, checks for circular data
|
|||
|
structures and doesn’t allow <a href="JSON.html#NaN">NaN</a>, <a
|
|||
|
href="JSON.html#Infinity">Infinity</a>, and -<a
|
|||
|
href="JSON.html#Infinity">Infinity</a>.</p>
|
|||
|
|
|||
|
<p>A <em>state</em> hash can have the following keys:</p>
|
|||
|
<ul><li>
|
|||
|
<p><b>indent</b>: a string used to indent levels (default: "),</p>
|
|||
|
</li><li>
|
|||
|
<p><b>space</b>: a string that is put after, a : or , delimiter (default: "),</p>
|
|||
|
</li><li>
|
|||
|
<p><b>space_before</b>: a string that is put before a : pair delimiter
|
|||
|
(default: "),</p>
|
|||
|
</li><li>
|
|||
|
<p><b>object_nl</b>: a string that is put at the end of a <a
|
|||
|
href="JSON.html">JSON</a> object (default: "),</p>
|
|||
|
</li><li>
|
|||
|
<p><b>array_nl</b>: a string that is put at the end of a <a
|
|||
|
href="JSON.html">JSON</a> array (default: "),</p>
|
|||
|
</li><li>
|
|||
|
<p><b>allow_nan</b>: true if <a href="JSON.html#NaN">NaN</a>, <a
|
|||
|
href="JSON.html#Infinity">Infinity</a>, and -<a
|
|||
|
href="JSON.html#Infinity">Infinity</a> should be generated, otherwise an
|
|||
|
exception is thrown if these values are encountered. This options defaults
|
|||
|
to false.</p>
|
|||
|
</li><li>
|
|||
|
<p><b>max_nesting</b>: The maximum depth of nesting allowed in the data
|
|||
|
structures from which <a href="JSON.html">JSON</a> is to be generated.
|
|||
|
Disable depth checking with :max_nesting => false, it defaults to 100.</p>
|
|||
|
</li></ul>
|
|||
|
|
|||
|
<p>See also the <a href="JSON.html#method-i-fast_generate">fast_generate</a>
|
|||
|
for the fastest creation method with the least amount of sanity checks, and
|
|||
|
the <a href="JSON.html#method-i-pretty_generate">pretty_generate</a> method
|
|||
|
for some defaults for pretty output.</p>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<div class="method-source-code" id="generate-source">
|
|||
|
<pre>
|
|||
|
<span class="ruby-comment"># File lib/json/common.rb, line 207</span>
|
|||
|
<span class="ruby-keyword">def</span> <span class="ruby-identifier">generate</span>(<span class="ruby-identifier">obj</span>, <span class="ruby-identifier">opts</span> = <span class="ruby-keyword">nil</span>)
|
|||
|
<span class="ruby-keyword">if</span> <span class="ruby-constant">State</span> <span class="ruby-operator">===</span> <span class="ruby-identifier">opts</span>
|
|||
|
<span class="ruby-identifier">state</span>, <span class="ruby-identifier">opts</span> = <span class="ruby-identifier">opts</span>, <span class="ruby-keyword">nil</span>
|
|||
|
<span class="ruby-keyword">else</span>
|
|||
|
<span class="ruby-identifier">state</span> = <span class="ruby-constant">SAFE_STATE_PROTOTYPE</span>.<span class="ruby-identifier">dup</span>
|
|||
|
<span class="ruby-keyword">end</span>
|
|||
|
<span class="ruby-keyword">if</span> <span class="ruby-identifier">opts</span>
|
|||
|
<span class="ruby-keyword">if</span> <span class="ruby-identifier">opts</span>.<span class="ruby-identifier">respond_to?</span> <span class="ruby-value">:to_hash</span>
|
|||
|
<span class="ruby-identifier">opts</span> = <span class="ruby-identifier">opts</span>.<span class="ruby-identifier">to_hash</span>
|
|||
|
<span class="ruby-keyword">elsif</span> <span class="ruby-identifier">opts</span>.<span class="ruby-identifier">respond_to?</span> <span class="ruby-value">:to_h</span>
|
|||
|
<span class="ruby-identifier">opts</span> = <span class="ruby-identifier">opts</span>.<span class="ruby-identifier">to_h</span>
|
|||
|
<span class="ruby-keyword">else</span>
|
|||
|
<span class="ruby-identifier">raise</span> <span class="ruby-constant">TypeError</span>, <span class="ruby-node">"can't convert #{opts.class} into Hash"</span>
|
|||
|
<span class="ruby-keyword">end</span>
|
|||
|
<span class="ruby-identifier">state</span> = <span class="ruby-identifier">state</span>.<span class="ruby-identifier">configure</span>(<span class="ruby-identifier">opts</span>)
|
|||
|
<span class="ruby-keyword">end</span>
|
|||
|
<span class="ruby-identifier">state</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">obj</span>)
|
|||
|
<span class="ruby-keyword">end</span></pre>
|
|||
|
</div><!-- generate-source -->
|
|||
|
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
</div><!-- generate-method -->
|
|||
|
|
|||
|
|
|||
|
<div id="load-method" class="method-detail ">
|
|||
|
<a name="method-i-load"></a>
|
|||
|
|
|||
|
|
|||
|
<div class="method-heading">
|
|||
|
<span class="method-name">load</span><span
|
|||
|
class="method-args">(source, proc = nil, options = {})</span>
|
|||
|
<span class="method-click-advice">click to toggle source</span>
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
<div class="method-description">
|
|||
|
|
|||
|
<p>Load a ruby data structure from a <a href="JSON.html">JSON</a>
|
|||
|
<em>source</em> and return it. A source can either be a string-like object,
|
|||
|
an IO-like object, or an object responding to the read method. If
|
|||
|
<em>proc</em> was given, it will be called with any nested Ruby object as
|
|||
|
an argument recursively in depth first order. To modify the default options
|
|||
|
pass in the optional <em>options</em> argument as well.</p>
|
|||
|
|
|||
|
<p>BEWARE: This method is meant to serialise data from trusted user input,
|
|||
|
like from your own database server or clients under your control, it could
|
|||
|
be dangerous to allow untrusted users to pass <a href="JSON.html">JSON</a>
|
|||
|
sources into it. The default options for the parser can be changed via the
|
|||
|
<a
|
|||
|
href="JSON.html#attribute-c-load_default_options">load_default_options</a>
|
|||
|
method.</p>
|
|||
|
|
|||
|
<p>This method is part of the implementation of the load/dump interface of
|
|||
|
Marshal and YAML.</p>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<div class="method-source-code" id="load-source">
|
|||
|
<pre>
|
|||
|
<span class="ruby-comment"># File lib/json/common.rb, line 322</span>
|
|||
|
<span class="ruby-keyword">def</span> <span class="ruby-identifier">load</span>(<span class="ruby-identifier">source</span>, <span class="ruby-identifier">proc</span> = <span class="ruby-keyword">nil</span>, <span class="ruby-identifier">options</span> = {})
|
|||
|
<span class="ruby-identifier">opts</span> = <span class="ruby-identifier">load_default_options</span>.<span class="ruby-identifier">merge</span> <span class="ruby-identifier">options</span>
|
|||
|
<span class="ruby-keyword">if</span> <span class="ruby-identifier">source</span>.<span class="ruby-identifier">respond_to?</span> <span class="ruby-value">:to_str</span>
|
|||
|
<span class="ruby-identifier">source</span> = <span class="ruby-identifier">source</span>.<span class="ruby-identifier">to_str</span>
|
|||
|
<span class="ruby-keyword">elsif</span> <span class="ruby-identifier">source</span>.<span class="ruby-identifier">respond_to?</span> <span class="ruby-value">:to_io</span>
|
|||
|
<span class="ruby-identifier">source</span> = <span class="ruby-identifier">source</span>.<span class="ruby-identifier">to_io</span>.<span class="ruby-identifier">read</span>
|
|||
|
<span class="ruby-keyword">elsif</span> <span class="ruby-identifier">source</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-value">:read</span>)
|
|||
|
<span class="ruby-identifier">source</span> = <span class="ruby-identifier">source</span>.<span class="ruby-identifier">read</span>
|
|||
|
<span class="ruby-keyword">end</span>
|
|||
|
<span class="ruby-keyword">if</span> <span class="ruby-identifier">opts</span>[<span class="ruby-value">:quirks_mode</span>] <span class="ruby-operator">&&</span> (<span class="ruby-identifier">source</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">source</span>.<span class="ruby-identifier">empty?</span>)
|
|||
|
<span class="ruby-identifier">source</span> = <span class="ruby-string">'null'</span>
|
|||
|
<span class="ruby-keyword">end</span>
|
|||
|
<span class="ruby-identifier">result</span> = <span class="ruby-identifier">parse</span>(<span class="ruby-identifier">source</span>, <span class="ruby-identifier">opts</span>)
|
|||
|
<span class="ruby-identifier">recurse_proc</span>(<span class="ruby-identifier">result</span>, &<span class="ruby-identifier">proc</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">proc</span>
|
|||
|
<span class="ruby-identifier">result</span>
|
|||
|
<span class="ruby-keyword">end</span></pre>
|
|||
|
</div><!-- load-source -->
|
|||
|
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
<div class="aliases">
|
|||
|
Also aliased as: <a href="JSON.html#method-i-restore">restore</a>
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
</div><!-- load-method -->
|
|||
|
|
|||
|
|
|||
|
<div id="parse-method" class="method-detail ">
|
|||
|
<a name="method-i-parse"></a>
|
|||
|
|
|||
|
|
|||
|
<div class="method-heading">
|
|||
|
<span class="method-name">parse</span><span
|
|||
|
class="method-args">(source, opts = {})</span>
|
|||
|
<span class="method-click-advice">click to toggle source</span>
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
<div class="method-description">
|
|||
|
|
|||
|
<p>Parse the <a href="JSON.html">JSON</a> document <em>source</em> into a Ruby
|
|||
|
data structure and return it.</p>
|
|||
|
|
|||
|
<p><em>opts</em> can have the following keys:</p>
|
|||
|
<ul><li>
|
|||
|
<p><b>max_nesting</b>: The maximum depth of nesting allowed in the parsed data
|
|||
|
structures. Disable depth checking with :max_nesting => false. It
|
|||
|
defaults to 100.</p>
|
|||
|
</li><li>
|
|||
|
<p><b>allow_nan</b>: If set to true, allow <a href="JSON.html#NaN">NaN</a>, <a
|
|||
|
href="JSON.html#Infinity">Infinity</a> and -<a
|
|||
|
href="JSON.html#Infinity">Infinity</a> in defiance of RFC 4627 to be parsed
|
|||
|
by the Parser. This option defaults to false.</p>
|
|||
|
</li><li>
|
|||
|
<p><b>symbolize_names</b>: If set to true, returns symbols for the names
|
|||
|
(keys) in a <a href="JSON.html">JSON</a> object. Otherwise strings are
|
|||
|
returned. Strings are the default.</p>
|
|||
|
</li><li>
|
|||
|
<p><b>create_additions</b>: If set to false, the Parser doesn't create
|
|||
|
additions even if a matching class and <a
|
|||
|
href="JSON.html#attribute-c-create_id">create_id</a> was found. This option
|
|||
|
defaults to true.</p>
|
|||
|
</li><li>
|
|||
|
<p><b>object_class</b>: Defaults to Hash</p>
|
|||
|
</li><li>
|
|||
|
<p><b>array_class</b>: Defaults to Array</p>
|
|||
|
</li></ul>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<div class="method-source-code" id="parse-source">
|
|||
|
<pre>
|
|||
|
<span class="ruby-comment"># File lib/json/common.rb, line 154</span>
|
|||
|
<span class="ruby-keyword">def</span> <span class="ruby-identifier">parse</span>(<span class="ruby-identifier">source</span>, <span class="ruby-identifier">opts</span> = {})
|
|||
|
<span class="ruby-constant">Parser</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">source</span>, <span class="ruby-identifier">opts</span>).<span class="ruby-identifier">parse</span>
|
|||
|
<span class="ruby-keyword">end</span></pre>
|
|||
|
</div><!-- parse-source -->
|
|||
|
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
</div><!-- parse-method -->
|
|||
|
|
|||
|
|
|||
|
<div id="parse-21-method" class="method-detail ">
|
|||
|
<a name="method-i-parse-21"></a>
|
|||
|
|
|||
|
|
|||
|
<div class="method-heading">
|
|||
|
<span class="method-name">parse!</span><span
|
|||
|
class="method-args">(source, opts = {})</span>
|
|||
|
<span class="method-click-advice">click to toggle source</span>
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
<div class="method-description">
|
|||
|
|
|||
|
<p>Parse the <a href="JSON.html">JSON</a> document <em>source</em> into a Ruby
|
|||
|
data structure and return it. The bang version of the parse method defaults
|
|||
|
to the more dangerous values for the <em>opts</em> hash, so be sure only to
|
|||
|
parse trusted <em>source</em> documents.</p>
|
|||
|
|
|||
|
<p><em>opts</em> can have the following keys:</p>
|
|||
|
<ul><li>
|
|||
|
<p><b>max_nesting</b>: The maximum depth of nesting allowed in the parsed data
|
|||
|
structures. Enable depth checking with :max_nesting => anInteger. The
|
|||
|
parse! methods defaults to not doing max depth checking: This can be
|
|||
|
dangerous if someone wants to fill up your stack.</p>
|
|||
|
</li><li>
|
|||
|
<p><b>allow_nan</b>: If set to true, allow <a href="JSON.html#NaN">NaN</a>, <a
|
|||
|
href="JSON.html#Infinity">Infinity</a>, and -<a
|
|||
|
href="JSON.html#Infinity">Infinity</a> in defiance of RFC 4627 to be parsed
|
|||
|
by the Parser. This option defaults to true.</p>
|
|||
|
</li><li>
|
|||
|
<p><b>create_additions</b>: If set to false, the Parser doesn't create
|
|||
|
additions even if a matching class and <a
|
|||
|
href="JSON.html#attribute-c-create_id">create_id</a> was found. This option
|
|||
|
defaults to true.</p>
|
|||
|
</li></ul>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<div class="method-source-code" id="parse-21-source">
|
|||
|
<pre>
|
|||
|
<span class="ruby-comment"># File lib/json/common.rb, line 173</span>
|
|||
|
<span class="ruby-keyword">def</span> <span class="ruby-identifier">parse!</span>(<span class="ruby-identifier">source</span>, <span class="ruby-identifier">opts</span> = {})
|
|||
|
<span class="ruby-identifier">opts</span> = {
|
|||
|
<span class="ruby-value">:max_nesting</span> =<span class="ruby-operator">></span> <span class="ruby-keyword">false</span>,
|
|||
|
<span class="ruby-value">:allow_nan</span> =<span class="ruby-operator">></span> <span class="ruby-keyword">true</span>
|
|||
|
}.<span class="ruby-identifier">update</span>(<span class="ruby-identifier">opts</span>)
|
|||
|
<span class="ruby-constant">Parser</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">source</span>, <span class="ruby-identifier">opts</span>).<span class="ruby-identifier">parse</span>
|
|||
|
<span class="ruby-keyword">end</span></pre>
|
|||
|
</div><!-- parse-21-source -->
|
|||
|
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
</div><!-- parse-21-method -->
|
|||
|
|
|||
|
|
|||
|
<div id="pretty_generate-method" class="method-detail ">
|
|||
|
<a name="method-i-pretty_generate"></a>
|
|||
|
|
|||
|
|
|||
|
<div class="method-heading">
|
|||
|
<span class="method-name">pretty_generate</span><span
|
|||
|
class="method-args">(obj, opts = nil)</span>
|
|||
|
<span class="method-click-advice">click to toggle source</span>
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
<div class="method-description">
|
|||
|
|
|||
|
<p>Generate a <a href="JSON.html">JSON</a> document from the Ruby data
|
|||
|
structure <em>obj</em> and return it. The returned document is a prettier
|
|||
|
form of the document returned by unparse.</p>
|
|||
|
|
|||
|
<p>The <em>opts</em> argument can be used to configure the generator. See the
|
|||
|
generate method for a more detailed explanation.</p>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<div class="method-source-code" id="pretty_generate-source">
|
|||
|
<pre>
|
|||
|
<span class="ruby-comment"># File lib/json/common.rb, line 269</span>
|
|||
|
<span class="ruby-keyword">def</span> <span class="ruby-identifier">pretty_generate</span>(<span class="ruby-identifier">obj</span>, <span class="ruby-identifier">opts</span> = <span class="ruby-keyword">nil</span>)
|
|||
|
<span class="ruby-keyword">if</span> <span class="ruby-constant">State</span> <span class="ruby-operator">===</span> <span class="ruby-identifier">opts</span>
|
|||
|
<span class="ruby-identifier">state</span>, <span class="ruby-identifier">opts</span> = <span class="ruby-identifier">opts</span>, <span class="ruby-keyword">nil</span>
|
|||
|
<span class="ruby-keyword">else</span>
|
|||
|
<span class="ruby-identifier">state</span> = <span class="ruby-constant">PRETTY_STATE_PROTOTYPE</span>.<span class="ruby-identifier">dup</span>
|
|||
|
<span class="ruby-keyword">end</span>
|
|||
|
<span class="ruby-keyword">if</span> <span class="ruby-identifier">opts</span>
|
|||
|
<span class="ruby-keyword">if</span> <span class="ruby-identifier">opts</span>.<span class="ruby-identifier">respond_to?</span> <span class="ruby-value">:to_hash</span>
|
|||
|
<span class="ruby-identifier">opts</span> = <span class="ruby-identifier">opts</span>.<span class="ruby-identifier">to_hash</span>
|
|||
|
<span class="ruby-keyword">elsif</span> <span class="ruby-identifier">opts</span>.<span class="ruby-identifier">respond_to?</span> <span class="ruby-value">:to_h</span>
|
|||
|
<span class="ruby-identifier">opts</span> = <span class="ruby-identifier">opts</span>.<span class="ruby-identifier">to_h</span>
|
|||
|
<span class="ruby-keyword">else</span>
|
|||
|
<span class="ruby-identifier">raise</span> <span class="ruby-constant">TypeError</span>, <span class="ruby-node">"can't convert #{opts.class} into Hash"</span>
|
|||
|
<span class="ruby-keyword">end</span>
|
|||
|
<span class="ruby-identifier">state</span>.<span class="ruby-identifier">configure</span>(<span class="ruby-identifier">opts</span>)
|
|||
|
<span class="ruby-keyword">end</span>
|
|||
|
<span class="ruby-identifier">state</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">obj</span>)
|
|||
|
<span class="ruby-keyword">end</span></pre>
|
|||
|
</div><!-- pretty_generate-source -->
|
|||
|
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
</div><!-- pretty_generate-method -->
|
|||
|
|
|||
|
|
|||
|
<div id="recurse_proc-method" class="method-detail ">
|
|||
|
<a name="method-i-recurse_proc"></a>
|
|||
|
|
|||
|
|
|||
|
<div class="method-heading">
|
|||
|
<span class="method-name">recurse_proc</span><span
|
|||
|
class="method-args">(result, &proc)</span>
|
|||
|
<span class="method-click-advice">click to toggle source</span>
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
<div class="method-description">
|
|||
|
|
|||
|
<p>Recursively calls passed <em>Proc</em> if the parsed data structure is an
|
|||
|
<em>Array</em> or <em>Hash</em></p>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<div class="method-source-code" id="recurse_proc-source">
|
|||
|
<pre>
|
|||
|
<span class="ruby-comment"># File lib/json/common.rb, line 340</span>
|
|||
|
<span class="ruby-keyword">def</span> <span class="ruby-identifier">recurse_proc</span>(<span class="ruby-identifier">result</span>, &<span class="ruby-identifier">proc</span>)
|
|||
|
<span class="ruby-keyword">case</span> <span class="ruby-identifier">result</span>
|
|||
|
<span class="ruby-keyword">when</span> <span class="ruby-constant">Array</span>
|
|||
|
<span class="ruby-identifier">result</span>.<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">x</span><span class="ruby-operator">|</span> <span class="ruby-identifier">recurse_proc</span> <span class="ruby-identifier">x</span>, &<span class="ruby-identifier">proc</span> }
|
|||
|
<span class="ruby-identifier">proc</span>.<span class="ruby-identifier">call</span> <span class="ruby-identifier">result</span>
|
|||
|
<span class="ruby-keyword">when</span> <span class="ruby-constant">Hash</span>
|
|||
|
<span class="ruby-identifier">result</span>.<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">x</span>, <span class="ruby-identifier">y</span><span class="ruby-operator">|</span> <span class="ruby-identifier">recurse_proc</span> <span class="ruby-identifier">x</span>, &<span class="ruby-identifier">proc</span>; <span class="ruby-identifier">recurse_proc</span> <span class="ruby-identifier">y</span>, &<span class="ruby-identifier">proc</span> }
|
|||
|
<span class="ruby-identifier">proc</span>.<span class="ruby-identifier">call</span> <span class="ruby-identifier">result</span>
|
|||
|
<span class="ruby-keyword">else</span>
|
|||
|
<span class="ruby-identifier">proc</span>.<span class="ruby-identifier">call</span> <span class="ruby-identifier">result</span>
|
|||
|
<span class="ruby-keyword">end</span>
|
|||
|
<span class="ruby-keyword">end</span></pre>
|
|||
|
</div><!-- recurse_proc-source -->
|
|||
|
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
</div><!-- recurse_proc-method -->
|
|||
|
|
|||
|
|
|||
|
</div><!-- public-instance-method-details -->
|
|||
|
|
|||
|
</div><!-- 5Buntitled-5D -->
|
|||
|
|
|||
|
|
|||
|
</div><!-- documentation -->
|
|||
|
|
|||
|
<div id="validator-badges">
|
|||
|
<p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
|
|||
|
<p><small>Generated with the <a href="http://deveiate.org/projects/Darkfish-Rdoc/">Darkfish
|
|||
|
Rdoc Generator</a> 2</small>.</p>
|
|||
|
</div>
|
|||
|
|
|||
|
</body>
|
|||
|
</html>
|
|||
|
|