|
5 | 5 |
|
6 | 6 | class Range
|
7 | 7 |
|
8 |
| - # Deserializes JSON string by constructing new Range object with arguments |
9 |
| - # <tt>a</tt> serialized by <tt>to_json</tt>. |
| 8 | + # Returns a new \Range object constructed from <tt>object['a']</tt>, |
| 9 | + # which must be an array of values suitable for a call to Range.new: |
| 10 | + # |
| 11 | + # require 'json/add/range' |
| 12 | + # Range.json_create({"a"=>[1, 4]}) # => 1..4 |
| 13 | + # Range.json_create({"a"=>[1, 4, true]}) # => 1...4 |
| 14 | + # Range.json_create({"a" => ['a', 'd']}) # => "a".."d" |
| 15 | + # |
10 | 16 | def self.json_create(object)
|
11 | 17 | new(*object['a'])
|
12 | 18 | end
|
13 | 19 |
|
14 |
| - # Returns a hash, that will be turned into a JSON object and represent this |
15 |
| - # object. |
| 20 | + # Returns a 2-element hash representing +self+: |
| 21 | + # |
| 22 | + # require 'json/add/range' |
| 23 | + # (1..4).as_json # => {"json_class"=>"Range", "a"=>[1, 4, false]} |
| 24 | + # (1...4).as_json # => {"json_class"=>"Range", "a"=>[1, 4, true]} |
| 25 | + # ('a'..'d').as_json # => {"json_class"=>"Range", "a"=>["a", "d", false]} |
| 26 | + # |
16 | 27 | def as_json(*)
|
17 | 28 | {
|
18 | 29 | JSON.create_id => self.class.name,
|
19 | 30 | 'a' => [ first, last, exclude_end? ]
|
20 | 31 | }
|
21 | 32 | end
|
22 | 33 |
|
23 |
| - # Stores class name (Range) with JSON array of arguments <tt>a</tt> which |
24 |
| - # include <tt>first</tt> (integer), <tt>last</tt> (integer), and |
25 |
| - # <tt>exclude_end?</tt> (boolean) as JSON string. |
| 34 | + # Returns a JSON string representing +self+: |
| 35 | + # |
| 36 | + # require 'json/add/range' |
| 37 | + # (1..4).to_json # => "{\"json_class\":\"Range\",\"a\":[1,4,false]}" |
| 38 | + # (1...4).to_json # => "{\"json_class\":\"Range\",\"a\":[1,4,true]}" |
| 39 | + # ('a'..'d').to_json # => "{\"json_class\":\"Range\",\"a\":[\"a\",\"d\",false]}" |
| 40 | + # |
26 | 41 | def to_json(*args)
|
27 | 42 | as_json.to_json(*args)
|
28 | 43 | end
|
|
0 commit comments