crazy_ivan 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (133) hide show
  1. data/.gitignore +7 -0
  2. data/LICENSE +20 -0
  3. data/README.rdoc +94 -0
  4. data/Rakefile +92 -0
  5. data/TODO +33 -0
  6. data/VERSION +1 -0
  7. data/bin/crazy_ivan +114 -0
  8. data/crazy_ivan.gemspec +182 -0
  9. data/lib/crazy_ivan.rb +5 -0
  10. data/lib/html_asset_crush.rb +56 -0
  11. data/lib/report_assembler.rb +78 -0
  12. data/lib/test_runner.rb +71 -0
  13. data/templates/css/ci.css +11 -0
  14. data/templates/index.html +105 -0
  15. data/templates/javascript/json-template.js +544 -0
  16. data/templates/javascript/prototype.js +4917 -0
  17. data/test/crazy_ivan_test.rb +4 -0
  18. data/test/test_helper.rb +9 -0
  19. data/vendor/json-1.1.7/CHANGES +119 -0
  20. data/vendor/json-1.1.7/GPL +340 -0
  21. data/vendor/json-1.1.7/README +78 -0
  22. data/vendor/json-1.1.7/RUBY +58 -0
  23. data/vendor/json-1.1.7/Rakefile +270 -0
  24. data/vendor/json-1.1.7/TODO +1 -0
  25. data/vendor/json-1.1.7/VERSION +1 -0
  26. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkComparison.log +52 -0
  27. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_fast-autocorrelation.dat +1000 -0
  28. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_fast.dat +1001 -0
  29. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_pretty-autocorrelation.dat +900 -0
  30. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_pretty.dat +901 -0
  31. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_safe-autocorrelation.dat +1000 -0
  32. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_safe.dat +1001 -0
  33. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt.log +261 -0
  34. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_fast-autocorrelation.dat +1000 -0
  35. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_fast.dat +1001 -0
  36. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_pretty-autocorrelation.dat +1000 -0
  37. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_pretty.dat +1001 -0
  38. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_safe-autocorrelation.dat +1000 -0
  39. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_safe.dat +1001 -0
  40. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure.log +262 -0
  41. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails#generator-autocorrelation.dat +1000 -0
  42. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails#generator.dat +1001 -0
  43. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails.log +82 -0
  44. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkComparison.log +34 -0
  45. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt#parser-autocorrelation.dat +900 -0
  46. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt#parser.dat +901 -0
  47. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt.log +81 -0
  48. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure#parser-autocorrelation.dat +1000 -0
  49. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure#parser.dat +1001 -0
  50. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure.log +82 -0
  51. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails#parser-autocorrelation.dat +1000 -0
  52. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails#parser.dat +1001 -0
  53. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails.log +82 -0
  54. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML#parser-autocorrelation.dat +1000 -0
  55. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML#parser.dat +1001 -0
  56. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML.log +82 -0
  57. data/vendor/json-1.1.7/benchmarks/generator_benchmark.rb +165 -0
  58. data/vendor/json-1.1.7/benchmarks/parser_benchmark.rb +197 -0
  59. data/vendor/json-1.1.7/bin/edit_json.rb +9 -0
  60. data/vendor/json-1.1.7/bin/prettify_json.rb +75 -0
  61. data/vendor/json-1.1.7/data/example.json +1 -0
  62. data/vendor/json-1.1.7/data/index.html +38 -0
  63. data/vendor/json-1.1.7/data/prototype.js +4184 -0
  64. data/vendor/json-1.1.7/doc-templates/main.txt +283 -0
  65. data/vendor/json-1.1.7/ext/json/ext/generator/extconf.rb +11 -0
  66. data/vendor/json-1.1.7/ext/json/ext/generator/generator.c +919 -0
  67. data/vendor/json-1.1.7/ext/json/ext/generator/unicode.c +182 -0
  68. data/vendor/json-1.1.7/ext/json/ext/generator/unicode.h +53 -0
  69. data/vendor/json-1.1.7/ext/json/ext/parser/extconf.rb +11 -0
  70. data/vendor/json-1.1.7/ext/json/ext/parser/parser.c +1829 -0
  71. data/vendor/json-1.1.7/ext/json/ext/parser/parser.rl +686 -0
  72. data/vendor/json-1.1.7/ext/json/ext/parser/unicode.c +154 -0
  73. data/vendor/json-1.1.7/ext/json/ext/parser/unicode.h +58 -0
  74. data/vendor/json-1.1.7/install.rb +26 -0
  75. data/vendor/json-1.1.7/lib/json.rb +10 -0
  76. data/vendor/json-1.1.7/lib/json/Array.xpm +21 -0
  77. data/vendor/json-1.1.7/lib/json/FalseClass.xpm +21 -0
  78. data/vendor/json-1.1.7/lib/json/Hash.xpm +21 -0
  79. data/vendor/json-1.1.7/lib/json/Key.xpm +73 -0
  80. data/vendor/json-1.1.7/lib/json/NilClass.xpm +21 -0
  81. data/vendor/json-1.1.7/lib/json/Numeric.xpm +28 -0
  82. data/vendor/json-1.1.7/lib/json/String.xpm +96 -0
  83. data/vendor/json-1.1.7/lib/json/TrueClass.xpm +21 -0
  84. data/vendor/json-1.1.7/lib/json/add/core.rb +135 -0
  85. data/vendor/json-1.1.7/lib/json/add/rails.rb +58 -0
  86. data/vendor/json-1.1.7/lib/json/common.rb +354 -0
  87. data/vendor/json-1.1.7/lib/json/editor.rb +1371 -0
  88. data/vendor/json-1.1.7/lib/json/ext.rb +15 -0
  89. data/vendor/json-1.1.7/lib/json/json.xpm +1499 -0
  90. data/vendor/json-1.1.7/lib/json/pure.rb +77 -0
  91. data/vendor/json-1.1.7/lib/json/pure/generator.rb +430 -0
  92. data/vendor/json-1.1.7/lib/json/pure/parser.rb +269 -0
  93. data/vendor/json-1.1.7/lib/json/version.rb +8 -0
  94. data/vendor/json-1.1.7/tests/fixtures/fail1.json +1 -0
  95. data/vendor/json-1.1.7/tests/fixtures/fail10.json +1 -0
  96. data/vendor/json-1.1.7/tests/fixtures/fail11.json +1 -0
  97. data/vendor/json-1.1.7/tests/fixtures/fail12.json +1 -0
  98. data/vendor/json-1.1.7/tests/fixtures/fail13.json +1 -0
  99. data/vendor/json-1.1.7/tests/fixtures/fail14.json +1 -0
  100. data/vendor/json-1.1.7/tests/fixtures/fail18.json +1 -0
  101. data/vendor/json-1.1.7/tests/fixtures/fail19.json +1 -0
  102. data/vendor/json-1.1.7/tests/fixtures/fail2.json +1 -0
  103. data/vendor/json-1.1.7/tests/fixtures/fail20.json +1 -0
  104. data/vendor/json-1.1.7/tests/fixtures/fail21.json +1 -0
  105. data/vendor/json-1.1.7/tests/fixtures/fail22.json +1 -0
  106. data/vendor/json-1.1.7/tests/fixtures/fail23.json +1 -0
  107. data/vendor/json-1.1.7/tests/fixtures/fail24.json +1 -0
  108. data/vendor/json-1.1.7/tests/fixtures/fail25.json +1 -0
  109. data/vendor/json-1.1.7/tests/fixtures/fail27.json +2 -0
  110. data/vendor/json-1.1.7/tests/fixtures/fail28.json +2 -0
  111. data/vendor/json-1.1.7/tests/fixtures/fail3.json +1 -0
  112. data/vendor/json-1.1.7/tests/fixtures/fail4.json +1 -0
  113. data/vendor/json-1.1.7/tests/fixtures/fail5.json +1 -0
  114. data/vendor/json-1.1.7/tests/fixtures/fail6.json +1 -0
  115. data/vendor/json-1.1.7/tests/fixtures/fail7.json +1 -0
  116. data/vendor/json-1.1.7/tests/fixtures/fail8.json +1 -0
  117. data/vendor/json-1.1.7/tests/fixtures/fail9.json +1 -0
  118. data/vendor/json-1.1.7/tests/fixtures/pass1.json +56 -0
  119. data/vendor/json-1.1.7/tests/fixtures/pass15.json +1 -0
  120. data/vendor/json-1.1.7/tests/fixtures/pass16.json +1 -0
  121. data/vendor/json-1.1.7/tests/fixtures/pass17.json +1 -0
  122. data/vendor/json-1.1.7/tests/fixtures/pass2.json +1 -0
  123. data/vendor/json-1.1.7/tests/fixtures/pass26.json +1 -0
  124. data/vendor/json-1.1.7/tests/fixtures/pass3.json +6 -0
  125. data/vendor/json-1.1.7/tests/test_json.rb +312 -0
  126. data/vendor/json-1.1.7/tests/test_json_addition.rb +164 -0
  127. data/vendor/json-1.1.7/tests/test_json_fixtures.rb +34 -0
  128. data/vendor/json-1.1.7/tests/test_json_generate.rb +106 -0
  129. data/vendor/json-1.1.7/tests/test_json_rails.rb +146 -0
  130. data/vendor/json-1.1.7/tests/test_json_unicode.rb +62 -0
  131. data/vendor/json-1.1.7/tools/fuzz.rb +139 -0
  132. data/vendor/json-1.1.7/tools/server.rb +61 -0
  133. metadata +196 -0
@@ -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,135 @@
1
+ # This file contains implementations of ruby core's custom objects for
2
+ # serialisation/deserialisation.
3
+
4
+ unless Object.const_defined?(:JSON) and ::JSON.const_defined?(:JSON_LOADED) and
5
+ ::JSON::JSON_LOADED
6
+ require 'json'
7
+ end
8
+ require 'date'
9
+
10
+ class Time
11
+ def self.json_create(object)
12
+ if usec = object.delete('u') # used to be tv_usec -> tv_nsec
13
+ object['n'] = usec * 1000
14
+ end
15
+ if respond_to?(:tv_nsec)
16
+ at(*object.values_at('s', 'n'))
17
+ else
18
+ at(object['s'], object['n'] / 1000)
19
+ end
20
+ end
21
+
22
+ def to_json(*args)
23
+ {
24
+ 'json_class' => self.class.name,
25
+ 's' => tv_sec,
26
+ 'n' => respond_to?(:tv_nsec) ? tv_nsec : tv_usec * 1000
27
+ }.to_json(*args)
28
+ end
29
+ end
30
+
31
+ class Date
32
+ def self.json_create(object)
33
+ civil(*object.values_at('y', 'm', 'd', 'sg'))
34
+ end
35
+
36
+ alias start sg unless method_defined?(:start)
37
+
38
+ def to_json(*args)
39
+ {
40
+ 'json_class' => self.class.name,
41
+ 'y' => year,
42
+ 'm' => month,
43
+ 'd' => day,
44
+ 'sg' => start,
45
+ }.to_json(*args)
46
+ end
47
+ end
48
+
49
+ class DateTime
50
+ def self.json_create(object)
51
+ args = object.values_at('y', 'm', 'd', 'H', 'M', 'S')
52
+ of_a, of_b = object['of'].split('/')
53
+ if of_b and of_b != '0'
54
+ args << Rational(of_a.to_i, of_b.to_i)
55
+ else
56
+ args << of_a
57
+ end
58
+ args << object['sg']
59
+ civil(*args)
60
+ end
61
+
62
+ alias start sg unless method_defined?(:start)
63
+
64
+ def to_json(*args)
65
+ {
66
+ 'json_class' => self.class.name,
67
+ 'y' => year,
68
+ 'm' => month,
69
+ 'd' => day,
70
+ 'H' => hour,
71
+ 'M' => min,
72
+ 'S' => sec,
73
+ 'of' => offset.to_s,
74
+ 'sg' => start,
75
+ }.to_json(*args)
76
+ end
77
+ end
78
+
79
+ class Range
80
+ def self.json_create(object)
81
+ new(*object['a'])
82
+ end
83
+
84
+ def to_json(*args)
85
+ {
86
+ 'json_class' => self.class.name,
87
+ 'a' => [ first, last, exclude_end? ]
88
+ }.to_json(*args)
89
+ end
90
+ end
91
+
92
+ class Struct
93
+ def self.json_create(object)
94
+ new(*object['v'])
95
+ end
96
+
97
+ def to_json(*args)
98
+ klass = self.class.name
99
+ klass.to_s.empty? and raise JSON::JSONError, "Only named structs are supported!"
100
+ {
101
+ 'json_class' => klass,
102
+ 'v' => values,
103
+ }.to_json(*args)
104
+ end
105
+ end
106
+
107
+ class Exception
108
+ def self.json_create(object)
109
+ result = new(object['m'])
110
+ result.set_backtrace object['b']
111
+ result
112
+ end
113
+
114
+ def to_json(*args)
115
+ {
116
+ 'json_class' => self.class.name,
117
+ 'm' => message,
118
+ 'b' => backtrace,
119
+ }.to_json(*args)
120
+ end
121
+ end
122
+
123
+ class Regexp
124
+ def self.json_create(object)
125
+ new(object['s'], object['o'])
126
+ end
127
+
128
+ def to_json(*)
129
+ {
130
+ 'json_class' => self.class.name,
131
+ 'o' => options,
132
+ 's' => source,
133
+ }.to_json
134
+ end
135
+ end
@@ -0,0 +1,58 @@
1
+ # This file contains implementations of rails custom objects for
2
+ # serialisation/deserialisation.
3
+
4
+ unless Object.const_defined?(:JSON) and ::JSON.const_defined?(:JSON_LOADED) and
5
+ ::JSON::JSON_LOADED
6
+ require 'json'
7
+ end
8
+
9
+ class Object
10
+ def self.json_create(object)
11
+ obj = new
12
+ for key, value in object
13
+ next if key == 'json_class'
14
+ instance_variable_set "@#{key}", value
15
+ end
16
+ obj
17
+ end
18
+
19
+ def to_json(*a)
20
+ result = {
21
+ 'json_class' => self.class.name
22
+ }
23
+ instance_variables.inject(result) do |r, name|
24
+ r[name[1..-1]] = instance_variable_get name
25
+ r
26
+ end
27
+ result.to_json(*a)
28
+ end
29
+ end
30
+
31
+ class Symbol
32
+ def to_json(*a)
33
+ to_s.to_json(*a)
34
+ end
35
+ end
36
+
37
+ module Enumerable
38
+ def to_json(*a)
39
+ to_a.to_json(*a)
40
+ end
41
+ end
42
+
43
+ # class Regexp
44
+ # def to_json(*)
45
+ # inspect
46
+ # end
47
+ # end
48
+ #
49
+ # The above rails definition has some problems:
50
+ #
51
+ # 1. { 'foo' => /bar/ }.to_json # => "{foo: /bar/}"
52
+ # This isn't valid JSON, because the regular expression syntax is not
53
+ # defined in RFC 4627. (And unquoted strings are disallowed there, too.)
54
+ # Though it is valid Javascript.
55
+ #
56
+ # 2. { 'foo' => /bar/mix }.to_json # => "{foo: /bar/mix}"
57
+ # This isn't even valid Javascript.
58
+
@@ -0,0 +1,354 @@
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
6
+ # as a Ruby data structure. Otherwise generate a JSON text from the Ruby
7
+ # data structure object and return it.
8
+ #
9
+ # The _opts_ argument is passed through to generate/parse respectively, see
10
+ # generate and parse for their documentation.
11
+ def [](object, opts = {})
12
+ if object.respond_to? :to_str
13
+ JSON.parse(object.to_str, opts => {})
14
+ else
15
+ JSON.generate(object, opts => {})
16
+ end
17
+ end
18
+
19
+ # Returns the JSON parser class, that is used by JSON. This might be either
20
+ # JSON::Ext::Parser or JSON::Pure::Parser.
21
+ attr_reader :parser
22
+
23
+ # Set the JSON parser class _parser_ to be used by JSON.
24
+ def parser=(parser) # :nodoc:
25
+ @parser = parser
26
+ remove_const :Parser if const_defined? :Parser
27
+ const_set :Parser, parser
28
+ end
29
+
30
+ # Return the constant located at _path_. The format of _path_ has to be
31
+ # either ::A::B::C or A::B::C. In any case A has to be located at the top
32
+ # level (absolute namespace path?). If there doesn't exist a constant at
33
+ # the given path, an ArgumentError is raised.
34
+ def deep_const_get(path) # :nodoc:
35
+ path = path.to_s
36
+ path.split(/::/).inject(Object) do |p, c|
37
+ case
38
+ when c.empty? then p
39
+ when p.const_defined?(c) then p.const_get(c)
40
+ else raise ArgumentError, "can't find const #{path}"
41
+ end
42
+ end
43
+ end
44
+
45
+ # Set the module _generator_ to be used by JSON.
46
+ def generator=(generator) # :nodoc:
47
+ @generator = generator
48
+ generator_methods = generator::GeneratorMethods
49
+ for const in generator_methods.constants
50
+ klass = deep_const_get(const)
51
+ modul = generator_methods.const_get(const)
52
+ klass.class_eval do
53
+ instance_methods(false).each do |m|
54
+ m.to_s == 'to_json' and remove_method m
55
+ end
56
+ include modul
57
+ end
58
+ end
59
+ self.state = generator::State
60
+ const_set :State, self.state
61
+ end
62
+
63
+ # Returns the JSON generator modul, that is used by JSON. This might be
64
+ # either JSON::Ext::Generator or JSON::Pure::Generator.
65
+ attr_reader :generator
66
+
67
+ # Returns the JSON generator state class, that is used by JSON. This might
68
+ # be either JSON::Ext::Generator::State or JSON::Pure::Generator::State.
69
+ attr_accessor :state
70
+
71
+ # This is create identifier, that is used to decide, if the _json_create_
72
+ # hook of a class should be called. It defaults to 'json_class'.
73
+ attr_accessor :create_id
74
+ end
75
+ self.create_id = 'json_class'
76
+
77
+ NaN = (-1.0) ** 0.5
78
+
79
+ Infinity = 1.0/0
80
+
81
+ MinusInfinity = -Infinity
82
+
83
+ # The base exception for JSON errors.
84
+ class JSONError < StandardError; end
85
+
86
+ # This exception is raised, if a parser error occurs.
87
+ class ParserError < JSONError; end
88
+
89
+ # This exception is raised, if the nesting of parsed datastructures is too
90
+ # deep.
91
+ class NestingError < ParserError; end
92
+
93
+ # This exception is raised, if a generator or unparser error occurs.
94
+ class GeneratorError < JSONError; end
95
+ # For backwards compatibility
96
+ UnparserError = GeneratorError
97
+
98
+ # If a circular data structure is encountered while unparsing
99
+ # this exception is raised.
100
+ class CircularDatastructure < GeneratorError; end
101
+
102
+ # This exception is raised, if the required unicode support is missing on the
103
+ # system. Usually this means, that the iconv library is not installed.
104
+ class MissingUnicodeSupport < JSONError; end
105
+
106
+ module_function
107
+
108
+ # Parse the JSON string _source_ into a Ruby data structure and return it.
109
+ #
110
+ # _opts_ can have the following
111
+ # keys:
112
+ # * *max_nesting*: The maximum depth of nesting allowed in the parsed data
113
+ # structures. Disable depth checking with :max_nesting => false, it defaults
114
+ # to 19.
115
+ # * *allow_nan*: If set to true, allow NaN, Infinity and -Infinity in
116
+ # defiance of RFC 4627 to be parsed by the Parser. This option defaults
117
+ # to false.
118
+ # * *create_additions*: If set to false, the Parser doesn't create
119
+ # additions even if a matchin class and create_id was found. This option
120
+ # defaults to true.
121
+ def parse(source, opts = {})
122
+ JSON.parser.new(source, opts).parse
123
+ end
124
+
125
+ # Parse the JSON string _source_ into a Ruby data structure and return it.
126
+ # The bang version of the parse method, defaults to the more dangerous values
127
+ # for the _opts_ hash, so be sure only to parse trusted _source_ strings.
128
+ #
129
+ # _opts_ can have the following keys:
130
+ # * *max_nesting*: The maximum depth of nesting allowed in the parsed data
131
+ # structures. Enable depth checking with :max_nesting => anInteger. The parse!
132
+ # methods defaults to not doing max depth checking: This can be dangerous,
133
+ # if someone wants to fill up your stack.
134
+ # * *allow_nan*: If set to true, allow NaN, Infinity, and -Infinity in
135
+ # defiance of RFC 4627 to be parsed by the Parser. This option defaults
136
+ # to true.
137
+ # * *create_additions*: If set to false, the Parser doesn't create
138
+ # additions even if a matchin class and create_id was found. This option
139
+ # defaults to true.
140
+ def parse!(source, opts = {})
141
+ opts = {
142
+ :max_nesting => false,
143
+ :allow_nan => true
144
+ }.update(opts)
145
+ JSON.parser.new(source, opts).parse
146
+ end
147
+
148
+ # Unparse the Ruby data structure _obj_ into a single line JSON string and
149
+ # return it. _state_ is
150
+ # * a JSON::State object,
151
+ # * or a Hash like object (responding to to_hash),
152
+ # * an object convertible into a hash by a to_h method,
153
+ # that is used as or to configure a State object.
154
+ #
155
+ # It defaults to a state object, that creates the shortest possible JSON text
156
+ # in one line, checks for circular data structures and doesn't allow NaN,
157
+ # Infinity, and -Infinity.
158
+ #
159
+ # A _state_ hash can have the following keys:
160
+ # * *indent*: a string used to indent levels (default: ''),
161
+ # * *space*: a string that is put after, a : or , delimiter (default: ''),
162
+ # * *space_before*: a string that is put before a : pair delimiter (default: ''),
163
+ # * *object_nl*: a string that is put at the end of a JSON object (default: ''),
164
+ # * *array_nl*: a string that is put at the end of a JSON array (default: ''),
165
+ # * *check_circular*: true if checking for circular data structures
166
+ # should be done (the default), false otherwise.
167
+ # * *allow_nan*: true if NaN, Infinity, and -Infinity should be
168
+ # generated, otherwise an exception is thrown, if these values are
169
+ # encountered. This options defaults to false.
170
+ # * *max_nesting*: The maximum depth of nesting allowed in the data
171
+ # structures from which JSON is to be generated. Disable depth checking
172
+ # with :max_nesting => false, it defaults to 19.
173
+ #
174
+ # See also the fast_generate for the fastest creation method with the least
175
+ # amount of sanity checks, and the pretty_generate method for some
176
+ # defaults for a pretty output.
177
+ def generate(obj, state = nil)
178
+ if state
179
+ state = State.from_state(state)
180
+ else
181
+ state = State.new
182
+ end
183
+ obj.to_json(state)
184
+ end
185
+
186
+ # :stopdoc:
187
+ # I want to deprecate these later, so I'll first be silent about them, and
188
+ # later delete them.
189
+ alias unparse generate
190
+ module_function :unparse
191
+ # :startdoc:
192
+
193
+ # Unparse the Ruby data structure _obj_ into a single line JSON string and
194
+ # return it. This method disables the checks for circles in Ruby objects, and
195
+ # also generates NaN, Infinity, and, -Infinity float values.
196
+ #
197
+ # *WARNING*: Be careful not to pass any Ruby data structures with circles as
198
+ # _obj_ argument, because this will cause JSON to go into an infinite loop.
199
+ def fast_generate(obj)
200
+ obj.to_json(nil)
201
+ end
202
+
203
+ # :stopdoc:
204
+ # I want to deprecate these later, so I'll first be silent about them, and later delete them.
205
+ alias fast_unparse fast_generate
206
+ module_function :fast_unparse
207
+ # :startdoc:
208
+
209
+ # Unparse the Ruby data structure _obj_ into a JSON string and return it. The
210
+ # returned string is a prettier form of the string returned by #unparse.
211
+ #
212
+ # The _opts_ argument can be used to configure the generator, see the
213
+ # generate method for a more detailed explanation.
214
+ def pretty_generate(obj, opts = nil)
215
+ state = JSON.state.new(
216
+ :indent => ' ',
217
+ :space => ' ',
218
+ :object_nl => "\n",
219
+ :array_nl => "\n",
220
+ :check_circular => true
221
+ )
222
+ if opts
223
+ if opts.respond_to? :to_hash
224
+ opts = opts.to_hash
225
+ elsif opts.respond_to? :to_h
226
+ opts = opts.to_h
227
+ else
228
+ raise TypeError, "can't convert #{opts.class} into Hash"
229
+ end
230
+ state.configure(opts)
231
+ end
232
+ obj.to_json(state)
233
+ end
234
+
235
+ # :stopdoc:
236
+ # I want to deprecate these later, so I'll first be silent about them, and later delete them.
237
+ alias pretty_unparse pretty_generate
238
+ module_function :pretty_unparse
239
+ # :startdoc:
240
+
241
+ # Load a ruby data structure from a JSON _source_ and return it. A source can
242
+ # either be a string-like object, an IO like object, or an object responding
243
+ # to the read method. If _proc_ was given, it will be called with any nested
244
+ # Ruby object as an argument recursively in depth first order.
245
+ #
246
+ # This method is part of the implementation of the load/dump interface of
247
+ # Marshal and YAML.
248
+ def load(source, proc = nil)
249
+ if source.respond_to? :to_str
250
+ source = source.to_str
251
+ elsif source.respond_to? :to_io
252
+ source = source.to_io.read
253
+ else
254
+ source = source.read
255
+ end
256
+ result = parse(source, :max_nesting => false, :allow_nan => true)
257
+ recurse_proc(result, &proc) if proc
258
+ result
259
+ end
260
+
261
+ def recurse_proc(result, &proc)
262
+ case result
263
+ when Array
264
+ result.each { |x| recurse_proc x, &proc }
265
+ proc.call result
266
+ when Hash
267
+ result.each { |x, y| recurse_proc x, &proc; recurse_proc y, &proc }
268
+ proc.call result
269
+ else
270
+ proc.call result
271
+ end
272
+ end
273
+ private :recurse_proc
274
+ module_function :recurse_proc
275
+
276
+ alias restore load
277
+ module_function :restore
278
+
279
+ # Dumps _obj_ as a JSON string, i.e. calls generate on the object and returns
280
+ # the result.
281
+ #
282
+ # If anIO (an IO like object or an object that responds to the write method)
283
+ # was given, the resulting JSON is written to it.
284
+ #
285
+ # If the number of nested arrays or objects exceeds _limit_ an ArgumentError
286
+ # exception is raised. This argument is similar (but not exactly the
287
+ # same!) to the _limit_ argument in Marshal.dump.
288
+ #
289
+ # This method is part of the implementation of the load/dump interface of
290
+ # Marshal and YAML.
291
+ def dump(obj, anIO = nil, limit = nil)
292
+ if anIO and limit.nil?
293
+ anIO = anIO.to_io if anIO.respond_to?(:to_io)
294
+ unless anIO.respond_to?(:write)
295
+ limit = anIO
296
+ anIO = nil
297
+ end
298
+ end
299
+ limit ||= 0
300
+ result = generate(obj, :allow_nan => true, :max_nesting => limit)
301
+ if anIO
302
+ anIO.write result
303
+ anIO
304
+ else
305
+ result
306
+ end
307
+ rescue JSON::NestingError
308
+ raise ArgumentError, "exceed depth limit"
309
+ end
310
+ end
311
+
312
+ module ::Kernel
313
+ # Outputs _objs_ to STDOUT as JSON strings in the shortest form, that is in
314
+ # one line.
315
+ def j(*objs)
316
+ objs.each do |obj|
317
+ puts JSON::generate(obj, :allow_nan => true, :max_nesting => false)
318
+ end
319
+ nil
320
+ end
321
+
322
+ # Ouputs _objs_ to STDOUT as JSON strings in a pretty format, with
323
+ # indentation and over many lines.
324
+ def jj(*objs)
325
+ objs.each do |obj|
326
+ puts JSON::pretty_generate(obj, :allow_nan => true, :max_nesting => false)
327
+ end
328
+ nil
329
+ end
330
+
331
+ # If _object_ is string-like parse the string and return the parsed result as
332
+ # a Ruby data structure. Otherwise generate a JSON text from the Ruby data
333
+ # structure object and return it.
334
+ #
335
+ # The _opts_ argument is passed through to generate/parse respectively, see
336
+ # generate and parse for their documentation.
337
+ def JSON(object, opts = {})
338
+ if object.respond_to? :to_str
339
+ JSON.parse(object.to_str, opts)
340
+ else
341
+ JSON.generate(object, opts)
342
+ end
343
+ end
344
+ end
345
+
346
+ class ::Class
347
+ # Returns true, if this class can be used to create an instance
348
+ # from a serialised JSON string. The class has to implement a class
349
+ # method _json_create_ that expects a hash as first parameter, which includes
350
+ # the required data.
351
+ def json_creatable?
352
+ respond_to?(:json_create)
353
+ end
354
+ end