json-jruby 1.2.0-universal-java-1.6 → 1.2.2-universal-java-1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,6 +7,19 @@ unless Object.const_defined?(:JSON) and ::JSON.const_defined?(:JSON_LOADED) and
7
7
  end
8
8
  require 'date'
9
9
 
10
+ class Symbol
11
+ def to_json(*a)
12
+ {
13
+ JSON.create_id => self.class.name,
14
+ 's' => to_s,
15
+ }.to_json(*a)
16
+ end
17
+
18
+ def self.json_create(o)
19
+ o['s'].to_sym
20
+ end
21
+ end
22
+
10
23
  class Time
11
24
  def self.json_create(object)
12
25
  if usec = object.delete('u') # used to be tv_usec -> tv_nsec
@@ -21,7 +34,7 @@ class Time
21
34
 
22
35
  def to_json(*args)
23
36
  {
24
- 'json_class' => self.class.name,
37
+ JSON.create_id => self.class.name,
25
38
  's' => tv_sec,
26
39
  'n' => respond_to?(:tv_nsec) ? tv_nsec : tv_usec * 1000
27
40
  }.to_json(*args)
@@ -37,7 +50,7 @@ class Date
37
50
 
38
51
  def to_json(*args)
39
52
  {
40
- 'json_class' => self.class.name,
53
+ JSON.create_id => self.class.name,
41
54
  'y' => year,
42
55
  'm' => month,
43
56
  'd' => day,
@@ -63,7 +76,7 @@ class DateTime
63
76
 
64
77
  def to_json(*args)
65
78
  {
66
- 'json_class' => self.class.name,
79
+ JSON.create_id => self.class.name,
67
80
  'y' => year,
68
81
  'm' => month,
69
82
  'd' => day,
@@ -83,7 +96,7 @@ class Range
83
96
 
84
97
  def to_json(*args)
85
98
  {
86
- 'json_class' => self.class.name,
99
+ JSON.create_id => self.class.name,
87
100
  'a' => [ first, last, exclude_end? ]
88
101
  }.to_json(*args)
89
102
  end
@@ -98,7 +111,7 @@ class Struct
98
111
  klass = self.class.name
99
112
  klass.to_s.empty? and raise JSON::JSONError, "Only named structs are supported!"
100
113
  {
101
- 'json_class' => klass,
114
+ JSON.create_id => klass,
102
115
  'v' => values,
103
116
  }.to_json(*args)
104
117
  end
@@ -113,7 +126,7 @@ class Exception
113
126
 
114
127
  def to_json(*args)
115
128
  {
116
- 'json_class' => self.class.name,
129
+ JSON.create_id => self.class.name,
117
130
  'm' => message,
118
131
  'b' => backtrace,
119
132
  }.to_json(*args)
@@ -127,7 +140,7 @@ class Regexp
127
140
 
128
141
  def to_json(*)
129
142
  {
130
- 'json_class' => self.class.name,
143
+ JSON.create_id => self.class.name,
131
144
  'o' => options,
132
145
  's' => source,
133
146
  }.to_json
@@ -10,7 +10,7 @@ class Object
10
10
  def self.json_create(object)
11
11
  obj = new
12
12
  for key, value in object
13
- next if key == 'json_class'
13
+ next if key == JSON.create_id
14
14
  instance_variable_set "@#{key}", value
15
15
  end
16
16
  obj
@@ -18,7 +18,7 @@ class Object
18
18
 
19
19
  def to_json(*a)
20
20
  result = {
21
- 'json_class' => self.class.name
21
+ JSON.create_id => self.class.name
22
22
  }
23
23
  instance_variables.inject(result) do |r, name|
24
24
  r[name[1..-1]] = instance_variable_get name
@@ -116,9 +116,14 @@ module JSON
116
116
  # * *allow_nan*: If set to true, allow NaN, Infinity and -Infinity in
117
117
  # defiance of RFC 4627 to be parsed by the Parser. This option defaults
118
118
  # to false.
119
+ # * *symbolize_names*: If set to true, returns symbols for the names
120
+ # (keys) in a JSON object. Otherwise strings are returned, which is also
121
+ # the default.
119
122
  # * *create_additions*: If set to false, the Parser doesn't create
120
123
  # additions even if a matchin class and create_id was found. This option
121
124
  # defaults to true.
125
+ # * *object_class*: Defaults to Hash
126
+ # * *array_class*: Defaults to Array
122
127
  def parse(source, opts = {})
123
128
  JSON.parser.new(source, opts).parse
124
129
  end
Binary file
Binary file
@@ -60,6 +60,9 @@ module JSON
60
60
  # * *allow_nan*: If set to true, allow NaN, Infinity and -Infinity in
61
61
  # defiance of RFC 4627 to be parsed by the Parser. This option defaults
62
62
  # to false.
63
+ # * *symbolize_names*: If set to true, returns symbols for the names
64
+ # (keys) in a JSON object. Otherwise strings are returned, which is also
65
+ # the default.
63
66
  # * *create_additions*: If set to false, the Parser doesn't create
64
67
  # additions even if a matchin class and create_id was found. This option
65
68
  # defaults to true.
@@ -109,6 +112,7 @@ module JSON
109
112
  @max_nesting = 0
110
113
  end
111
114
  @allow_nan = !!opts[:allow_nan]
115
+ @symbolize_names = !!opts[:symbolize_names]
112
116
  ca = true
113
117
  ca = opts[:create_additions] if opts.key?(:create_additions)
114
118
  @create_id = ca ? JSON.create_id : nil
@@ -267,7 +271,7 @@ module JSON
267
271
  end
268
272
  skip(IGNORE)
269
273
  unless (value = parse_value).equal? UNPARSED
270
- result[string] = value
274
+ result[@symbolize_names ? string.to_sym : string] = value
271
275
  delim = false
272
276
  skip(IGNORE)
273
277
  if scan(COLLECTION_DELIMITER)
@@ -1,6 +1,6 @@
1
1
  module JSON
2
2
  # JSON version
3
- VERSION = '1.2.0'
3
+ VERSION = '1.2.2'
4
4
  VERSION_ARRAY = VERSION.split(/\./).map { |x| x.to_i } # :nodoc:
5
5
  VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
6
6
  VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
@@ -302,6 +302,13 @@ EOT
302
302
  assert_equal too_deep, ok
303
303
  end
304
304
 
305
+ def test_symbolize_names
306
+ assert_equal({ "foo" => "bar", "baz" => "quux" },
307
+ JSON.parse('{"foo":"bar", "baz":"quux"}'))
308
+ assert_equal({ :foo => "bar", :baz => "quux" },
309
+ JSON.parse('{"foo":"bar", "baz":"quux"}', :symbolize_names => true))
310
+ end
311
+
305
312
  def test_load_dump
306
313
  too_deep = '[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]'
307
314
  assert_equal too_deep, JSON.dump(eval(too_deep))
@@ -95,7 +95,7 @@ class TC_JSONAddition < Test::Unit::TestCase
95
95
  c = C.new
96
96
  assert !C.json_creatable?
97
97
  json = generate(c)
98
- assert_raises(ArgumentError) { JSON.parse(json) }
98
+ assert_raises(ArgumentError, NameError) { JSON.parse(json) }
99
99
  end
100
100
 
101
101
  def test_raw_strings
@@ -110,11 +110,9 @@ class TC_JSONAddition < Test::Unit::TestCase
110
110
  json_raw_object = raw.to_json_raw_object
111
111
  hash = { 'json_class' => 'String', 'raw'=> raw_array }
112
112
  assert_equal hash, json_raw_object
113
- json_raw = <<EOT.chomp
114
- {\"json_class\":\"String\",\"raw\":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255]}
115
- EOT
116
- # "
117
- assert_equal json_raw, json
113
+ assert_match /\A\{.*\}\Z/, json
114
+ assert_match /"json_class":"String"/, json
115
+ assert_match /"raw":\[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255\]/, json
118
116
  raw_again = JSON.parse(json)
119
117
  assert_equal raw, raw_again
120
118
  end
@@ -116,7 +116,7 @@ class TC_JSONRails < Test::Unit::TestCase
116
116
  c = C.new # with rails addition all objects are theoretically creatable
117
117
  assert C.json_creatable?
118
118
  json = generate(c)
119
- assert_raises(ArgumentError) { JSON.parse(json) }
119
+ assert_raises(ArgumentError, NameError) { JSON.parse(json) }
120
120
  end
121
121
 
122
122
  def test_raw_strings
@@ -131,11 +131,9 @@ class TC_JSONRails < Test::Unit::TestCase
131
131
  json_raw_object = raw.to_json_raw_object
132
132
  hash = { 'json_class' => 'String', 'raw'=> raw_array }
133
133
  assert_equal hash, json_raw_object
134
- json_raw = <<EOT.chomp
135
- {\"json_class\":\"String\",\"raw\":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255]}
136
- EOT
137
- # "
138
- assert_equal json_raw, json
134
+ assert_match /\A\{.*\}\Z/, json
135
+ assert_match /"json_class":"String"/, json
136
+ assert_match /"raw":\[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255\]/, json
139
137
  raw_again = JSON.parse(json)
140
138
  assert_equal raw, raw_again
141
139
  end
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-jruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 2
8
+ - 2
9
+ version: 1.2.2
5
10
  platform: universal-java-1.6
6
11
  authors:
7
12
  - Daniel Luz
@@ -9,7 +14,7 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2009-11-28 00:00:00 -02:00
17
+ date: 2010-02-28 00:00:00 -03:00
13
18
  default_executable:
14
19
  dependencies: []
15
20
 
@@ -85,18 +90,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
85
90
  requirements:
86
91
  - - ">="
87
92
  - !ruby/object:Gem::Version
93
+ segments:
94
+ - 0
88
95
  version: "0"
89
- version:
90
96
  required_rubygems_version: !ruby/object:Gem::Requirement
91
97
  requirements:
92
98
  - - ">="
93
99
  - !ruby/object:Gem::Version
100
+ segments:
101
+ - 0
94
102
  version: "0"
95
- version:
96
103
  requirements: []
97
104
 
98
105
  rubyforge_project: json-jruby
99
- rubygems_version: 1.3.5
106
+ rubygems_version: 1.3.6
100
107
  signing_key:
101
108
  specification_version: 3
102
109
  summary: A JSON implementation as a JRuby extension