Skip to content

Commit 3b9ecef

Browse files
authored
Merge pull request #347 from zverok/enhance-docs
Enhance generic JSON and #generate docs
2 parents 5ae4931 + 3845491 commit 3b9ecef

File tree

2 files changed

+61
-18
lines changed

2 files changed

+61
-18
lines changed

lib/json.rb

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@
99
# JSON is completely language agnostic, making it the ideal interchange format.
1010
#
1111
# Built on two universally available structures:
12-
# 1. A collection of name/value pairs. Often referred to as an _object_, hash table, record, struct, keyed list, or associative array.
13-
# 2. An ordered list of values. More commonly called an _array_, vector, sequence or list.
12+
#
13+
# 1. A collection of name/value pairs. Often referred to as an _object_, hash table,
14+
# record, struct, keyed list, or associative array.
15+
# 2. An ordered list of values. More commonly called an _array_, vector, sequence or
16+
# list.
1417
#
1518
# To read more about JSON visit: http://json.org
1619
#
@@ -22,7 +25,7 @@
2225
# require 'json'
2326
#
2427
# my_hash = JSON.parse('{"hello": "goodbye"}')
25-
# puts my_hash["hello"] => "goodbye"
28+
# puts my_hash["hello"] # => "goodbye"
2629
#
2730
# Notice the extra quotes <tt>''</tt> around the hash notation. Ruby expects
2831
# the argument to be a string and can't convert objects like a hash or array.
@@ -37,13 +40,50 @@
3740
# require 'json'
3841
#
3942
# my_hash = {:hello => "goodbye"}
40-
# puts JSON.generate(my_hash) => "{\"hello\":\"goodbye\"}"
43+
# puts JSON.generate(my_hash) # => "{\"hello\":\"goodbye\"}"
4144
#
4245
# Or an alternative way:
4346
#
4447
# require 'json'
45-
# puts {:hello => "goodbye"}.to_json => "{\"hello\":\"goodbye\"}"
48+
# puts({:hello => "goodbye"}.to_json) # => "{\"hello\":\"goodbye\"}"
49+
#
50+
# <tt>JSON.generate</tt> only allows objects or arrays to be converted
51+
# to JSON syntax. <tt>to_json</tt>, however, accepts many Ruby classes
52+
# even though it acts only as a method for serialization:
53+
#
54+
# require 'json'
4655
#
56+
# 1.to_json # => "1"
57+
#
58+
# The {#generate}[rdoc-ref:JSON#generate] method accepts a variety of options
59+
# to set the formatting of string output and defining what input is accepteable.
60+
# There are also shortcut methods pretty_generate (with a set of options to
61+
# generate human-readable multiline JSON) and fast_generate (with a set of
62+
# options to generate JSON faster at the price of disabling some checks).
63+
#
64+
# == Extended rendering and loading of Ruby objects
65+
#
66+
# JSON library provides optional _additions_ allowing to serialize and
67+
# deserialize Ruby classes without loosing their type.
68+
#
69+
# # without additions
70+
# require "json"
71+
# json = JSON.generate({range: 1..3, regex: /test/})
72+
# # => '{"range":"1..3","regex":"(?-mix:test)"}'
73+
# JSON.parse(json)
74+
# # => {"range"=>"1..3", "regex"=>"(?-mix:test)"}
75+
#
76+
# # with additions
77+
# require "json/add/range"
78+
# require "json/add/regexp"
79+
# json = JSON.generate({range: 1..3, regex: /test/})
80+
# # => '{"range":{"json_class":"Range","a":[1,3,false]},"regex":{"json_class":"Regexp","o":0,"s":"test"}}'
81+
# JSON.parse(json)
82+
# # => {"range"=>{"json_class"=>"Range", "a"=>[1, 3, false]}, "regex"=>{"json_class"=>"Regexp", "o"=>0, "s"=>"test"}}
83+
# JSON.load(json)
84+
# # => {"range"=>1..3, "regex"=>/test/}
85+
#
86+
# See JSON.load for details.
4787
module JSON
4888
require 'json/version'
4989

lib/json/common.rb

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -180,27 +180,30 @@ def parse!(source, opts = {})
180180
end
181181

182182
# Generate a JSON document from the Ruby data structure _obj_ and return
183-
# it. _state_ is * a JSON::State object,
184-
# * or a Hash like object (responding to to_hash),
185-
# * an object convertible into a hash by a to_h method,
186-
# that is used as or to configure a State object.
183+
# it. _opts_ is
184+
# * a Hash like object (responding to +to_hash+),
185+
# * or an object convertible into a hash by a +to_h+ method,
186+
# * or a <tt>JSON::State</tt> object.
187187
#
188-
# It defaults to a state object, that creates the shortest possible JSON text
189-
# in one line, checks for circular data structures and doesn't allow NaN,
188+
# If hash-alike or hash-convertible object is provided, it is internally
189+
# converted into a State object.
190+
#
191+
# The default options are set to create the shortest possible JSON text
192+
# in one line, check for circular data structures and do not allow NaN,
190193
# Infinity, and -Infinity.
191194
#
192-
# A _state_ hash can have the following keys:
193-
# * *indent*: a string used to indent levels (default: ''),
194-
# * *space*: a string that is put after, a : or , delimiter (default: ''),
195-
# * *space_before*: a string that is put before a : pair delimiter (default: ''),
196-
# * *object_nl*: a string that is put at the end of a JSON object (default: ''),
197-
# * *array_nl*: a string that is put at the end of a JSON array (default: ''),
195+
# An _opts_ hash can have the following keys:
196+
# * *indent*: a string used to indent levels (default: <tt>''</tt>),
197+
# * *space*: a string that is put after a <tt>:</tt> pair delimiter (default: <tt>''</tt>),
198+
# * *space_before*: a string that is put before a <tt>:</tt> pair delimiter (default: <tt>''</tt>),
199+
# * *object_nl*: a string that is put at the end of a JSON object (default: <tt>''</tt>),
200+
# * *array_nl*: a string that is put at the end of a JSON array (default: <tt>''</tt>),
198201
# * *allow_nan*: true if NaN, Infinity, and -Infinity should be
199202
# generated, otherwise an exception is thrown if these values are
200203
# encountered. This options defaults to false.
201204
# * *max_nesting*: The maximum depth of nesting allowed in the data
202205
# structures from which JSON is to be generated. Disable depth checking
203-
# with :max_nesting => false, it defaults to 100.
206+
# with <tt>max_nesting: false</tt>, it defaults to 100.
204207
#
205208
# See also the fast_generate for the fastest creation method with the least
206209
# amount of sanity checks, and the pretty_generate method for some

0 commit comments

Comments
 (0)