ruby-json 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING +504 -0
- data/README +16 -0
- data/doc/classes/Array.html +162 -0
- data/doc/classes/Array.src/M000011.html +27 -0
- data/doc/classes/Array.src/M000012.html +32 -0
- data/doc/classes/ArrayTest.html +129 -0
- data/doc/classes/ArrayTest.src/M000014.html +28 -0
- data/doc/classes/FalseClass.html +131 -0
- data/doc/classes/FalseClass.src/M000013.html +18 -0
- data/doc/classes/Hash.html +156 -0
- data/doc/classes/Hash.src/M000009.html +29 -0
- data/doc/classes/Hash.src/M000010.html +45 -0
- data/doc/classes/JSON.html +103 -0
- data/doc/classes/JSON/Lexer.html +411 -0
- data/doc/classes/JSON/Lexer.src/M000022.html +19 -0
- data/doc/classes/JSON/Lexer.src/M000023.html +18 -0
- data/doc/classes/JSON/Lexer.src/M000024.html +18 -0
- data/doc/classes/JSON/Lexer.src/M000025.html +20 -0
- data/doc/classes/JSON/Lexer.src/M000026.html +20 -0
- data/doc/classes/JSON/Lexer.src/M000027.html +21 -0
- data/doc/classes/JSON/Lexer.src/M000028.html +43 -0
- data/doc/classes/JSON/Lexer.src/M000029.html +67 -0
- data/doc/classes/JSON/Lexer.src/M000030.html +46 -0
- data/doc/classes/JSON/Lexer.src/M000031.html +26 -0
- data/doc/classes/JSON/Lexer.src/M000032.html +61 -0
- data/doc/classes/JSON/Lexer.src/M000033.html +29 -0
- data/doc/classes/JSON/Lexer.src/M000034.html +18 -0
- data/doc/classes/JSON/Lexer.src/M000035.html +19 -0
- data/doc/classes/JSON/Lexer.src/M000036.html +20 -0
- data/doc/classes/LexerTest.html +219 -0
- data/doc/classes/LexerTest.src/M000002.html +24 -0
- data/doc/classes/LexerTest.src/M000003.html +22 -0
- data/doc/classes/LexerTest.src/M000004.html +26 -0
- data/doc/classes/LexerTest.src/M000005.html +23 -0
- data/doc/classes/LexerTest.src/M000006.html +31 -0
- data/doc/classes/LexerTest.src/M000007.html +27 -0
- data/doc/classes/LexerTest.src/M000008.html +58 -0
- data/doc/classes/NilClass.html +131 -0
- data/doc/classes/NilClass.src/M000017.html +18 -0
- data/doc/classes/Numeric.html +131 -0
- data/doc/classes/Numeric.src/M000016.html +18 -0
- data/doc/classes/Object.html +176 -0
- data/doc/classes/Object.src/M000018.html +18 -0
- data/doc/classes/ObjectTest.html +129 -0
- data/doc/classes/ObjectTest.src/M000015.html +28 -0
- data/doc/classes/String.html +151 -0
- data/doc/classes/String.src/M000019.html +18 -0
- data/doc/classes/String.src/M000020.html +46 -0
- data/doc/classes/TrueClass.html +131 -0
- data/doc/classes/TrueClass.src/M000021.html +18 -0
- data/doc/created.rid +1 -0
- data/doc/files/install_rb.html +194 -0
- data/doc/files/install_rb.src/M000001.html +43 -0
- data/doc/files/json/lexer_rb.html +156 -0
- data/doc/files/json/objects_rb.html +154 -0
- data/doc/files/test/arraytest_rb.html +150 -0
- data/doc/files/test/lexertest_rb.html +150 -0
- data/doc/files/test/objtest_rb.html +150 -0
- data/doc/fr_class_index.html +39 -0
- data/doc/fr_file_index.html +32 -0
- data/doc/fr_method_index.html +62 -0
- data/doc/index.html +24 -0
- data/doc/rdoc-style.css +175 -0
- data/install.rb +67 -0
- data/lib/json/lexer.rb +297 -0
- data/lib/json/objects.rb +201 -0
- data/ruby-json.gemspec +25 -0
- data/test/arraytest.rb +45 -0
- data/test/lexertest.rb +146 -0
- data/test/objtest.rb +45 -0
- metadata +126 -0
data/lib/json/objects.rb
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
#
|
2
|
+
# JSON Objects
|
3
|
+
# Copyright (C) 2003,2005 Rafael R. Sevilla <dido@imperium.ph>
|
4
|
+
# This file is part of JSON for Ruby
|
5
|
+
#
|
6
|
+
# JSON for Ruby is free software; you can redistribute it and/or
|
7
|
+
# modify it under the terms of the GNU Lesser General Public License
|
8
|
+
# as published by the Free Software Foundation; either version 2.1 of
|
9
|
+
# the License, or (at your option) any later version.
|
10
|
+
#
|
11
|
+
# JSON for Ruby is distributed in the hope that it will be useful,
|
12
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14
|
+
# Lesser General Public License for more details.
|
15
|
+
#
|
16
|
+
# You should have received a copy of the GNU Lesser General Public
|
17
|
+
# License along with JSON for Ruby; if not, write to the Free
|
18
|
+
# Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
19
|
+
# 02111-1307 USA.
|
20
|
+
#
|
21
|
+
# Author:: Rafael R. Sevilla (mailto:dido@imperium.ph)
|
22
|
+
# Heavily modified by Adam Kramer (mailto:adam@the-kramers.net)
|
23
|
+
# Copyright:: Copyright (c) 2003,2005 Rafael R. Sevilla
|
24
|
+
# License:: GNU Lesser General Public License
|
25
|
+
# $Id: objects.rb,v 1.1 2005/01/28 02:08:45 didosevilla Exp $
|
26
|
+
#
|
27
|
+
|
28
|
+
|
29
|
+
class Object
|
30
|
+
def to_json #all strings in json need to have double quotes around them, treat random objects as strings
|
31
|
+
String.to_json(to_s)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
class NilClass
|
37
|
+
def to_json
|
38
|
+
"null"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
class TrueClass
|
42
|
+
def to_json
|
43
|
+
"true"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
class FalseClass
|
48
|
+
def to_json
|
49
|
+
"false"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
class Numeric
|
54
|
+
def to_json
|
55
|
+
to_s
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
class String
|
60
|
+
# produce a string in double quotes with all the necessary quoting
|
61
|
+
# done
|
62
|
+
def to_json
|
63
|
+
return String.to_json(self)
|
64
|
+
end
|
65
|
+
|
66
|
+
def self.to_json(str)
|
67
|
+
return "\"\"" if (str.length == 0)
|
68
|
+
newstr = "\""
|
69
|
+
str.each_byte {
|
70
|
+
|b|
|
71
|
+
c = b.chr
|
72
|
+
case c
|
73
|
+
when /\\|\">/
|
74
|
+
newstr << "\\" + c
|
75
|
+
when "\b"
|
76
|
+
newstr << "\\b"
|
77
|
+
when "\t"
|
78
|
+
newstr << "\\t"
|
79
|
+
when "\n"
|
80
|
+
newstr << "\\n"
|
81
|
+
when "\f"
|
82
|
+
newstr << "\\f"
|
83
|
+
when "\r"
|
84
|
+
newstr << "\\r"
|
85
|
+
else
|
86
|
+
if (c < ' ')
|
87
|
+
t = "000" + sprintf("%0x", b)
|
88
|
+
newstr << ("\\u" + t[0,t.length - 4])
|
89
|
+
else
|
90
|
+
newstr << c
|
91
|
+
end
|
92
|
+
end
|
93
|
+
}
|
94
|
+
newstr += '"'
|
95
|
+
return(newstr)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
class Array
|
100
|
+
|
101
|
+
# This method will return a string giving the contents of the JSON
|
102
|
+
# array in standard JSON format.
|
103
|
+
def to_json
|
104
|
+
retval = '['
|
105
|
+
|
106
|
+
first=true
|
107
|
+
self.each { |obj|
|
108
|
+
retval << ',' unless first
|
109
|
+
retval << obj.to_json
|
110
|
+
first=false
|
111
|
+
}
|
112
|
+
retval << "]"
|
113
|
+
return(retval)
|
114
|
+
end
|
115
|
+
|
116
|
+
# This method will parse a JSON array from the passed lexer
|
117
|
+
# object. It takes a lexer object which is about to read a JSON
|
118
|
+
# array. It raises a runtime error otherwise. It returns the
|
119
|
+
# original JSON array. This method is not intended to be used
|
120
|
+
# directly.
|
121
|
+
# =====Parameters
|
122
|
+
# +lexer+:: Lexer object to use
|
123
|
+
def from_json(lexer)
|
124
|
+
raise "A JSON Array must begin with '['" if (lexer.nextclean != "[")
|
125
|
+
return if lexer.nextclean == ']'
|
126
|
+
lexer.back
|
127
|
+
loop {
|
128
|
+
self << lexer.nextvalue
|
129
|
+
case lexer.nextclean
|
130
|
+
when ','
|
131
|
+
return if (lexer.nextclean == ']')
|
132
|
+
lexer.back
|
133
|
+
when ']'
|
134
|
+
return(self)
|
135
|
+
else
|
136
|
+
raise "Expected a ',' or ']'"
|
137
|
+
end
|
138
|
+
}
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
class Hash
|
146
|
+
|
147
|
+
# This method will serialize the hash into regular JSON format.
|
148
|
+
def to_json
|
149
|
+
retval = "{"
|
150
|
+
|
151
|
+
first = true
|
152
|
+
self.each {|key, val|
|
153
|
+
retval << "," unless first
|
154
|
+
key = key.to_s #keys in json hashes need to be strings, nothing else.
|
155
|
+
retval << key.to_json + ":"
|
156
|
+
retval << val.to_json
|
157
|
+
first = false
|
158
|
+
}
|
159
|
+
retval << "}"
|
160
|
+
return(retval)
|
161
|
+
end
|
162
|
+
|
163
|
+
# This method will parse a JSON object from the passed lexer
|
164
|
+
# object. It takes a lexer object which is about to read a JSON
|
165
|
+
# object. It raises a runtime error otherwise. It returns the
|
166
|
+
# original JSON object. This method probably shouldn't be used
|
167
|
+
# directly.
|
168
|
+
# =====Parameters
|
169
|
+
def from_json(lexer)
|
170
|
+
lexer.unescape if (lexer.nextclean == '%')
|
171
|
+
lexer.back
|
172
|
+
raise "A JSON Object must begin with '{'" if (lexer.nextclean != "{")
|
173
|
+
loop {
|
174
|
+
c = lexer.nextclean
|
175
|
+
key = nil
|
176
|
+
case c
|
177
|
+
when '\0'
|
178
|
+
raise "A JSON Object must end with '}'"
|
179
|
+
when '}'
|
180
|
+
return;
|
181
|
+
else
|
182
|
+
lexer.back
|
183
|
+
key = lexer.nextvalue().to_s()
|
184
|
+
end
|
185
|
+
raise "Expected a ':' after a key" if (lexer.nextclean() != ':')
|
186
|
+
self[key] = lexer.nextvalue()
|
187
|
+
case lexer.nextclean()
|
188
|
+
when ','
|
189
|
+
return if (lexer.nextclean() == '}')
|
190
|
+
lexer.back
|
191
|
+
when '}'
|
192
|
+
return(self)
|
193
|
+
else
|
194
|
+
raise "Expected a ',' or '}'"
|
195
|
+
end
|
196
|
+
}
|
197
|
+
return(self)
|
198
|
+
end
|
199
|
+
|
200
|
+
end
|
201
|
+
|
data/ruby-json.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = %q{ruby-json}
|
3
|
+
s.version = "1.1.1"
|
4
|
+
s.date = %q{2005-01-28}
|
5
|
+
s.summary = %q{ruby-json is a library for using the JavaScript Object Notation (JSON) under Ruby.}
|
6
|
+
s.require_paths = ["json"]
|
7
|
+
s.email = %q{dido@imperium.ph}
|
8
|
+
s.homepage = %q{http://sourceforge.net/projects/json}
|
9
|
+
s.description = %q{ruby-json is a library able to serialize and deserialize Ruby objects to and from their JSON representation.}
|
10
|
+
|
11
|
+
s.require_path = 'lib'
|
12
|
+
s.autorequire = %q{json/lexer}
|
13
|
+
s.has_rdoc = true
|
14
|
+
s.files = ["doc", "lib/json", "test",
|
15
|
+
"README", "ruby-json.gemspec", "install.rb", "COPYING",
|
16
|
+
"doc/files", "doc/index.html", "doc/rdoc-style.css",
|
17
|
+
"doc/fr_method_index.html", "doc/fr_class_index.html",
|
18
|
+
"doc/fr_file_index.html", "doc/created.rid", "doc/classes", "doc/files/json",
|
19
|
+
"doc/files/test", "doc/files/install_rb.html", "doc/files/install_rb.src", "doc/files/json/lexer_rb.html", "doc/files/json/objects_rb.html", "doc/files/test/lexertest_rb.html", "doc/files/test/objtest_rb.html", "doc/files/test/arraytest_rb.html", "doc/files/install_rb.src/M000001.html", "doc/classes/JSON", "doc/classes/Hash.html", "doc/classes/ObjectTest.html", "doc/classes/JSON.html", "doc/classes/String.html", "doc/classes/Array.src", "doc/classes/Numeric.src", "doc/classes/Hash.src", "doc/classes/NilClass.html", "doc/classes/LexerTest.src", "doc/classes/String.src", "doc/classes/Object.src", "doc/classes/Object.html", "doc/classes/FalseClass.src", "doc/classes/Array.html", "doc/classes/ObjectTest.src", "doc/classes/TrueClass.src", "doc/classes/TrueClass.html", "doc/classes/FalseClass.html", "doc/classes/LexerTest.html", "doc/classes/NilClass.src", "doc/classes/ArrayTest.html", "doc/classes/Numeric.html", "doc/classes/ArrayTest.src", "doc/classes/JSON/Lexer.src", "doc/classes/JSON/Lexer.html", "doc/classes/JSON/Lexer.src/M000022.html", "doc/classes/JSON/Lexer.src/M000023.html", "doc/classes/JSON/Lexer.src/M000024.html", "doc/classes/JSON/Lexer.src/M000025.html", "doc/classes/JSON/Lexer.src/M000026.html", "doc/classes/JSON/Lexer.src/M000027.html", "doc/classes/JSON/Lexer.src/M000028.html", "doc/classes/JSON/Lexer.src/M000029.html", "doc/classes/JSON/Lexer.src/M000030.html", "doc/classes/JSON/Lexer.src/M000031.html", "doc/classes/JSON/Lexer.src/M000032.html", "doc/classes/JSON/Lexer.src/M000033.html", "doc/classes/JSON/Lexer.src/M000034.html", "doc/classes/JSON/Lexer.src/M000035.html", "doc/classes/JSON/Lexer.src/M000036.html", "doc/classes/Array.src/M000011.html", "doc/classes/Array.src/M000012.html", "doc/classes/Numeric.src/M000016.html", "doc/classes/Hash.src/M000009.html", "doc/classes/Hash.src/M000010.html", "doc/classes/LexerTest.src/M000008.html", "doc/classes/LexerTest.src/M000002.html", "doc/classes/LexerTest.src/M000003.html", "doc/classes/LexerTest.src/M000004.html", "doc/classes/LexerTest.src/M000005.html", "doc/classes/LexerTest.src/M000006.html", "doc/classes/LexerTest.src/M000007.html", "doc/classes/String.src/M000019.html", "doc/classes/String.src/M000020.html", "doc/classes/Object.src/M000018.html", "doc/classes/FalseClass.src/M000013.html", "doc/classes/ObjectTest.src/M000015.html", "doc/classes/TrueClass.src/M000021.html", "doc/classes/NilClass.src/M000017.html", "doc/classes/ArrayTest.src/M000014.html",
|
20
|
+
"lib/json/objects.rb",
|
21
|
+
"lib/json/lexer.rb",
|
22
|
+
"test/lexertest.rb",
|
23
|
+
"test/objtest.rb",
|
24
|
+
"test/arraytest.rb"]
|
25
|
+
end
|
data/test/arraytest.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
#
|
2
|
+
# Unit Tests for JSON Arrays
|
3
|
+
# Copyright (C) 2002 Rafael R. Sevilla <dido@imperium.ph>
|
4
|
+
# This file is part of JSON for Ruby
|
5
|
+
#
|
6
|
+
# JSON for Ruby is free software; you can redistribute it and/or
|
7
|
+
# modify it under the terms of the GNU Lesser General Public License
|
8
|
+
# as published by the Free Software Foundation; either version 2.1 of
|
9
|
+
# the License, or (at your option) any later version.
|
10
|
+
#
|
11
|
+
# JSON for Ruby is distributed in the hope that it will be useful,
|
12
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14
|
+
# Lesser General Public License for more details.
|
15
|
+
#
|
16
|
+
# You should have received a copy of the GNU Lesser General Public
|
17
|
+
# License along with JSON for Ruby; if not, write to the Free
|
18
|
+
# Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
19
|
+
# 02111-1307 USA.
|
20
|
+
#
|
21
|
+
# Author:: Rafael R. Sevilla (mailto:dido@imperium.ph)
|
22
|
+
# Copyright:: Copyright (c) 2003 Rafael R. Sevilla
|
23
|
+
# License:: GNU Lesser General Public License
|
24
|
+
# $Id: arraytest.rb,v 1.2 2005/01/28 02:59:02 didosevilla Exp $
|
25
|
+
#
|
26
|
+
|
27
|
+
require 'test/unit'
|
28
|
+
require 'json/lexer'
|
29
|
+
require 'json/objects'
|
30
|
+
|
31
|
+
class ArrayTest < Test::Unit::TestCase
|
32
|
+
def test_array_to_json
|
33
|
+
array = Array.new
|
34
|
+
array[0] = 'bar'
|
35
|
+
array[1] = 1
|
36
|
+
array[2] = 2.7182818
|
37
|
+
|
38
|
+
str = array.to_json
|
39
|
+
sarray = JSON::Lexer.new(str).nextvalue
|
40
|
+
assert(sarray.class == Array, "array was not generated by deserialization")
|
41
|
+
assert(sarray[0] == 'bar', "error in serialization of a string to JSON")
|
42
|
+
assert(sarray[1] == 1, "error in serialization of a FixNum to JSON")
|
43
|
+
assert(sarray[2] == 2.7182818, "error in serialization of a float to JSON")
|
44
|
+
end
|
45
|
+
end
|
data/test/lexertest.rb
ADDED
@@ -0,0 +1,146 @@
|
|
1
|
+
#
|
2
|
+
# Unit tests for JSON Lexer
|
3
|
+
# Copyright (C) 2003 Rafael R. Sevilla <dido@imperium.ph>
|
4
|
+
# This file is part of JSON for Ruby
|
5
|
+
#
|
6
|
+
# JSON for Ruby is free software; you can redistribute it and/or
|
7
|
+
# modify it under the terms of the GNU Lesser General Public License
|
8
|
+
# as published by the Free Software Foundation; either version 2.1 of
|
9
|
+
# the License, or (at your option) any later version.
|
10
|
+
#
|
11
|
+
# JSON for Ruby is distributed in the hope that it will be useful,
|
12
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14
|
+
# Lesser General Public License for more details.
|
15
|
+
#
|
16
|
+
# You should have received a copy of the GNU Lesser General Public
|
17
|
+
# License along with JSON for Ruby; if not, write to the Free
|
18
|
+
# Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
19
|
+
# 02111-1307 USA.
|
20
|
+
#
|
21
|
+
# Author:: Rafael R. Sevilla (mailto:dido@imperium.ph)
|
22
|
+
# Copyright:: Copyright (c) 2003 Rafael R. Sevilla
|
23
|
+
# License:: GNU Lesser General Public License
|
24
|
+
# $Id: lexertest.rb,v 1.3 2005/01/28 03:03:51 didosevilla Exp $
|
25
|
+
#
|
26
|
+
|
27
|
+
require 'test/unit'
|
28
|
+
#require 'test/unit/ui/console/testrunner'
|
29
|
+
require 'json/lexer'
|
30
|
+
require 'json/objects'
|
31
|
+
|
32
|
+
class LexerTest < Test::Unit::TestCase
|
33
|
+
def test_nextchar_back
|
34
|
+
str = "some_string"
|
35
|
+
lex = JSON::Lexer.new(str)
|
36
|
+
c = lex.nextchar
|
37
|
+
assert(c == 's', "wrong character read")
|
38
|
+
lex.back
|
39
|
+
c = lex.nextchar
|
40
|
+
assert(c == 's', "backing up produces inconsistent results")
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_ending
|
44
|
+
lex = JSON::Lexer.new("ab")
|
45
|
+
lex.nextchar
|
46
|
+
assert(lex.more?, "more? method produces wrong results (no more when there should be more)")
|
47
|
+
lex.nextchar
|
48
|
+
assert(!lex.more?, "more? method produces wrong results (more when there are no more)")
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_nextmatch
|
52
|
+
lex = JSON::Lexer.new("abc")
|
53
|
+
c = ""
|
54
|
+
assert_nothing_raised {
|
55
|
+
c = lex.nextmatch('a')
|
56
|
+
}
|
57
|
+
assert(c == 'a', "nextmatch is wrong")
|
58
|
+
assert_raises(RuntimeError, "exception not raised for not found") {
|
59
|
+
c = lex.nextmatch('a')
|
60
|
+
}
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_nextchars
|
64
|
+
lex = JSON::Lexer.new("some_string")
|
65
|
+
str = lex.nextchars(4)
|
66
|
+
assert(str == 'some', "nextchars doesn't work correctly")
|
67
|
+
assert_raises(RuntimeError, "exception not raised for substring bounds error") {
|
68
|
+
lex.nextchars(10)
|
69
|
+
}
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_nextclean
|
73
|
+
str1 = "/\t// comment\nb"
|
74
|
+
lex = JSON::Lexer.new(str1)
|
75
|
+
assert(lex.nextclean == '/', "nextclean seems to have problems")
|
76
|
+
assert(lex.nextclean == 'b', "// comment processing has problems")
|
77
|
+
str2 = "/ /* comment */b"
|
78
|
+
lex = JSON::Lexer.new(str2)
|
79
|
+
assert(lex.nextclean == '/', "nextclean seems to have problems")
|
80
|
+
assert(lex.nextclean == 'b', "/* .. */ comment processing has problems")
|
81
|
+
# test unclosed comments
|
82
|
+
lex = JSON::Lexer.new("a/* unclosed comment")
|
83
|
+
assert(lex.nextclean == 'a', "nextclean seems to have problems")
|
84
|
+
assert_raises(RuntimeError, "unclosed comment doesn't raise exceptions") {
|
85
|
+
lex.nextclean
|
86
|
+
}
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_nextstring
|
90
|
+
str1 = "str\""
|
91
|
+
lex = JSON::Lexer.new(str1)
|
92
|
+
assert(lex.nextstring('"') == "str", "string processing has problems")
|
93
|
+
str2 = '\b\t\n\f\r"'
|
94
|
+
lex = JSON::Lexer.new(str2)
|
95
|
+
assert(lex.nextstring('"') == "\b\t\n\f\r", "escape sequence processing has bugs")
|
96
|
+
# UTF8 conversion tests for escape sequences
|
97
|
+
str3 = '\u1234"'
|
98
|
+
lex = JSON::Lexer.new(str3)
|
99
|
+
assert(lex.nextstring('"') == "\341\210\224", "Unicode escape sequence processing has bugs")
|
100
|
+
end
|
101
|
+
|
102
|
+
def test_nextvalue
|
103
|
+
str = "false"
|
104
|
+
lex = JSON::Lexer.new(str)
|
105
|
+
assert(lex.nextvalue == false, "error parsing false");
|
106
|
+
|
107
|
+
str = "true"
|
108
|
+
lex = JSON::Lexer.new(str)
|
109
|
+
assert(lex.nextvalue == true, "error parsing true");
|
110
|
+
|
111
|
+
str = "31337"
|
112
|
+
lex = JSON::Lexer.new(str)
|
113
|
+
assert(lex.nextvalue == 31337, "error parsing integer");
|
114
|
+
|
115
|
+
str = "0.577289"
|
116
|
+
lex = JSON::Lexer.new(str)
|
117
|
+
assert(lex.nextvalue == 0.577289, "error parsing float")
|
118
|
+
|
119
|
+
str = "\"123\n\""
|
120
|
+
lex = JSON::Lexer.new(str)
|
121
|
+
assert(lex.nextvalue == "123\n", "error parsing string")
|
122
|
+
|
123
|
+
str = "null"
|
124
|
+
lex = JSON::Lexer.new(str)
|
125
|
+
assert(lex.nextvalue.nil?, "error parsing null")
|
126
|
+
|
127
|
+
str = "[ 1, \"abc\", 2.7182818 ]"
|
128
|
+
lex = JSON::Lexer.new(str)
|
129
|
+
array = lex.nextvalue
|
130
|
+
assert(array[0] == 1, "error parsing an array (0th elem)")
|
131
|
+
assert(array[1] == "abc", "error parsing an array (1st elem)")
|
132
|
+
assert(array[2] = 2.7182818, "error parsing an array (2nd elem)")
|
133
|
+
|
134
|
+
str = '{"foo":"bar", "xyz":1, "e":2.7182818}'
|
135
|
+
lex = JSON::Lexer.new(str)
|
136
|
+
obj = lex.nextvalue
|
137
|
+
assert(obj["foo"] == "bar", "error parsing an object ('foo' elem)")
|
138
|
+
assert(obj["xyz"] == 1, "error parsing an object ('xyz' elem)")
|
139
|
+
assert(obj["e"] == 2.7182818, "error parsing an object ('e' elem)")
|
140
|
+
|
141
|
+
str = "false"
|
142
|
+
lex = JSON::Lexer.new(str)
|
143
|
+
assert(lex.nextvalue == false, "error parsing false");
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
data/test/objtest.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
#
|
2
|
+
# Unit Tests for JSON Objects
|
3
|
+
# Copyright (C) 2002 Rafael R. Sevilla <dido@imperium.ph>
|
4
|
+
# This file is part of JSON for Ruby
|
5
|
+
#
|
6
|
+
# JSON for Ruby is free software; you can redistribute it and/or
|
7
|
+
# modify it under the terms of the GNU Lesser General Public License
|
8
|
+
# as published by the Free Software Foundation; either version 2.1 of
|
9
|
+
# the License, or (at your option) any later version.
|
10
|
+
#
|
11
|
+
# JSON for Ruby is distributed in the hope that it will be useful,
|
12
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14
|
+
# Lesser General Public License for more details.
|
15
|
+
#
|
16
|
+
# You should have received a copy of the GNU Lesser General Public
|
17
|
+
# License along with JSON for Ruby; if not, write to the Free
|
18
|
+
# Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
19
|
+
# 02111-1307 USA.
|
20
|
+
#
|
21
|
+
# Author:: Rafael R. Sevilla (mailto:dido@imperium.ph)
|
22
|
+
# Copyright:: Copyright (c) 2003 Rafael R. Sevilla
|
23
|
+
# License:: GNU Lesser General Public License
|
24
|
+
# $Id: objtest.rb,v 1.2 2005/01/28 02:59:30 didosevilla Exp $
|
25
|
+
#
|
26
|
+
|
27
|
+
require 'test/unit'
|
28
|
+
require 'json/lexer'
|
29
|
+
require 'json/objects'
|
30
|
+
|
31
|
+
class ObjectTest < Test::Unit::TestCase
|
32
|
+
def test_to_json
|
33
|
+
obj = Hash.new
|
34
|
+
obj['foo'] = 'bar'
|
35
|
+
obj['xyz'] = 1
|
36
|
+
obj['euler'] = 0.577215665
|
37
|
+
|
38
|
+
str = obj.to_json
|
39
|
+
sobj = JSON::Lexer.new(str).nextvalue
|
40
|
+
assert(sobj.class == Hash, "Hash was not generated by deserialization")
|
41
|
+
assert(sobj['foo'] == 'bar', "error in serialization of a string to JSON")
|
42
|
+
assert(sobj['xyz'] == 1, "error in serialization of a FixNum to JSON")
|
43
|
+
assert(sobj['euler'] == 0.577215665, "error in serialization of a float to JSON")
|
44
|
+
end
|
45
|
+
end
|