json_pure 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (83) hide show
  1. data/CHANGES +25 -0
  2. data/GPL +340 -0
  3. data/README +77 -0
  4. data/Rakefile +250 -0
  5. data/TODO +1 -0
  6. data/VERSION +1 -0
  7. data/benchmarks/benchmark.txt +133 -0
  8. data/benchmarks/benchmark_generator.rb +44 -0
  9. data/benchmarks/benchmark_parser.rb +22 -0
  10. data/benchmarks/benchmark_rails.rb +26 -0
  11. data/bin/edit_json.rb +11 -0
  12. data/data/example.json +1 -0
  13. data/data/index.html +37 -0
  14. data/data/prototype.js +2515 -0
  15. data/ext/json/ext/generator/Makefile +149 -0
  16. data/ext/json/ext/generator/extconf.rb +9 -0
  17. data/ext/json/ext/generator/generator.c +729 -0
  18. data/ext/json/ext/generator/unicode.c +184 -0
  19. data/ext/json/ext/generator/unicode.h +40 -0
  20. data/ext/json/ext/parser/Makefile +149 -0
  21. data/ext/json/ext/parser/extconf.rb +9 -0
  22. data/ext/json/ext/parser/parser.c +1551 -0
  23. data/ext/json/ext/parser/parser.rl +515 -0
  24. data/ext/json/ext/parser/unicode.c +156 -0
  25. data/ext/json/ext/parser/unicode.h +44 -0
  26. data/install.rb +26 -0
  27. data/lib/json.rb +205 -0
  28. data/lib/json/Array.xpm +21 -0
  29. data/lib/json/FalseClass.xpm +21 -0
  30. data/lib/json/Hash.xpm +21 -0
  31. data/lib/json/Key.xpm +73 -0
  32. data/lib/json/NilClass.xpm +21 -0
  33. data/lib/json/Numeric.xpm +28 -0
  34. data/lib/json/String.xpm +96 -0
  35. data/lib/json/TrueClass.xpm +21 -0
  36. data/lib/json/common.rb +184 -0
  37. data/lib/json/editor.rb +1207 -0
  38. data/lib/json/ext.rb +13 -0
  39. data/lib/json/json.xpm +1499 -0
  40. data/lib/json/pure.rb +75 -0
  41. data/lib/json/pure/generator.rb +321 -0
  42. data/lib/json/pure/parser.rb +210 -0
  43. data/lib/json/version.rb +8 -0
  44. data/tests/fixtures/fail1.json +1 -0
  45. data/tests/fixtures/fail10.json +1 -0
  46. data/tests/fixtures/fail11.json +1 -0
  47. data/tests/fixtures/fail12.json +1 -0
  48. data/tests/fixtures/fail13.json +1 -0
  49. data/tests/fixtures/fail14.json +1 -0
  50. data/tests/fixtures/fail15.json +1 -0
  51. data/tests/fixtures/fail16.json +1 -0
  52. data/tests/fixtures/fail17.json +1 -0
  53. data/tests/fixtures/fail19.json +1 -0
  54. data/tests/fixtures/fail2.json +1 -0
  55. data/tests/fixtures/fail20.json +1 -0
  56. data/tests/fixtures/fail21.json +1 -0
  57. data/tests/fixtures/fail22.json +1 -0
  58. data/tests/fixtures/fail23.json +1 -0
  59. data/tests/fixtures/fail24.json +1 -0
  60. data/tests/fixtures/fail25.json +1 -0
  61. data/tests/fixtures/fail26.json +1 -0
  62. data/tests/fixtures/fail27.json +2 -0
  63. data/tests/fixtures/fail28.json +2 -0
  64. data/tests/fixtures/fail3.json +1 -0
  65. data/tests/fixtures/fail4.json +1 -0
  66. data/tests/fixtures/fail5.json +1 -0
  67. data/tests/fixtures/fail6.json +1 -0
  68. data/tests/fixtures/fail7.json +1 -0
  69. data/tests/fixtures/fail8.json +1 -0
  70. data/tests/fixtures/fail9.json +1 -0
  71. data/tests/fixtures/pass1.json +56 -0
  72. data/tests/fixtures/pass18.json +1 -0
  73. data/tests/fixtures/pass2.json +1 -0
  74. data/tests/fixtures/pass3.json +6 -0
  75. data/tests/runner.rb +24 -0
  76. data/tests/test_json.rb +235 -0
  77. data/tests/test_json_addition.rb +94 -0
  78. data/tests/test_json_fixtures.rb +30 -0
  79. data/tests/test_json_generate.rb +81 -0
  80. data/tests/test_json_unicode.rb +55 -0
  81. data/tools/fuzz.rb +133 -0
  82. data/tools/server.rb +62 -0
  83. metadata +146 -0
@@ -0,0 +1,21 @@
1
+ /* XPM */
2
+ static char * False_xpm[] = {
3
+ "16 16 2 1",
4
+ " c None",
5
+ ". c #FF0000",
6
+ " ",
7
+ " ",
8
+ " ",
9
+ " ...... ",
10
+ " . ",
11
+ " . ",
12
+ " . ",
13
+ " ...... ",
14
+ " . ",
15
+ " . ",
16
+ " . ",
17
+ " . ",
18
+ " . ",
19
+ " ",
20
+ " ",
21
+ " "};
data/lib/json/Hash.xpm ADDED
@@ -0,0 +1,21 @@
1
+ /* XPM */
2
+ static char * Hash_xpm[] = {
3
+ "16 16 2 1",
4
+ " c None",
5
+ ". c #000000",
6
+ " ",
7
+ " ",
8
+ " ",
9
+ " . . ",
10
+ " . . ",
11
+ " . . ",
12
+ " ......... ",
13
+ " . . ",
14
+ " . . ",
15
+ " ......... ",
16
+ " . . ",
17
+ " . . ",
18
+ " . . ",
19
+ " ",
20
+ " ",
21
+ " "};
data/lib/json/Key.xpm ADDED
@@ -0,0 +1,73 @@
1
+ /* XPM */
2
+ static char * Key_xpm[] = {
3
+ "16 16 54 1",
4
+ " c None",
5
+ ". c #110007",
6
+ "+ c #0E0900",
7
+ "@ c #000013",
8
+ "# c #070600",
9
+ "$ c #F6F006",
10
+ "% c #ECE711",
11
+ "& c #E5EE00",
12
+ "* c #16021E",
13
+ "= c #120900",
14
+ "- c #EDF12B",
15
+ "; c #000033",
16
+ "> c #0F0000",
17
+ ", c #FFFE03",
18
+ "' c #E6E500",
19
+ ") c #16021B",
20
+ "! c #F7F502",
21
+ "~ c #000E00",
22
+ "{ c #130000",
23
+ "] c #FFF000",
24
+ "^ c #FFE711",
25
+ "/ c #140005",
26
+ "( c #190025",
27
+ "_ c #E9DD27",
28
+ ": c #E7DC04",
29
+ "< c #FFEC09",
30
+ "[ c #FFE707",
31
+ "} c #FFDE10",
32
+ "| c #150021",
33
+ "1 c #160700",
34
+ "2 c #FAF60E",
35
+ "3 c #EFE301",
36
+ "4 c #FEF300",
37
+ "5 c #E7E000",
38
+ "6 c #FFFF08",
39
+ "7 c #0E0206",
40
+ "8 c #040000",
41
+ "9 c #03052E",
42
+ "0 c #041212",
43
+ "a c #070300",
44
+ "b c #F2E713",
45
+ "c c #F9DE13",
46
+ "d c #36091E",
47
+ "e c #00001C",
48
+ "f c #1F0010",
49
+ "g c #FFF500",
50
+ "h c #DEDE00",
51
+ "i c #050A00",
52
+ "j c #FAF14A",
53
+ "k c #F5F200",
54
+ "l c #040404",
55
+ "m c #1A0D00",
56
+ "n c #EDE43D",
57
+ "o c #ECE007",
58
+ " ",
59
+ " ",
60
+ " .+@ ",
61
+ " #$%&* ",
62
+ " =-;>,') ",
63
+ " >!~{]^/ ",
64
+ " (_:<[}| ",
65
+ " 1234567 ",
66
+ " 890abcd ",
67
+ " efghi ",
68
+ " >jkl ",
69
+ " mnol ",
70
+ " >kl ",
71
+ " ll ",
72
+ " ",
73
+ " "};
@@ -0,0 +1,21 @@
1
+ /* XPM */
2
+ static char * False_xpm[] = {
3
+ "16 16 2 1",
4
+ " c None",
5
+ ". c #000000",
6
+ " ",
7
+ " ",
8
+ " ",
9
+ " ... ",
10
+ " . . ",
11
+ " . . ",
12
+ " . . ",
13
+ " . . ",
14
+ " . . ",
15
+ " . . ",
16
+ " . . ",
17
+ " . . ",
18
+ " ... ",
19
+ " ",
20
+ " ",
21
+ " "};
@@ -0,0 +1,28 @@
1
+ /* XPM */
2
+ static char * Numeric_xpm[] = {
3
+ "16 16 9 1",
4
+ " c None",
5
+ ". c #FF0000",
6
+ "+ c #0000FF",
7
+ "@ c #0023DB",
8
+ "# c #00EA14",
9
+ "$ c #00FF00",
10
+ "% c #004FAF",
11
+ "& c #0028D6",
12
+ "* c #00F20C",
13
+ " ",
14
+ " ",
15
+ " ",
16
+ " ... +++@#$$$$ ",
17
+ " .+ %& $$ ",
18
+ " . + $ ",
19
+ " . + $$ ",
20
+ " . ++$$$$ ",
21
+ " . + $$ ",
22
+ " . + $ ",
23
+ " . + $ ",
24
+ " . + $ $$ ",
25
+ " .....++++*$$ ",
26
+ " ",
27
+ " ",
28
+ " "};
@@ -0,0 +1,96 @@
1
+ /* XPM */
2
+ static char * String_xpm[] = {
3
+ "16 16 77 1",
4
+ " c None",
5
+ ". c #000000",
6
+ "+ c #040404",
7
+ "@ c #080806",
8
+ "# c #090606",
9
+ "$ c #EEEAE1",
10
+ "% c #E7E3DA",
11
+ "& c #E0DBD1",
12
+ "* c #D4B46F",
13
+ "= c #0C0906",
14
+ "- c #E3C072",
15
+ "; c #E4C072",
16
+ "> c #060505",
17
+ ", c #0B0A08",
18
+ "' c #D5B264",
19
+ ") c #D3AF5A",
20
+ "! c #080602",
21
+ "~ c #E1B863",
22
+ "{ c #DDB151",
23
+ "] c #DBAE4A",
24
+ "^ c #DDB152",
25
+ "/ c #DDB252",
26
+ "( c #070705",
27
+ "_ c #0C0A07",
28
+ ": c #D3A33B",
29
+ "< c #020201",
30
+ "[ c #DAAA41",
31
+ "} c #040302",
32
+ "| c #E4D9BF",
33
+ "1 c #0B0907",
34
+ "2 c #030201",
35
+ "3 c #020200",
36
+ "4 c #C99115",
37
+ "5 c #080704",
38
+ "6 c #DBC8A2",
39
+ "7 c #E7D7B4",
40
+ "8 c #E0CD9E",
41
+ "9 c #080601",
42
+ "0 c #040400",
43
+ "a c #010100",
44
+ "b c #0B0B08",
45
+ "c c #DCBF83",
46
+ "d c #DCBC75",
47
+ "e c #DEB559",
48
+ "f c #040301",
49
+ "g c #BC8815",
50
+ "h c #120E07",
51
+ "i c #060402",
52
+ "j c #0A0804",
53
+ "k c #D4A747",
54
+ "l c #D6A12F",
55
+ "m c #0E0C05",
56
+ "n c #C8C1B0",
57
+ "o c #1D1B15",
58
+ "p c #D7AD51",
59
+ "q c #070502",
60
+ "r c #080804",
61
+ "s c #BC953B",
62
+ "t c #C4BDAD",
63
+ "u c #0B0807",
64
+ "v c #DBAC47",
65
+ "w c #1B150A",
66
+ "x c #B78A2C",
67
+ "y c #D8A83C",
68
+ "z c #D4A338",
69
+ "A c #0F0B03",
70
+ "B c #181105",
71
+ "C c #C59325",
72
+ "D c #C18E1F",
73
+ "E c #060600",
74
+ "F c #CC992D",
75
+ "G c #B98B25",
76
+ "H c #B3831F",
77
+ "I c #C08C1C",
78
+ "J c #060500",
79
+ "K c #0E0C03",
80
+ "L c #0D0A00",
81
+ " ",
82
+ " .+@# ",
83
+ " .$%&*= ",
84
+ " .-;>,')! ",
85
+ " .~. .{]. ",
86
+ " .^/. (_:< ",
87
+ " .[.}|$12 ",
88
+ " 345678}90 ",
89
+ " a2bcdefgh ",
90
+ " ijkl.mno ",
91
+ " <pq. rstu ",
92
+ " .]v. wx= ",
93
+ " .yzABCDE ",
94
+ " .FGHIJ ",
95
+ " 0KL0 ",
96
+ " "};
@@ -0,0 +1,21 @@
1
+ /* XPM */
2
+ static char * TrueClass_xpm[] = {
3
+ "16 16 2 1",
4
+ " c None",
5
+ ". c #0BF311",
6
+ " ",
7
+ " ",
8
+ " ",
9
+ " ......... ",
10
+ " . ",
11
+ " . ",
12
+ " . ",
13
+ " . ",
14
+ " . ",
15
+ " . ",
16
+ " . ",
17
+ " . ",
18
+ " . ",
19
+ " ",
20
+ " ",
21
+ " "};
@@ -0,0 +1,184 @@
1
+ require 'json/version'
2
+
3
+ module JSON
4
+ class << self
5
+ # If object is string like parse the string and return the parsed result as a
6
+ # Ruby data structure. Otherwise generate a JSON text from the Ruby data
7
+ # structure object and return it.
8
+ def [](object)
9
+ if object.respond_to? :to_str
10
+ JSON.parse(object.to_str)
11
+ else
12
+ JSON.generate(object)
13
+ end
14
+ end
15
+
16
+ # Returns the JSON parser class, that is used by JSON. This might be either
17
+ # JSON::Ext::Parser or JSON::Pure::Parser.
18
+ attr_reader :parser
19
+
20
+ # Set the JSON parser class _parser_ to be used by JSON.
21
+ def parser=(parser) # :nodoc:
22
+ @parser = parser
23
+ remove_const :Parser if const_defined? :Parser
24
+ const_set :Parser, parser
25
+ end
26
+
27
+ # Return the constant located at _path_. The format of _path_ has to be
28
+ # either ::A::B::C or A::B::C. In any case A has to be located at the top
29
+ # level (absolute namespace path?). If there doesn't exist a constant at
30
+ # the given path, an ArgumentError is raised.
31
+ def deep_const_get(path) # :nodoc:
32
+ path.split(/::/).inject(Object) do |p, c|
33
+ case
34
+ when c.empty? then p
35
+ when p.const_defined?(c) then p.const_get(c)
36
+ else raise ArgumentError, "can't find #{path}"
37
+ end
38
+ end
39
+ end
40
+
41
+ # Set the module _generator_ to be used by JSON.
42
+ def generator=(generator) # :nodoc:
43
+ @generator = generator
44
+ generator_methods = generator::GeneratorMethods
45
+ for const in generator_methods.constants
46
+ klass = deep_const_get(const)
47
+ modul = generator_methods.const_get(const)
48
+ klass.class_eval do
49
+ instance_methods(false).each do |m|
50
+ m.to_s == 'to_json' and remove_method m
51
+ end
52
+ include modul
53
+ end
54
+ end
55
+ self.state = generator::State
56
+ const_set :State, self.state
57
+ end
58
+
59
+ # Returns the JSON generator modul, that is used by JSON. This might be
60
+ # either JSON::Ext::Generator or JSON::Pure::Generator.
61
+ attr_reader :generator
62
+
63
+ # Returns the JSON generator state class, that is used by JSON. This might
64
+ # be either JSON::Ext::Generator::State or JSON::Pure::Generator::State.
65
+ attr_accessor :state
66
+
67
+ # This is create identifier, that is used to decide, if the _json_create_
68
+ # hook of a class should be called. It defaults to 'json_class'.
69
+ attr_accessor :create_id
70
+ end
71
+ self.create_id = 'json_class'
72
+
73
+ # The base exception for JSON errors.
74
+ class JSONError < StandardError; end
75
+
76
+ # This exception is raised, if a parser error occurs.
77
+ class ParserError < JSONError; end
78
+
79
+ # This exception is raised, if a generator or unparser error occurs.
80
+ class GeneratorError < JSONError; end
81
+ # For backwards compatibility
82
+ UnparserError = GeneratorError
83
+
84
+ # If a circular data structure is encountered while unparsing
85
+ # this exception is raised.
86
+ class CircularDatastructure < GeneratorError; end
87
+
88
+ # This exception is raised, if the required unicode support is missing on the
89
+ # system. Usually this means, that the iconv library is not installed.
90
+ class MissingUnicodeSupport < JSONError; end
91
+
92
+ module_function
93
+
94
+ # Parse the JSON string _source_ into a Ruby data structure and return it.
95
+ def parse(source)
96
+ JSON.parser.new(source).parse
97
+ end
98
+
99
+ # Unparse the Ruby data structure _obj_ into a single line JSON string and
100
+ # return it. _state_ is a JSON::State object, that can be used to configure
101
+ # the output further.
102
+ #
103
+ # It defaults to a state object, that creates the shortest possible JSON text
104
+ # in one line and only checks for circular data structures. If you are sure,
105
+ # that the objects don't contain any circles, you can set _state_ to nil, to
106
+ # disable these checks in order to create the JSON text faster. See also
107
+ # fast_generate.
108
+ def generate(obj, state = JSON.state.new)
109
+ obj.to_json(state)
110
+ end
111
+
112
+ alias unparse generate
113
+ module_function :unparse
114
+
115
+ # Unparse the Ruby data structure _obj_ into a single line JSON string and
116
+ # return it. This method disables the checks for circles in Ruby objects.
117
+ #
118
+ # *WARNING*: Be careful not to pass any Ruby data structures with circles as
119
+ # _obj_ argument, because this will cause JSON to go into an infinite loop.
120
+ def fast_generate(obj)
121
+ obj.to_json(nil)
122
+ end
123
+
124
+ alias fast_unparse fast_generate
125
+ module_function :fast_unparse
126
+
127
+ # Unparse the Ruby data structure _obj_ into a JSON string and return it. The
128
+ # returned string is a prettier form of the string returned by #unparse.
129
+ def pretty_generate(obj)
130
+ state = JSON.state.new(
131
+ :indent => ' ',
132
+ :space => ' ',
133
+ :object_nl => "\n",
134
+ :array_nl => "\n",
135
+ :check_circular => true
136
+ )
137
+ obj.to_json(state)
138
+ end
139
+
140
+ alias pretty_unparse pretty_generate
141
+ module_function :pretty_unparse
142
+ end
143
+
144
+ module ::Kernel
145
+ # Outputs _objs_ to STDOUT as JSON strings in the shortest form, that is in
146
+ # one line.
147
+ def j(*objs)
148
+ objs.each do |obj|
149
+ puts JSON::generate(obj)
150
+ end
151
+ nil
152
+ end
153
+
154
+ # Ouputs _objs_ to STDOUT as JSON strings in a pretty format, with
155
+ # indentation and over many lines.
156
+ def jj(*objs)
157
+ objs.each do |obj|
158
+ puts JSON::pretty_generate(obj)
159
+ end
160
+ nil
161
+ end
162
+
163
+ # If object is string like parse the string and return the parsed result as a
164
+ # Ruby data structure. Otherwise generate a JSON text from the Ruby data
165
+ # structure object and return it.
166
+ def JSON(object)
167
+ if object.respond_to? :to_str
168
+ JSON.parse(object.to_str)
169
+ else
170
+ JSON.generate(object)
171
+ end
172
+ end
173
+ end
174
+
175
+ class ::Class
176
+ # Returns true, if this class can be used to create an instance
177
+ # from a serialised JSON string. The class has to implement a class
178
+ # method _json_create_ that expects a hash as first parameter, which includes
179
+ # the required data.
180
+ def json_creatable?
181
+ respond_to?(:json_create)
182
+ end
183
+ end
184
+ # vim: set et sw=2 ts=2: