json 1.5.1-java → 1.5.2-java
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of json might be problematic. Click here for more details.
- data/lib/json.rb +52 -0
- data/lib/json/add/core.rb +35 -4
- data/lib/json/common.rb +5 -1
- data/lib/json/ext/generator.jar +0 -0
- data/lib/json/ext/parser.jar +0 -0
- data/lib/json/pure/generator.rb +1 -1
- data/lib/json/version.rb +1 -1
- data/tests/test_json.rb +25 -6
- data/tests/test_json_generate.rb +1 -0
- metadata +47 -53
data/lib/json.rb
CHANGED
@@ -1,3 +1,55 @@
|
|
1
|
+
##
|
2
|
+
# = JavaScript Object Notation (JSON)
|
3
|
+
#
|
4
|
+
# JSON is a lightweight data-interchange format. It is easy for us
|
5
|
+
# humans to read and write. Plus, equally simple for machines to generate or parse.
|
6
|
+
# JSON is completely language agnostic, making it the ideal interchange format.
|
7
|
+
#
|
8
|
+
# Built on two universally available structures:
|
9
|
+
# 1. A collection of name/value pairs. Often referred to as an _object_, hash table, record, struct, keyed list, or associative array.
|
10
|
+
# 2. An orderd list of values. More commonly named as an _array_, vector, sequence, or list.
|
11
|
+
#
|
12
|
+
# To read more about JSON visit: http://json.org
|
13
|
+
#
|
14
|
+
# == Parsing JSON
|
15
|
+
#
|
16
|
+
# To parse a JSON string received by another application, or generated within
|
17
|
+
# your existing application:
|
18
|
+
#
|
19
|
+
# require 'json'
|
20
|
+
#
|
21
|
+
# my_hash = JSON.parse('{"hello": "goodbye"}')
|
22
|
+
# puts my_hash["hello"] => "goodbye"
|
23
|
+
#
|
24
|
+
# Notice the extra quotes <tt>''</tt> around the hash notation. Ruby expects
|
25
|
+
# the argument to be a string and can't convert objects like a hash or array.
|
26
|
+
#
|
27
|
+
# Ruby converts your string into a hash
|
28
|
+
#
|
29
|
+
# == Generating JSON
|
30
|
+
#
|
31
|
+
# Creating a JSON string for communication or serialization is
|
32
|
+
# just as simple.
|
33
|
+
#
|
34
|
+
# require 'json'
|
35
|
+
#
|
36
|
+
# my_hash = {:hello => "goodbye"}
|
37
|
+
# puts JSON.generate(my_hash) => "{\"hello\":\"goodbye\"}"
|
38
|
+
#
|
39
|
+
# Or an alternative way:
|
40
|
+
#
|
41
|
+
# require 'json'
|
42
|
+
# puts {:hello => "goodbye"}.to_json => "{\"hello\":\"goodbye\"}"
|
43
|
+
#
|
44
|
+
# <tt>JSON.generate</tt> only allows objects or arrays to be converted
|
45
|
+
# to JSON syntax. While <tt>to_json</tt> accepts many Ruby classes
|
46
|
+
# even though it only acts a method for serialization:
|
47
|
+
#
|
48
|
+
# require 'json'
|
49
|
+
#
|
50
|
+
# 1.to_json => "1"
|
51
|
+
#
|
52
|
+
|
1
53
|
require 'json/common'
|
2
54
|
module JSON
|
3
55
|
require 'json/version'
|
data/lib/json/add/core.rb
CHANGED
@@ -6,20 +6,26 @@ unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
|
6
6
|
end
|
7
7
|
require 'date'
|
8
8
|
|
9
|
+
# Symbol serialization/deserialization
|
9
10
|
class Symbol
|
11
|
+
# Stores class name (Symbol) with String representation of Symbol as a JSON string.
|
10
12
|
def to_json(*a)
|
11
13
|
{
|
12
14
|
JSON.create_id => self.class.name,
|
13
15
|
's' => to_s,
|
14
16
|
}.to_json(*a)
|
15
17
|
end
|
16
|
-
|
18
|
+
|
19
|
+
# Deserializes JSON string by converting the <tt>string</tt> value stored in the object to a Symbol
|
17
20
|
def self.json_create(o)
|
18
21
|
o['s'].to_sym
|
19
22
|
end
|
20
23
|
end
|
21
24
|
|
25
|
+
# Time serialization/deserialization
|
22
26
|
class Time
|
27
|
+
|
28
|
+
# Deserializes JSON string by converting time since epoch to Time
|
23
29
|
def self.json_create(object)
|
24
30
|
if usec = object.delete('u') # used to be tv_usec -> tv_nsec
|
25
31
|
object['n'] = usec * 1000
|
@@ -30,7 +36,8 @@ class Time
|
|
30
36
|
at(object['s'], object['n'] / 1000)
|
31
37
|
end
|
32
38
|
end
|
33
|
-
|
39
|
+
|
40
|
+
# Stores class name (Time) with number of seconds since epoch and number of microseconds for Time as JSON string
|
34
41
|
def to_json(*args)
|
35
42
|
{
|
36
43
|
JSON.create_id => self.class.name,
|
@@ -40,13 +47,17 @@ class Time
|
|
40
47
|
end
|
41
48
|
end
|
42
49
|
|
50
|
+
# Date serialization/deserialization
|
43
51
|
class Date
|
52
|
+
|
53
|
+
# Deserializes JSON string by converting Julian year <tt>y</tt>, month <tt>m</tt>, day <tt>d</tt> and Day of Calendar Reform <tt>sg</tt> to Date.
|
44
54
|
def self.json_create(object)
|
45
55
|
civil(*object.values_at('y', 'm', 'd', 'sg'))
|
46
56
|
end
|
47
57
|
|
48
58
|
alias start sg unless method_defined?(:start)
|
49
|
-
|
59
|
+
|
60
|
+
# Stores class name (Date) with Julian year <tt>y</tt>, month <tt>m</tt>, day <tt>d</tt> and Day of Calendar Reform <tt>sg</tt> as JSON string
|
50
61
|
def to_json(*args)
|
51
62
|
{
|
52
63
|
JSON.create_id => self.class.name,
|
@@ -58,7 +69,10 @@ class Date
|
|
58
69
|
end
|
59
70
|
end
|
60
71
|
|
72
|
+
# DateTime serialization/deserialization
|
61
73
|
class DateTime
|
74
|
+
|
75
|
+
# Deserializes JSON string by converting year <tt>y</tt>, month <tt>m</tt>, day <tt>d</tt>, hour <tt>H</tt>, minute <tt>M</tt>, second <tt>S</tt>, offset <tt>of</tt> and Day of Calendar Reform <tt>sg</tt> to DateTime.
|
62
76
|
def self.json_create(object)
|
63
77
|
args = object.values_at('y', 'm', 'd', 'H', 'M', 'S')
|
64
78
|
of_a, of_b = object['of'].split('/')
|
@@ -72,7 +86,8 @@ class DateTime
|
|
72
86
|
end
|
73
87
|
|
74
88
|
alias start sg unless method_defined?(:start)
|
75
|
-
|
89
|
+
|
90
|
+
# Stores class name (DateTime) with Julian year <tt>y</tt>, month <tt>m</tt>, day <tt>d</tt>, hour <tt>H</tt>, minute <tt>M</tt>, second <tt>S</tt>, offset <tt>of</tt> and Day of Calendar Reform <tt>sg</tt> as JSON string
|
76
91
|
def to_json(*args)
|
77
92
|
{
|
78
93
|
JSON.create_id => self.class.name,
|
@@ -88,11 +103,15 @@ class DateTime
|
|
88
103
|
end
|
89
104
|
end
|
90
105
|
|
106
|
+
# Range serialization/deserialization
|
91
107
|
class Range
|
108
|
+
|
109
|
+
# Deserializes JSON string by constructing new Range object with arguments <tt>a</tt> serialized by <tt>to_json</tt>.
|
92
110
|
def self.json_create(object)
|
93
111
|
new(*object['a'])
|
94
112
|
end
|
95
113
|
|
114
|
+
# Stores class name (Range) with JSON array of arguments <tt>a</tt> which include <tt>first</tt> (integer), <tt>last</tt> (integer), and <tt>exclude_end?</tt> (boolean) as JSON string.
|
96
115
|
def to_json(*args)
|
97
116
|
{
|
98
117
|
JSON.create_id => self.class.name,
|
@@ -101,11 +120,15 @@ class Range
|
|
101
120
|
end
|
102
121
|
end
|
103
122
|
|
123
|
+
# Struct serialization/deserialization
|
104
124
|
class Struct
|
125
|
+
|
126
|
+
# Deserializes JSON string by constructing new Struct object with values <tt>v</tt> serialized by <tt>to_json</tt>.
|
105
127
|
def self.json_create(object)
|
106
128
|
new(*object['v'])
|
107
129
|
end
|
108
130
|
|
131
|
+
# Stores class name (Struct) with Struct values <tt>v</tt> as a JSON string. Only named structs are supported.
|
109
132
|
def to_json(*args)
|
110
133
|
klass = self.class.name
|
111
134
|
klass.to_s.empty? and raise JSON::JSONError, "Only named structs are supported!"
|
@@ -116,13 +139,17 @@ class Struct
|
|
116
139
|
end
|
117
140
|
end
|
118
141
|
|
142
|
+
# Exception serialization/deserialization
|
119
143
|
class Exception
|
144
|
+
|
145
|
+
# Deserializes JSON string by constructing new Exception object with message <tt>m</tt> and backtrace <tt>b</tt> serialized with <tt>to_json</tt>
|
120
146
|
def self.json_create(object)
|
121
147
|
result = new(object['m'])
|
122
148
|
result.set_backtrace object['b']
|
123
149
|
result
|
124
150
|
end
|
125
151
|
|
152
|
+
# Stores class name (Exception) with message <tt>m</tt> and backtrace array <tt>b</tt> as JSON string
|
126
153
|
def to_json(*args)
|
127
154
|
{
|
128
155
|
JSON.create_id => self.class.name,
|
@@ -132,11 +159,15 @@ class Exception
|
|
132
159
|
end
|
133
160
|
end
|
134
161
|
|
162
|
+
# Regexp serialization/deserialization
|
135
163
|
class Regexp
|
164
|
+
|
165
|
+
# Deserializes JSON string by constructing new Regexp object with source <tt>s</tt> (Regexp or String) and options <tt>o</tt> serialized by <tt>to_json</tt>
|
136
166
|
def self.json_create(object)
|
137
167
|
new(object['s'], object['o'])
|
138
168
|
end
|
139
169
|
|
170
|
+
# Stores class name (Regexp) with options <tt>o</tt> and source <tt>s</tt> (Regexp or String) as JSON string
|
140
171
|
def to_json(*)
|
141
172
|
{
|
142
173
|
JSON.create_id => self.class.name,
|
data/lib/json/common.rb
CHANGED
@@ -291,7 +291,8 @@ module JSON
|
|
291
291
|
recurse_proc(result, &proc) if proc
|
292
292
|
result
|
293
293
|
end
|
294
|
-
|
294
|
+
|
295
|
+
# Recursively calls passed _Proc_ if the parsed data structure is an _Array_ or _Hash_
|
295
296
|
def recurse_proc(result, &proc)
|
296
297
|
case result
|
297
298
|
when Array
|
@@ -351,11 +352,13 @@ module JSON
|
|
351
352
|
|
352
353
|
# Shortuct for iconv.
|
353
354
|
if ::String.method_defined?(:encode)
|
355
|
+
# Encodes string using Ruby's _String.encode_
|
354
356
|
def self.iconv(to, from, string)
|
355
357
|
string.encode(to, from)
|
356
358
|
end
|
357
359
|
else
|
358
360
|
require 'iconv'
|
361
|
+
# Encodes string using _iconv_ library
|
359
362
|
def self.iconv(to, from, string)
|
360
363
|
Iconv.iconv(to, from, string).first
|
361
364
|
end
|
@@ -408,6 +411,7 @@ module ::Kernel
|
|
408
411
|
end
|
409
412
|
end
|
410
413
|
|
414
|
+
# Extends any Class to include _json_creatable?_ method.
|
411
415
|
class ::Class
|
412
416
|
# Returns true, if this class can be used to create an instance
|
413
417
|
# from a serialised JSON string. The class has to implement a class
|
data/lib/json/ext/generator.jar
CHANGED
Binary file
|
data/lib/json/ext/parser.jar
CHANGED
Binary file
|
data/lib/json/pure/generator.rb
CHANGED
@@ -99,7 +99,7 @@ module JSON
|
|
99
99
|
module Pure
|
100
100
|
module Generator
|
101
101
|
# This class is used to create State instances, that are use to hold data
|
102
|
-
# while generating a JSON text from a
|
102
|
+
# while generating a JSON text from a Ruby data structure.
|
103
103
|
class State
|
104
104
|
# Creates a State object from _opts_, which ought to be Hash to create
|
105
105
|
# a new State instance configured by _opts_, something else to create
|
data/lib/json/version.rb
CHANGED
data/tests/test_json.rb
CHANGED
@@ -154,7 +154,16 @@ class TC_JSON < Test::Unit::TestCase
|
|
154
154
|
, [2718.0E-3 ],\r[ null] , [[1, -2, 3 ]], [false ],[ true]\n ] }))
|
155
155
|
end
|
156
156
|
|
157
|
-
class SubArray < Array
|
157
|
+
class SubArray < Array
|
158
|
+
def <<(v)
|
159
|
+
@shifted = true
|
160
|
+
super
|
161
|
+
end
|
162
|
+
|
163
|
+
def shifted?
|
164
|
+
@shifted
|
165
|
+
end
|
166
|
+
end
|
158
167
|
|
159
168
|
class SubArray2 < Array
|
160
169
|
def to_json(*a)
|
@@ -171,9 +180,10 @@ class TC_JSON < Test::Unit::TestCase
|
|
171
180
|
end
|
172
181
|
|
173
182
|
def test_parse_array_custom_class
|
174
|
-
res = parse('[]', :array_class => SubArray)
|
175
|
-
assert_equal([], res)
|
183
|
+
res = parse('[1,2]', :array_class => SubArray)
|
184
|
+
assert_equal([1,2], res)
|
176
185
|
assert_equal(SubArray, res.class)
|
186
|
+
assert res.shifted?
|
177
187
|
end
|
178
188
|
|
179
189
|
def test_parse_object
|
@@ -184,6 +194,14 @@ class TC_JSON < Test::Unit::TestCase
|
|
184
194
|
end
|
185
195
|
|
186
196
|
class SubHash < Hash
|
197
|
+
def []=(k, v)
|
198
|
+
@item_set = true
|
199
|
+
super
|
200
|
+
end
|
201
|
+
|
202
|
+
def item_set?
|
203
|
+
@item_set
|
204
|
+
end
|
187
205
|
end
|
188
206
|
|
189
207
|
class SubHash2 < Hash
|
@@ -200,9 +218,10 @@ class TC_JSON < Test::Unit::TestCase
|
|
200
218
|
end
|
201
219
|
|
202
220
|
def test_parse_object_custom_class
|
203
|
-
res = parse('{}', :object_class =>
|
204
|
-
assert_equal({}, res)
|
205
|
-
assert_equal(
|
221
|
+
res = parse('{"foo":"bar"}', :object_class => SubHash)
|
222
|
+
assert_equal({"foo" => "bar"}, res)
|
223
|
+
assert_equal(SubHash, res.class)
|
224
|
+
assert res.item_set?
|
206
225
|
end
|
207
226
|
|
208
227
|
def test_generation_of_core_subclasses_with_new_to_json
|
data/tests/test_json_generate.rb
CHANGED
@@ -54,6 +54,7 @@ EOT
|
|
54
54
|
|
55
55
|
def test_generate_pretty
|
56
56
|
json = pretty_generate(@hash)
|
57
|
+
# hashes aren't (insertion) ordered on every ruby implementation assert_equal(@json3, json)
|
57
58
|
assert_equal(JSON.parse(@json3), JSON.parse(json))
|
58
59
|
parsed_json = parse(json)
|
59
60
|
assert_equal(@hash, parsed_json)
|
metadata
CHANGED
@@ -1,12 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
prerelease:
|
5
|
-
|
6
|
-
- 1
|
7
|
-
- 5
|
8
|
-
- 1
|
9
|
-
version: 1.5.1
|
4
|
+
prerelease:
|
5
|
+
version: 1.5.2
|
10
6
|
platform: java
|
11
7
|
authors:
|
12
8
|
- Daniel Luz
|
@@ -14,7 +10,7 @@ autorequire:
|
|
14
10
|
bindir: bin
|
15
11
|
cert_chain: []
|
16
12
|
|
17
|
-
date: 2011-
|
13
|
+
date: 2011-06-15 00:00:00 +02:00
|
18
14
|
default_executable:
|
19
15
|
dependencies: []
|
20
16
|
|
@@ -29,64 +25,64 @@ extra_rdoc_files: []
|
|
29
25
|
files:
|
30
26
|
- lib/json.rb
|
31
27
|
- lib/json/json.xpm
|
32
|
-
- lib/json/Key.xpm
|
33
|
-
- lib/json/String.xpm
|
34
|
-
- lib/json/Numeric.xpm
|
35
|
-
- lib/json/Hash.xpm
|
36
|
-
- lib/json/common.rb
|
37
|
-
- lib/json/Array.xpm
|
38
|
-
- lib/json/FalseClass.xpm
|
39
28
|
- lib/json/TrueClass.xpm
|
40
|
-
- lib/json/pure.rb
|
41
29
|
- lib/json/version.rb
|
30
|
+
- lib/json/Array.xpm
|
31
|
+
- lib/json/common.rb
|
42
32
|
- lib/json/ext.rb
|
33
|
+
- lib/json/pure.rb
|
34
|
+
- lib/json/Key.xpm
|
35
|
+
- lib/json/FalseClass.xpm
|
43
36
|
- lib/json/editor.rb
|
37
|
+
- lib/json/Numeric.xpm
|
44
38
|
- lib/json/NilClass.xpm
|
45
|
-
- lib/json/
|
39
|
+
- lib/json/String.xpm
|
40
|
+
- lib/json/Hash.xpm
|
46
41
|
- lib/json/add/core.rb
|
47
|
-
- lib/json/
|
48
|
-
- lib/json/ext/generator.jar
|
42
|
+
- lib/json/add/rails.rb
|
49
43
|
- lib/json/pure/generator.rb
|
50
44
|
- lib/json/pure/parser.rb
|
51
|
-
-
|
52
|
-
-
|
53
|
-
- tests/test_json.rb
|
45
|
+
- lib/json/ext/generator.jar
|
46
|
+
- lib/json/ext/parser.jar
|
54
47
|
- tests/test_json_string_matching.rb
|
55
|
-
- tests/test_json_generate.rb
|
56
|
-
- tests/test_json_unicode.rb
|
57
|
-
- tests/setup_variant.rb
|
58
48
|
- tests/test_json_fixtures.rb
|
59
|
-
- tests/
|
60
|
-
- tests/
|
61
|
-
- tests/
|
49
|
+
- tests/setup_variant.rb
|
50
|
+
- tests/test_json_unicode.rb
|
51
|
+
- tests/test_json_addition.rb
|
52
|
+
- tests/test_json_generate.rb
|
53
|
+
- tests/test_json_encoding.rb
|
54
|
+
- tests/test_json.rb
|
55
|
+
- tests/fixtures/fail6.json
|
56
|
+
- tests/fixtures/fail9.json
|
57
|
+
- tests/fixtures/fail10.json
|
58
|
+
- tests/fixtures/fail24.json
|
62
59
|
- tests/fixtures/fail28.json
|
60
|
+
- tests/fixtures/fail13.json
|
61
|
+
- tests/fixtures/fail4.json
|
62
|
+
- tests/fixtures/pass3.json
|
63
|
+
- tests/fixtures/fail11.json
|
64
|
+
- tests/fixtures/fail14.json
|
65
|
+
- tests/fixtures/fail3.json
|
66
|
+
- tests/fixtures/fail12.json
|
67
|
+
- tests/fixtures/pass16.json
|
68
|
+
- tests/fixtures/pass15.json
|
69
|
+
- tests/fixtures/fail20.json
|
63
70
|
- tests/fixtures/fail8.json
|
64
|
-
- tests/fixtures/fail19.json
|
65
71
|
- tests/fixtures/pass2.json
|
72
|
+
- tests/fixtures/fail5.json
|
73
|
+
- tests/fixtures/fail1.json
|
74
|
+
- tests/fixtures/fail25.json
|
75
|
+
- tests/fixtures/pass17.json
|
76
|
+
- tests/fixtures/fail7.json
|
66
77
|
- tests/fixtures/pass26.json
|
67
|
-
- tests/fixtures/pass1.json
|
68
|
-
- tests/fixtures/fail3.json
|
69
|
-
- tests/fixtures/fail20.json
|
70
|
-
- tests/fixtures/pass3.json
|
71
|
-
- tests/fixtures/pass15.json
|
72
|
-
- tests/fixtures/fail12.json
|
73
|
-
- tests/fixtures/fail13.json
|
74
|
-
- tests/fixtures/fail22.json
|
75
|
-
- tests/fixtures/fail24.json
|
76
|
-
- tests/fixtures/fail9.json
|
77
|
-
- tests/fixtures/fail2.json
|
78
|
-
- tests/fixtures/fail14.json
|
79
|
-
- tests/fixtures/fail6.json
|
80
78
|
- tests/fixtures/fail21.json
|
81
|
-
- tests/fixtures/
|
82
|
-
- tests/fixtures/
|
83
|
-
- tests/fixtures/fail11.json
|
84
|
-
- tests/fixtures/fail25.json
|
85
|
-
- tests/fixtures/fail5.json
|
79
|
+
- tests/fixtures/pass1.json
|
80
|
+
- tests/fixtures/fail23.json
|
86
81
|
- tests/fixtures/fail18.json
|
82
|
+
- tests/fixtures/fail2.json
|
83
|
+
- tests/fixtures/fail22.json
|
87
84
|
- tests/fixtures/fail27.json
|
88
|
-
- tests/fixtures/
|
89
|
-
- tests/fixtures/fail23.json
|
85
|
+
- tests/fixtures/fail19.json
|
90
86
|
has_rdoc: true
|
91
87
|
homepage: http://json-jruby.rubyforge.org/
|
92
88
|
licenses: []
|
@@ -97,23 +93,21 @@ rdoc_options: []
|
|
97
93
|
require_paths:
|
98
94
|
- lib
|
99
95
|
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
100
97
|
requirements:
|
101
98
|
- - ">="
|
102
99
|
- !ruby/object:Gem::Version
|
103
|
-
segments:
|
104
|
-
- 0
|
105
100
|
version: "0"
|
106
101
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
none: false
|
107
103
|
requirements:
|
108
104
|
- - ">="
|
109
105
|
- !ruby/object:Gem::Version
|
110
|
-
segments:
|
111
|
-
- 0
|
112
106
|
version: "0"
|
113
107
|
requirements: []
|
114
108
|
|
115
109
|
rubyforge_project: json-jruby
|
116
|
-
rubygems_version: 1.
|
110
|
+
rubygems_version: 1.5.1
|
117
111
|
signing_key:
|
118
112
|
specification_version: 3
|
119
113
|
summary: JSON implementation for JRuby
|