json-schema 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -18,7 +18,7 @@ From the git repo:
18
18
 
19
19
  <pre>
20
20
  $ gem build json-schema.gemspec
21
- $ gem install json-schema-1.0.2.gem
21
+ $ gem install json-schema-1.0.3.gem
22
22
  </pre>
23
23
 
24
24
 
@@ -188,7 +188,7 @@ JSON::Validator.validate(schema,data) # => false
188
188
 
189
189
  h2. JSON Backends
190
190
 
191
- The JSON::Schema library currently supports the <code>json</code> and <code>yajl-ruby</code> backend JSON parsers. If either of these libraries are installed, they will be automatically loaded and used to parse any JSON strings supplied by the user.
191
+ The JSON Schema library currently supports the <code>json</code> and <code>yajl-ruby</code> backend JSON parsers. If either of these libraries are installed, they will be automatically loaded and used to parse any JSON strings supplied by the user.
192
192
 
193
193
  If more than one of the supported JSON backends are installed, the <code>yajl-ruby</code> parser is used by default. This can be changed by issuing the following before validation:
194
194
 
@@ -196,6 +196,7 @@ If more than one of the supported JSON backends are installed, the <code>yajl-ru
196
196
  JSON::Validator.json_backend = :json
197
197
  </pre>
198
198
 
199
+ Optionally, the JSON Schema library supports using the MultiJSON library for selecting JSON backends. If the MultiJSON library is installed, it will be autoloaded.
199
200
 
200
201
  h2. Notes
201
202
 
data/lib/json-schema.rb CHANGED
@@ -1,26 +1,19 @@
1
1
  require 'rubygems'
2
2
 
3
- def load_gem gemname
4
- if begin
5
- Gem::Specification::find_by_name(gemname)
6
- rescue Gem::LoadError
7
- false
8
- rescue
9
- Gem.available?(gemname)
10
- end
11
- require gemname
3
+ if begin
4
+ Gem::Specification::find_by_name('multi_json')
5
+ rescue Gem::LoadError
6
+ false
7
+ rescue
8
+ Gem.available?('multi_json')
12
9
  end
10
+ require 'multi_json'
11
+
12
+ # Force MultiJson to load an engine before we define the JSON constant here; otherwise,
13
+ # it looks for things that are under the JSON namespace that aren't there (since we have defined it here)
14
+ MultiJson.engine
13
15
  end
14
16
 
15
- load_gem('yajl')
16
- load_gem('json')
17
-
18
- require File.join(File.dirname(__FILE__),"json-schema/lib/multi_json/multi_json.rb")
19
-
20
- # Force MultiJson to load an engine before we define the JSON constant here; otherwise,
21
- # it looks for things that are under the JSON namespace that aren't there (since we have defined it here)
22
- MultiJson.engine
23
-
24
17
  $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/json-schema"
25
18
 
26
19
  require 'rubygems'
@@ -88,7 +88,10 @@ module JSON
88
88
  }
89
89
  @@validators = {}
90
90
  @@default_validator = nil
91
+ @@available_json_backends = []
92
+ @@json_backend = nil
91
93
  @@errors = []
94
+ @@serializer = nil
92
95
 
93
96
  def self.version_string_for(version)
94
97
  # I'm not a fan of this, but it's quick and dirty to get it working for now
@@ -321,16 +324,74 @@ module JSON
321
324
  end
322
325
 
323
326
  def json_backend
324
- MultiJson.engine
327
+ if defined?(MultiJson)
328
+ MultiJson.engine
329
+ else
330
+ @@json_backend
331
+ end
325
332
  end
326
333
 
327
334
  def json_backend=(backend)
328
- backend = 'json_gem' if backend.to_s == 'json'
329
- MultiJson.engine = backend
335
+ if defined?(MultiJson)
336
+ backend = backend == 'json' ? 'json_gem' : backend
337
+ MultiJson.engine = backend
338
+ else
339
+ backend = backend.to_s
340
+ if @@available_json_backends.include?(backend)
341
+ @@json_backend = backend
342
+ else
343
+ raise JSON::Schema::JsonParseError.new("The JSON backend '#{backend}' could not be found.")
344
+ end
345
+ end
330
346
  end
331
347
 
332
348
  def parse(s)
333
- MultiJson.decode(s)
349
+ if defined?(MultiJson)
350
+ MultiJson.decode(s)
351
+ else
352
+ case @@json_backend.to_s
353
+ when 'json'
354
+ JSON.parse(s)
355
+ when 'yajl'
356
+ json = StringIO.new(s)
357
+ parser = Yajl::Parser.new
358
+ parser.parse(json)
359
+ else
360
+ raise JSON::Schema::JsonParseError.new("No supported JSON parsers found. The following parsers are suported:\n * yajl-ruby\n * json")
361
+ end
362
+ end
363
+ end
364
+
365
+ if !defined?(MultiJson)
366
+ if begin
367
+ Gem::Specification::find_by_name('json')
368
+ rescue Gem::LoadError
369
+ false
370
+ rescue
371
+ Gem.available?('json')
372
+ end
373
+ require 'json'
374
+ @@available_json_backends << 'json'
375
+ @@json_backend = 'json'
376
+ end
377
+
378
+ if begin
379
+ Gem::Specification::find_by_name('yajl-ruby')
380
+ rescue Gem::LoadError
381
+ false
382
+ rescue
383
+ Gem.available?('yajl-ruby')
384
+ end
385
+ require 'yajl'
386
+ @@available_json_backends << 'yajl'
387
+ @@json_backend = 'yajl'
388
+ end
389
+
390
+ if @@json_backend == 'yajl'
391
+ @@serializer = lambda{|o| Yajl::Encoder.encode(o) }
392
+ else
393
+ @@serializer = lambda{|o| Marshal.dump(o) }
394
+ end
334
395
  end
335
396
  end
336
397
 
@@ -351,7 +412,11 @@ module JSON
351
412
  end
352
413
 
353
414
  def serialize schema
354
- MultiJson.encode(schema)
415
+ if defined?(MultiJson)
416
+ MultiJson.encode(schema)
417
+ else
418
+ @@serializer.call(schema)
419
+ end
355
420
  end
356
421
 
357
422
  def fake_uri schema
data/test/test_files.rb CHANGED
@@ -8,28 +8,36 @@ class JSONSchemaTest < Test::Unit::TestCase
8
8
  #
9
9
 
10
10
  def test_schema_from_file
11
- data = {"a" => 5}
12
- assert(JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_1.json"),data))
13
- data = {"a" => "bad"}
14
- assert(!JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_1.json"),data))
11
+ if JSON::Validator.json_backend != nil
12
+ data = {"a" => 5}
13
+ assert(JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_1.json"),data))
14
+ data = {"a" => "bad"}
15
+ assert(!JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_1.json"),data))
16
+ end
15
17
  end
16
18
 
17
19
  def test_data_from_file
18
- schema = {"type" => "object", "properties" => {"a" => {"type" => "integer"}}}
19
- assert(JSON::Validator.validate(schema,File.join(File.dirname(__FILE__),"data/good_data_1.json")))
20
- assert(!JSON::Validator.validate(schema,File.join(File.dirname(__FILE__),"data/bad_data_1.json")))
20
+ if JSON::Validator.json_backend != nil
21
+ schema = {"type" => "object", "properties" => {"a" => {"type" => "integer"}}}
22
+ assert(JSON::Validator.validate(schema,File.join(File.dirname(__FILE__),"data/good_data_1.json")))
23
+ assert(!JSON::Validator.validate(schema,File.join(File.dirname(__FILE__),"data/bad_data_1.json")))
24
+ end
21
25
  end
22
26
 
23
27
  def test_both_from_file
24
- assert(JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_1.json"),File.join(File.dirname(__FILE__),"data/good_data_1.json")))
25
- assert(!JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_1.json"),File.join(File.dirname(__FILE__),"data/bad_data_1.json")))
28
+ if JSON::Validator.json_backend != nil
29
+ assert(JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_1.json"),File.join(File.dirname(__FILE__),"data/good_data_1.json")))
30
+ assert(!JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_1.json"),File.join(File.dirname(__FILE__),"data/bad_data_1.json")))
31
+ end
26
32
  end
27
33
 
28
34
  def test_file_ref
29
- data = {"b" => {"a" => 5}}
30
- assert(JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_2.json"),data))
35
+ if JSON::Validator.json_backend != nil
36
+ data = {"b" => {"a" => 5}}
37
+ assert(JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_2.json"),data))
31
38
 
32
- data = {"b" => {"a" => "boo"}}
33
- assert(!JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_1.json"),data))
39
+ data = {"b" => {"a" => "boo"}}
40
+ assert(!JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_1.json"),data))
41
+ end
34
42
  end
35
43
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 0
8
- - 2
9
- version: 1.0.2
8
+ - 3
9
+ version: 1.0.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Kenny Hoxworth
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2012-02-13 00:00:00 -05:00
17
+ date: 2012-02-14 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -27,7 +27,6 @@ extensions: []
27
27
  extra_rdoc_files:
28
28
  - README.textile
29
29
  - LICENSE.md
30
- - LICENSE-multi_json.md
31
30
  files:
32
31
  - lib/json-schema/attributes/additionalitems.rb
33
32
  - lib/json-schema/attributes/additionalproperties.rb
@@ -54,14 +53,6 @@ files:
54
53
  - lib/json-schema/attributes/ref.rb
55
54
  - lib/json-schema/attributes/type.rb
56
55
  - lib/json-schema/attributes/uniqueitems.rb
57
- - lib/json-schema/lib/multi_json/multi_json/engines/json_common.rb
58
- - lib/json-schema/lib/multi_json/multi_json/engines/json_gem.rb
59
- - lib/json-schema/lib/multi_json/multi_json/engines/json_pure.rb
60
- - lib/json-schema/lib/multi_json/multi_json/engines/ok_json.rb
61
- - lib/json-schema/lib/multi_json/multi_json/engines/yajl.rb
62
- - lib/json-schema/lib/multi_json/multi_json/vendor/ok_json.rb
63
- - lib/json-schema/lib/multi_json/multi_json/version.rb
64
- - lib/json-schema/lib/multi_json/multi_json.rb
65
56
  - lib/json-schema/schema.rb
66
57
  - lib/json-schema/uri/file.rb
67
58
  - lib/json-schema/uri/uuid.rb
@@ -75,7 +66,6 @@ files:
75
66
  - resources/draft-03.json
76
67
  - README.textile
77
68
  - LICENSE.md
78
- - LICENSE-multi_json.md
79
69
  has_rdoc: true
80
70
  homepage: http://github.com/hoxworth/json-schema/tree/master
81
71
  licenses: []
@@ -1,20 +0,0 @@
1
- Copyright (c) 2010 Michael Bleigh, Josh Kalderimis, Erik Michaels-Ober, and Intridea, Inc.
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining
4
- a copy of this software and associated documentation files (the
5
- "Software"), to deal in the Software without restriction, including
6
- without limitation the rights to use, copy, modify, merge, publish,
7
- distribute, sublicense, and/or sell copies of the Software, and to
8
- permit persons to whom the Software is furnished to do so, subject to
9
- the following conditions:
10
-
11
- The above copyright notice and this permission notice shall be
12
- included in all copies or substantial portions of the Software.
13
-
14
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,85 +0,0 @@
1
- module MultiJson
2
- class DecodeError < StandardError
3
- attr_reader :data
4
- def initialize(message, backtrace, data)
5
- super(message)
6
- self.set_backtrace(backtrace)
7
- @data = data
8
- end
9
- end
10
-
11
- module_function
12
-
13
- @engine = nil
14
-
15
- # Get the current engine class.
16
- def engine
17
- return @engine if @engine
18
- self.engine = self.default_engine
19
- @engine
20
- end
21
-
22
- REQUIREMENT_MAP = [
23
- ["yajl", :yajl],
24
- ["json", :json_gem],
25
- ["json/pure", :json_pure]
26
- ]
27
-
28
- DEFAULT_ENGINE_WARNING = 'Warning: multi_json is using default ok_json engine. Suggested action: require and load an appropriate JSON library.'
29
-
30
- # The default engine based on what you currently
31
- # have loaded and installed. First checks to see
32
- # if any engines are already loaded, then checks
33
- # to see which are installed if none are loaded.
34
- def default_engine
35
- return :yajl if defined?(::Yajl)
36
- return :json_gem if defined?(::JSON)
37
-
38
- REQUIREMENT_MAP.each do |(library, engine)|
39
- begin
40
- require library
41
- return engine
42
- rescue LoadError
43
- next
44
- end
45
- end
46
-
47
- Kernel.warn DEFAULT_ENGINE_WARNING
48
- :ok_json
49
- end
50
-
51
- # Set the JSON parser utilizing a symbol, string, or class.
52
- # Supported by default are:
53
- #
54
- # * <tt>:json_gem</tt>
55
- # * <tt>:json_pure</tt>
56
- # * <tt>:ok_json</tt>
57
- # * <tt>:yajl</tt>
58
- def engine=(new_engine)
59
- case new_engine
60
- when String, Symbol
61
- require File.join(File.dirname(__FILE__),"multi_json/engines/#{new_engine}")
62
- @engine = MultiJson::Engines.const_get("#{new_engine.to_s.split('_').map{|s| s.capitalize}.join('')}")
63
- when Class
64
- @engine = new_engine
65
- else
66
- raise "Did not recognize your engine specification. Please specify either a symbol or a class."
67
- end
68
- end
69
-
70
- # Decode a JSON string into Ruby.
71
- #
72
- # <b>Options</b>
73
- #
74
- # <tt>:symbolize_keys</tt> :: If true, will use symbols instead of strings for the keys.
75
- def decode(string, options = {})
76
- engine.decode(string, options)
77
- rescue engine::ParseError => exception
78
- raise DecodeError.new(exception.message, exception.backtrace, string)
79
- end
80
-
81
- # Encodes a Ruby object as JSON.
82
- def encode(object, options = {})
83
- engine.encode(object, options)
84
- end
85
- end
@@ -1,27 +0,0 @@
1
- module MultiJson
2
- module Engines
3
- module JsonCommon
4
-
5
- def decode(string, options = {})
6
- opts = {}
7
- opts[:symbolize_names] = options[:symbolize_keys]
8
- string = string.read if string.respond_to?(:read)
9
- ::JSON.parse(string, opts)
10
- end
11
-
12
- def encode(object, options = {})
13
- object.to_json(process_options(options))
14
- end
15
-
16
- protected
17
-
18
- def process_options(options={})
19
- return options if options.empty?
20
- opts = {}
21
- opts.merge!(JSON::PRETTY_STATE_PROTOTYPE.to_h) if options.delete(:pretty)
22
- opts.merge! options
23
- end
24
-
25
- end
26
- end
27
- end
@@ -1,12 +0,0 @@
1
- require 'json' unless defined?(::JSON)
2
- require File.join(File.dirname(__FILE__),'../engines/json_common')
3
-
4
- module MultiJson
5
- module Engines
6
- # Use the JSON gem to encode/decode.
7
- class JsonGem
8
- ParseError = ::JSON::ParserError
9
- extend JsonCommon
10
- end
11
- end
12
- end
@@ -1,12 +0,0 @@
1
- require 'json/pure' unless defined?(::JSON)
2
- require File.join(File.dirname(__FILE__),'../engines/json_common')
3
-
4
- module MultiJson
5
- module Engines
6
- # Use JSON pure to encode/decode.
7
- class JsonPure
8
- ParseError = ::JSON::ParserError
9
- extend JsonCommon
10
- end
11
- end
12
- end
@@ -1,48 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "../vendor/ok_json")
2
-
3
- module MultiJson
4
- module Engines
5
- class OkJson
6
- ParseError = ::MultiJson::OkJson::Error
7
-
8
- def self.decode(string, options = {}) #:nodoc:
9
- string = string.read if string.respond_to?(:read)
10
- result = ::MultiJson::OkJson.decode(string)
11
- options[:symbolize_keys] ? symbolize_keys(result) : result
12
- end
13
-
14
- def self.encode(object, options = {}) #:nodoc:
15
- ::MultiJson::OkJson.valenc(stringify_keys(object))
16
- end
17
-
18
- def self.symbolize_keys(object) #:nodoc:
19
- modify_keys(object) do |key|
20
- key.is_a?(String) ? key.to_sym : key
21
- end
22
- end
23
-
24
- def self.stringify_keys(object) #:nodoc:
25
- modify_keys(object) do |key|
26
- key.is_a?(Symbol) ? key.to_s : key
27
- end
28
- end
29
-
30
- def self.modify_keys(object, &modifier) #:nodoc:
31
- case object
32
- when Array
33
- object.map do |value|
34
- modify_keys(value, &modifier)
35
- end
36
- when Hash
37
- object.inject({}) do |result, (key, value)|
38
- new_key = modifier.call(key)
39
- new_value = modify_keys(value, &modifier)
40
- result.merge! new_key => new_value
41
- end
42
- else
43
- object
44
- end
45
- end
46
- end
47
- end
48
- end
@@ -1,18 +0,0 @@
1
- require 'yajl' unless defined?(Yajl)
2
-
3
- module MultiJson
4
- module Engines
5
- # Use the Yajl-Ruby library to encode/decode.
6
- class Yajl
7
- ParseError = ::Yajl::ParseError
8
-
9
- def self.decode(string, options = {}) #:nodoc:
10
- ::Yajl::Parser.new(:symbolize_keys => options[:symbolize_keys]).parse(string)
11
- end
12
-
13
- def self.encode(object, options = {}) #:nodoc:
14
- ::Yajl::Encoder.encode(object, options)
15
- end
16
- end
17
- end
18
- end
@@ -1,587 +0,0 @@
1
- # Copyright 2011 Keith Rarick
2
- #
3
- # Permission is hereby granted, free of charge, to any person obtaining a copy
4
- # of this software and associated documentation files (the "Software"), to deal
5
- # in the Software without restriction, including without limitation the rights
6
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
- # copies of the Software, and to permit persons to whom the Software is
8
- # furnished to do so, subject to the following conditions:
9
- #
10
- # The above copyright notice and this permission notice shall be included in
11
- # all copies or substantial portions of the Software.
12
- #
13
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
- # THE SOFTWARE.
20
-
21
- # See https://github.com/kr/okjson for updates.
22
-
23
- require 'stringio'
24
-
25
- # Some parts adapted from
26
- # http://golang.org/src/pkg/json/decode.go and
27
- # http://golang.org/src/pkg/utf8/utf8.go
28
- module MultiJson
29
- module OkJson
30
- extend self
31
-
32
-
33
- # Decodes a json document in string s and
34
- # returns the corresponding ruby value.
35
- # String s must be valid UTF-8. If you have
36
- # a string in some other encoding, convert
37
- # it first.
38
- #
39
- # String values in the resulting structure
40
- # will be UTF-8.
41
- def decode(s)
42
- ts = lex(s)
43
- v, ts = textparse(ts)
44
- if ts.length > 0
45
- raise Error, 'trailing garbage'
46
- end
47
- v
48
- end
49
-
50
-
51
- # Parses a "json text" in the sense of RFC 4627.
52
- # Returns the parsed value and any trailing tokens.
53
- # Note: this is almost the same as valparse,
54
- # except that it does not accept atomic values.
55
- def textparse(ts)
56
- if ts.length < 0
57
- raise Error, 'empty'
58
- end
59
-
60
- typ, _, val = ts[0]
61
- case typ
62
- when '{' then objparse(ts)
63
- when '[' then arrparse(ts)
64
- else
65
- raise Error, "unexpected #{val.inspect}"
66
- end
67
- end
68
-
69
-
70
- # Parses a "value" in the sense of RFC 4627.
71
- # Returns the parsed value and any trailing tokens.
72
- def valparse(ts)
73
- if ts.length < 0
74
- raise Error, 'empty'
75
- end
76
-
77
- typ, _, val = ts[0]
78
- case typ
79
- when '{' then objparse(ts)
80
- when '[' then arrparse(ts)
81
- when :val,:str then [val, ts[1..-1]]
82
- else
83
- raise Error, "unexpected #{val.inspect}"
84
- end
85
- end
86
-
87
-
88
- # Parses an "object" in the sense of RFC 4627.
89
- # Returns the parsed value and any trailing tokens.
90
- def objparse(ts)
91
- ts = eat('{', ts)
92
- obj = {}
93
-
94
- if ts[0][0] == '}'
95
- return obj, ts[1..-1]
96
- end
97
-
98
- k, v, ts = pairparse(ts)
99
- obj[k] = v
100
-
101
- if ts[0][0] == '}'
102
- return obj, ts[1..-1]
103
- end
104
-
105
- loop do
106
- ts = eat(',', ts)
107
-
108
- k, v, ts = pairparse(ts)
109
- obj[k] = v
110
-
111
- if ts[0][0] == '}'
112
- return obj, ts[1..-1]
113
- end
114
- end
115
- end
116
-
117
-
118
- # Parses a "member" in the sense of RFC 4627.
119
- # Returns the parsed values and any trailing tokens.
120
- def pairparse(ts)
121
- (typ, _, k), ts = ts[0], ts[1..-1]
122
- if typ != :str
123
- raise Error, "unexpected #{k.inspect}"
124
- end
125
- ts = eat(':', ts)
126
- v, ts = valparse(ts)
127
- [k, v, ts]
128
- end
129
-
130
-
131
- # Parses an "array" in the sense of RFC 4627.
132
- # Returns the parsed value and any trailing tokens.
133
- def arrparse(ts)
134
- ts = eat('[', ts)
135
- arr = []
136
-
137
- if ts[0][0] == ']'
138
- return arr, ts[1..-1]
139
- end
140
-
141
- v, ts = valparse(ts)
142
- arr << v
143
-
144
- if ts[0][0] == ']'
145
- return arr, ts[1..-1]
146
- end
147
-
148
- loop do
149
- ts = eat(',', ts)
150
-
151
- v, ts = valparse(ts)
152
- arr << v
153
-
154
- if ts[0][0] == ']'
155
- return arr, ts[1..-1]
156
- end
157
- end
158
- end
159
-
160
-
161
- def eat(typ, ts)
162
- if ts[0][0] != typ
163
- raise Error, "expected #{typ} (got #{ts[0].inspect})"
164
- end
165
- ts[1..-1]
166
- end
167
-
168
-
169
- # Sans s and returns a list of json tokens,
170
- # excluding white space (as defined in RFC 4627).
171
- def lex(s)
172
- ts = []
173
- while s.length > 0
174
- typ, lexeme, val = tok(s)
175
- if typ == nil
176
- raise Error, "invalid character at #{s[0,10].inspect}"
177
- end
178
- if typ != :space
179
- ts << [typ, lexeme, val]
180
- end
181
- s = s[lexeme.length..-1]
182
- end
183
- ts
184
- end
185
-
186
-
187
- # Scans the first token in s and
188
- # returns a 3-element list, or nil
189
- # if no such token exists.
190
- #
191
- # The first list element is one of
192
- # '{', '}', ':', ',', '[', ']',
193
- # :val, :str, and :space.
194
- #
195
- # The second element is the lexeme.
196
- #
197
- # The third element is the value of the
198
- # token for :val and :str, otherwise
199
- # it is the lexeme.
200
- def tok(s)
201
- case s[0]
202
- when ?{ then ['{', s[0,1], s[0,1]]
203
- when ?} then ['}', s[0,1], s[0,1]]
204
- when ?: then [':', s[0,1], s[0,1]]
205
- when ?, then [',', s[0,1], s[0,1]]
206
- when ?[ then ['[', s[0,1], s[0,1]]
207
- when ?] then [']', s[0,1], s[0,1]]
208
- when ?n then nulltok(s)
209
- when ?t then truetok(s)
210
- when ?f then falsetok(s)
211
- when ?" then strtok(s)
212
- when Spc then [:space, s[0,1], s[0,1]]
213
- when ?\t then [:space, s[0,1], s[0,1]]
214
- when ?\n then [:space, s[0,1], s[0,1]]
215
- when ?\r then [:space, s[0,1], s[0,1]]
216
- else numtok(s)
217
- end
218
- end
219
-
220
-
221
- def nulltok(s); s[0,4] == 'null' && [:val, 'null', nil] end
222
- def truetok(s); s[0,4] == 'true' && [:val, 'true', true] end
223
- def falsetok(s); s[0,5] == 'false' && [:val, 'false', false] end
224
-
225
-
226
- def numtok(s)
227
- m = /-?([1-9][0-9]+|[0-9])([.][0-9]+)?([eE][+-]?[0-9]+)?/.match(s)
228
- if m && m.begin(0) == 0
229
- if m[3] && !m[2]
230
- [:val, m[0], Integer(m[1])*(10**Integer(m[3][1..-1]))]
231
- elsif m[2]
232
- [:val, m[0], Float(m[0])]
233
- else
234
- [:val, m[0], Integer(m[0])]
235
- end
236
- end
237
- end
238
-
239
-
240
- def strtok(s)
241
- m = /"([^"\\]|\\["\/\\bfnrt]|\\u[0-9a-fA-F]{4})*"/.match(s)
242
- if ! m
243
- raise Error, "invalid string literal at #{abbrev(s)}"
244
- end
245
- [:str, m[0], unquote(m[0])]
246
- end
247
-
248
-
249
- def abbrev(s)
250
- t = s[0,10]
251
- p = t['`']
252
- t = t[0,p] if p
253
- t = t + '...' if t.length < s.length
254
- '`' + t + '`'
255
- end
256
-
257
-
258
- # Converts a quoted json string literal q into a UTF-8-encoded string.
259
- # The rules are different than for Ruby, so we cannot use eval.
260
- # Unquote will raise an error if q contains control characters.
261
- def unquote(q)
262
- q = q[1...-1]
263
- a = q.dup # allocate a big enough string
264
- r, w = 0, 0
265
- while r < q.length
266
- c = q[r]
267
- case true
268
- when c == ?\\
269
- r += 1
270
- if r >= q.length
271
- raise Error, "string literal ends with a \"\\\": \"#{q}\""
272
- end
273
-
274
- case q[r]
275
- when ?",?\\,?/,?'
276
- a[w] = q[r]
277
- r += 1
278
- w += 1
279
- when ?b,?f,?n,?r,?t
280
- a[w] = Unesc[q[r]]
281
- r += 1
282
- w += 1
283
- when ?u
284
- r += 1
285
- uchar = begin
286
- hexdec4(q[r,4])
287
- rescue RuntimeError => e
288
- raise Error, "invalid escape sequence \\u#{q[r,4]}: #{e}"
289
- end
290
- r += 4
291
- if surrogate? uchar
292
- if q.length >= r+6
293
- uchar1 = hexdec4(q[r+2,4])
294
- uchar = subst(uchar, uchar1)
295
- if uchar != Ucharerr
296
- # A valid pair; consume.
297
- r += 6
298
- end
299
- end
300
- end
301
- w += ucharenc(a, w, uchar)
302
- else
303
- raise Error, "invalid escape char #{q[r]} in \"#{q}\""
304
- end
305
- when c == ?", c < Spc
306
- raise Error, "invalid character in string literal \"#{q}\""
307
- else
308
- # Copy anything else byte-for-byte.
309
- # Valid UTF-8 will remain valid UTF-8.
310
- # Invalid UTF-8 will remain invalid UTF-8.
311
- a[w] = c
312
- r += 1
313
- w += 1
314
- end
315
- end
316
- a[0,w]
317
- end
318
-
319
-
320
- # Encodes unicode character u as UTF-8
321
- # bytes in string a at position i.
322
- # Returns the number of bytes written.
323
- def ucharenc(a, i, u)
324
- case true
325
- when u <= Uchar1max
326
- a[i] = (u & 0xff).chr
327
- 1
328
- when u <= Uchar2max
329
- a[i+0] = (Utag2 | ((u>>6)&0xff)).chr
330
- a[i+1] = (Utagx | (u&Umaskx)).chr
331
- 2
332
- when u <= Uchar3max
333
- a[i+0] = (Utag3 | ((u>>12)&0xff)).chr
334
- a[i+1] = (Utagx | ((u>>6)&Umaskx)).chr
335
- a[i+2] = (Utagx | (u&Umaskx)).chr
336
- 3
337
- else
338
- a[i+0] = (Utag4 | ((u>>18)&0xff)).chr
339
- a[i+1] = (Utagx | ((u>>12)&Umaskx)).chr
340
- a[i+2] = (Utagx | ((u>>6)&Umaskx)).chr
341
- a[i+3] = (Utagx | (u&Umaskx)).chr
342
- 4
343
- end
344
- end
345
-
346
-
347
- def hexdec4(s)
348
- if s.length != 4
349
- raise Error, 'short'
350
- end
351
- (nibble(s[0])<<12) | (nibble(s[1])<<8) | (nibble(s[2])<<4) | nibble(s[3])
352
- end
353
-
354
-
355
- def subst(u1, u2)
356
- if Usurr1 <= u1 && u1 < Usurr2 && Usurr2 <= u2 && u2 < Usurr3
357
- return ((u1-Usurr1)<<10) | (u2-Usurr2) + Usurrself
358
- end
359
- return Ucharerr
360
- end
361
-
362
-
363
- def unsubst(u)
364
- if u < Usurrself || u > Umax || surrogate?(u)
365
- return Ucharerr, Ucharerr
366
- end
367
- u -= Usurrself
368
- [Usurr1 + ((u>>10)&0x3ff), Usurr2 + (u&0x3ff)]
369
- end
370
-
371
-
372
- def surrogate?(u)
373
- Usurr1 <= u && u < Usurr3
374
- end
375
-
376
-
377
- def nibble(c)
378
- case true
379
- when ?0 <= c && c <= ?9 then c.ord - ?0.ord
380
- when ?a <= c && c <= ?z then c.ord - ?a.ord + 10
381
- when ?A <= c && c <= ?Z then c.ord - ?A.ord + 10
382
- else
383
- raise Error, "invalid hex code #{c}"
384
- end
385
- end
386
-
387
-
388
- # Encodes x into a json text. It may contain only
389
- # Array, Hash, String, Numeric, true, false, nil.
390
- # (Note, this list excludes Symbol.)
391
- # X itself must be an Array or a Hash.
392
- # No other value can be encoded, and an error will
393
- # be raised if x contains any other value, such as
394
- # Nan, Infinity, Symbol, and Proc, or if a Hash key
395
- # is not a String.
396
- # Strings contained in x must be valid UTF-8.
397
- def encode(x)
398
- case x
399
- when Hash then objenc(x)
400
- when Array then arrenc(x)
401
- else
402
- raise Error, 'root value must be an Array or a Hash'
403
- end
404
- end
405
-
406
-
407
- def valenc(x)
408
- case x
409
- when Hash then objenc(x)
410
- when Array then arrenc(x)
411
- when String then strenc(x)
412
- when Numeric then numenc(x)
413
- when true then "true"
414
- when false then "false"
415
- when nil then "null"
416
- else
417
- if x.respond_to?(:to_json)
418
- x.to_json
419
- else
420
- raise Error, "cannot encode #{x.class}: #{x.inspect}"
421
- end
422
- end
423
- end
424
-
425
-
426
- def objenc(x)
427
- '{' + x.map{|k,v| keyenc(k) + ':' + valenc(v)}.join(',') + '}'
428
- end
429
-
430
-
431
- def arrenc(a)
432
- '[' + a.map{|x| valenc(x)}.join(',') + ']'
433
- end
434
-
435
-
436
- def keyenc(k)
437
- case k
438
- when String then strenc(k)
439
- else
440
- raise Error, "Hash key is not a string: #{k.inspect}"
441
- end
442
- end
443
-
444
-
445
- def strenc(s)
446
- t = StringIO.new
447
- t.putc(?")
448
- r = 0
449
- while r < s.length
450
- case s[r]
451
- when ?" then t.print('\\"')
452
- when ?\\ then t.print('\\\\')
453
- when ?\b then t.print('\\b')
454
- when ?\f then t.print('\\f')
455
- when ?\n then t.print('\\n')
456
- when ?\r then t.print('\\r')
457
- when ?\t then t.print('\\t')
458
- else
459
- c = s[r]
460
- case true
461
- when Spc <= c && c <= ?~
462
- t.putc(c)
463
- when true
464
- u, size = uchardec(s, r)
465
- r += size - 1 # we add one more at the bottom of the loop
466
- if u < 0x10000
467
- t.print('\\u')
468
- hexenc4(t, u)
469
- else
470
- u1, u2 = unsubst(u)
471
- t.print('\\u')
472
- hexenc4(t, u1)
473
- t.print('\\u')
474
- hexenc4(t, u2)
475
- end
476
- else
477
- # invalid byte; skip it
478
- end
479
- end
480
- r += 1
481
- end
482
- t.putc(?")
483
- t.string
484
- end
485
-
486
-
487
- def hexenc4(t, u)
488
- t.putc(Hex[(u>>12)&0xf])
489
- t.putc(Hex[(u>>8)&0xf])
490
- t.putc(Hex[(u>>4)&0xf])
491
- t.putc(Hex[u&0xf])
492
- end
493
-
494
-
495
- def numenc(x)
496
- if x.nan? || x.infinite?
497
- return 'null'
498
- end rescue nil
499
- "#{x}"
500
- end
501
-
502
-
503
- # Decodes unicode character u from UTF-8
504
- # bytes in string s at position i.
505
- # Returns u and the number of bytes read.
506
- def uchardec(s, i)
507
- n = s.length - i
508
- return [Ucharerr, 1] if n < 1
509
-
510
- c0 = s[i].ord
511
-
512
- # 1-byte, 7-bit sequence?
513
- if c0 < Utagx
514
- return [c0, 1]
515
- end
516
-
517
- # unexpected continuation byte?
518
- return [Ucharerr, 1] if c0 < Utag2
519
-
520
- # need continuation byte
521
- return [Ucharerr, 1] if n < 2
522
- c1 = s[i+1].ord
523
- return [Ucharerr, 1] if c1 < Utagx || Utag2 <= c1
524
-
525
- # 2-byte, 11-bit sequence?
526
- if c0 < Utag3
527
- u = (c0&Umask2)<<6 | (c1&Umaskx)
528
- return [Ucharerr, 1] if u <= Uchar1max
529
- return [u, 2]
530
- end
531
-
532
- # need second continuation byte
533
- return [Ucharerr, 1] if n < 3
534
- c2 = s[i+2].ord
535
- return [Ucharerr, 1] if c2 < Utagx || Utag2 <= c2
536
-
537
- # 3-byte, 16-bit sequence?
538
- if c0 < Utag4
539
- u = (c0&Umask3)<<12 | (c1&Umaskx)<<6 | (c2&Umaskx)
540
- return [Ucharerr, 1] if u <= Uchar2max
541
- return [u, 3]
542
- end
543
-
544
- # need third continuation byte
545
- return [Ucharerr, 1] if n < 4
546
- c3 = s[i+3].ord
547
- return [Ucharerr, 1] if c3 < Utagx || Utag2 <= c3
548
-
549
- # 4-byte, 21-bit sequence?
550
- if c0 < Utag5
551
- u = (c0&Umask4)<<18 | (c1&Umaskx)<<12 | (c2&Umaskx)<<6 | (c3&Umaskx)
552
- return [Ucharerr, 1] if u <= Uchar3max
553
- return [u, 4]
554
- end
555
-
556
- return [Ucharerr, 1]
557
- end
558
-
559
-
560
- class Error < ::StandardError
561
- end
562
-
563
-
564
- Utagx = 0x80 # 1000 0000
565
- Utag2 = 0xc0 # 1100 0000
566
- Utag3 = 0xe0 # 1110 0000
567
- Utag4 = 0xf0 # 1111 0000
568
- Utag5 = 0xF8 # 1111 1000
569
- Umaskx = 0x3f # 0011 1111
570
- Umask2 = 0x1f # 0001 1111
571
- Umask3 = 0x0f # 0000 1111
572
- Umask4 = 0x07 # 0000 0111
573
- Uchar1max = (1<<7) - 1
574
- Uchar2max = (1<<11) - 1
575
- Uchar3max = (1<<16) - 1
576
- Ucharerr = 0xFFFD # unicode "replacement char"
577
- Usurrself = 0x10000
578
- Usurr1 = 0xd800
579
- Usurr2 = 0xdc00
580
- Usurr3 = 0xe000
581
- Umax = 0x10ffff
582
-
583
- Spc = ' '[0]
584
- Unesc = {?b=>?\b, ?f=>?\f, ?n=>?\n, ?r=>?\r, ?t=>?\t}
585
- Hex = '0123456789abcdef'
586
- end
587
- end
@@ -1,3 +0,0 @@
1
- module MultiJson
2
- VERSION = "1.0.4"
3
- end