RFC7159 7159

Sign up to get free protection for your applications and to get access to all the features.
Files changed (177) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +61 -0
  3. data/.rspec +4 -0
  4. data/.yardopts +2 -0
  5. data/Gemfile +46 -0
  6. data/LICENSE.txt +40 -0
  7. data/README.txt +899 -0
  8. data/RFC7159 +899 -0
  9. data/RFC7159.gemspec +85 -0
  10. data/Rakefile +82 -0
  11. data/lib/RFC7159.rb +80 -0
  12. data/lib/RFC7159/array.rb +134 -0
  13. data/lib/RFC7159/dumper.rb +231 -0
  14. data/lib/RFC7159/false.rb +79 -0
  15. data/lib/RFC7159/null.rb +79 -0
  16. data/lib/RFC7159/number.rb +149 -0
  17. data/lib/RFC7159/object.rb +140 -0
  18. data/lib/RFC7159/parser.ry +267 -0
  19. data/lib/RFC7159/string.rb +221 -0
  20. data/lib/RFC7159/true.rb +79 -0
  21. data/lib/RFC7159/value.rb +96 -0
  22. data/lib/RFC7159/version.rb +48 -0
  23. data/spec/RFC7159_spec.rb +275 -0
  24. data/spec/acceptance/README.txt +66 -0
  25. data/spec/acceptance/invalid/0001-ws/0001-verical-tab.txt +1 -0
  26. data/spec/acceptance/invalid/0001-ws/0002-null.txt +0 -0
  27. data/spec/acceptance/invalid/0001-ws/0003-space-in-number-1.txt +1 -0
  28. data/spec/acceptance/invalid/0001-ws/0004-space-in-number-2.txt +1 -0
  29. data/spec/acceptance/invalid/0001-ws/0005-space-in-number-3.txt +1 -0
  30. data/spec/acceptance/invalid/0001-ws/0006-space-in-number-4.txt +1 -0
  31. data/spec/acceptance/invalid/0001-ws/0007-space-in-number-5.txt +1 -0
  32. data/spec/acceptance/invalid/0001-ws/0008-space-in-number-6.txt +1 -0
  33. data/spec/acceptance/invalid/0001-ws/0009-space-in-literal.txt +1 -0
  34. data/spec/acceptance/invalid/0001-ws/0010-Unicode-LINE_SEPARATOR.txt +1 -0
  35. data/spec/acceptance/invalid/0002-comments/0001-C-style.txt +1 -0
  36. data/spec/acceptance/invalid/0002-comments/0002-C-plusplus-style.txt +2 -0
  37. data/spec/acceptance/invalid/0002-comments/0003-sh-style.txt +2 -0
  38. data/spec/acceptance/invalid/0002-comments/0004-python-docstring.txt +3 -0
  39. data/spec/acceptance/invalid/0002-comments/0005-SQL-style.txt +2 -0
  40. data/spec/acceptance/invalid/0002-comments/0006-BASIC-style.txt +2 -0
  41. data/spec/acceptance/invalid/0003-literals/0001-FALSE.txt +1 -0
  42. data/spec/acceptance/invalid/0003-literals/0002-NULL.txt +1 -0
  43. data/spec/acceptance/invalid/0003-literals/0003-TRUE.txt +1 -0
  44. data/spec/acceptance/invalid/0003-literals/0004-NUL.txt +1 -0
  45. data/spec/acceptance/invalid/0003-literals/0005-nil.txt +1 -0
  46. data/spec/acceptance/invalid/0003-literals/0006-undef.txt +1 -0
  47. data/spec/acceptance/invalid/0003-literals/0007-modifier.txt +1 -0
  48. data/spec/acceptance/invalid/0003-literals/0008-undefined.txt +1 -0
  49. data/spec/acceptance/invalid/0004-numbers/0001-omit-zero.txt +1 -0
  50. data/spec/acceptance/invalid/0004-numbers/0002-minus-dot.txt +1 -0
  51. data/spec/acceptance/invalid/0004-numbers/0003-missing-frac.txt +1 -0
  52. data/spec/acceptance/invalid/0004-numbers/0004-missing-exp.txt +1 -0
  53. data/spec/acceptance/invalid/0004-numbers/0005-octal.txt +1 -0
  54. data/spec/acceptance/invalid/0004-numbers/0006-hexadecimal.txt +1 -0
  55. data/spec/acceptance/invalid/0004-numbers/0007-comma.txt +1 -0
  56. data/spec/acceptance/invalid/0004-numbers/0008-perl-underscore.txt +1 -0
  57. data/spec/acceptance/invalid/0004-numbers/0009-NaN.txt +1 -0
  58. data/spec/acceptance/invalid/0004-numbers/0010-Inf.txt +1 -0
  59. data/spec/acceptance/invalid/0004-numbers/0011-Infinity.txt +1 -0
  60. data/spec/acceptance/invalid/0005-strings/0000-NUL.txt +0 -0
  61. data/spec/acceptance/invalid/0005-strings/0001-not-terminated.txt +1 -0
  62. data/spec/acceptance/invalid/0005-strings/0002-single-quote.txt +1 -0
  63. data/spec/acceptance/invalid/0005-strings/0003-back-quote.txt +1 -0
  64. data/spec/acceptance/invalid/0005-strings/0004-carriage-return.txt +1 -0
  65. data/spec/acceptance/invalid/0005-strings/0005-line-feed.txt +2 -0
  66. data/spec/acceptance/invalid/0005-strings/0006-unknown-escape-a.txt +1 -0
  67. data/spec/acceptance/invalid/0005-strings/0007-unknown-escape-perl-style.txt +1 -0
  68. data/spec/acceptance/invalid/0005-strings/0008-unknown-escape-C-style.txt +1 -0
  69. data/spec/acceptance/invalid/0005-strings/0009-unknown-escape-ruby-style.txt +1 -0
  70. data/spec/acceptance/invalid/0005-strings/0010-escape-too-short.txt +1 -0
  71. data/spec/acceptance/invalid/0005-strings/0011-C-string-concat.txt +1 -0
  72. data/spec/acceptance/invalid/0005-strings/0012-perl-string-concat.txt +1 -0
  73. data/spec/acceptance/invalid/0005-strings/0013-Java-string-concat.txt +1 -0
  74. data/spec/acceptance/invalid/0006-encodings/0001-CESU-8.txt +1 -0
  75. data/spec/acceptance/invalid/0006-encodings/0002-Windows-31J.txt +1 -0
  76. data/spec/acceptance/invalid/0006-encodings/0003-EBCDIC.txt +1 -0
  77. data/spec/acceptance/invalid/0006-encodings/0004-overlong-utf8.txt +1 -0
  78. data/spec/acceptance/invalid/0007-arrays/0001-lacks-open.txt +1 -0
  79. data/spec/acceptance/invalid/0007-arrays/0002-lacks-close.txt +1 -0
  80. data/spec/acceptance/invalid/0007-arrays/0003-interleaving-parens.txt +1 -0
  81. data/spec/acceptance/invalid/0007-arrays/0004-dangling-comma.txt +1 -0
  82. data/spec/acceptance/invalid/0007-arrays/0005-missing-comma.txt +1 -0
  83. data/spec/acceptance/invalid/0007-arrays/0006-colon-instead-of-comma.txt +1 -0
  84. data/spec/acceptance/invalid/0008-hashes/0001-key-missing.txt +1 -0
  85. data/spec/acceptance/invalid/0008-hashes/0002-value-missing.txt +1 -0
  86. data/spec/acceptance/invalid/0008-hashes/0003-true-key.txt +1 -0
  87. data/spec/acceptance/invalid/0008-hashes/0004-false-key.txt +1 -0
  88. data/spec/acceptance/invalid/0008-hashes/0005-null-key.txt +1 -0
  89. data/spec/acceptance/invalid/0008-hashes/0006-numeric-key.txt +1 -0
  90. data/spec/acceptance/invalid/0008-hashes/0007-array-key.txt +1 -0
  91. data/spec/acceptance/invalid/0008-hashes/0008-hash-key.txt +1 -0
  92. data/spec/acceptance/invalid/0008-hashes/0009-key-not-escaped.txt +4 -0
  93. data/spec/acceptance/invalid/0009-javascriptisms/0001-JSONP.txt +1 -0
  94. data/spec/acceptance/invalid/0009-javascriptisms/0002-new-Array.txt +3 -0
  95. data/spec/acceptance/invalid/0009-javascriptisms/0003-new-Date.txt +1 -0
  96. data/spec/acceptance/invalid/0009-javascriptisms/0004-new-Error.txt +1 -0
  97. data/spec/acceptance/invalid/0009-javascriptisms/0005-Math.txt +1 -0
  98. data/spec/acceptance/invalid/0009-javascriptisms/0006-regular-expression.txt +1 -0
  99. data/spec/acceptance/invalid/0009-javascriptisms/0007-function.txt +7 -0
  100. data/spec/acceptance/invalid/0009-javascriptisms/0008-this.txt +1 -0
  101. data/spec/acceptance/invalid/0009-javascriptisms/0009-plusplus.txt +3 -0
  102. data/spec/acceptance/invalid/0009-javascriptisms/0010-ternary-operator.txt +1 -0
  103. data/spec/acceptance/valid/0001-ws/0001-space.json +1 -0
  104. data/spec/acceptance/valid/0001-ws/0002-tab.json +1 -0
  105. data/spec/acceptance/valid/0001-ws/0003-lf.json +1 -0
  106. data/spec/acceptance/valid/0001-ws/0004-cr.json +1 -0
  107. data/spec/acceptance/valid/0001-ws/0005-before.json +1 -0
  108. data/spec/acceptance/valid/0001-ws/0006-after.json +1 -0
  109. data/spec/acceptance/valid/0001-ws/0007-around-comma.json +3 -0
  110. data/spec/acceptance/valid/0001-ws/0008-around-colon.json +3 -0
  111. data/spec/acceptance/valid/0002-bare-values/0001-false.json +1 -0
  112. data/spec/acceptance/valid/0002-bare-values/0002-null.json +1 -0
  113. data/spec/acceptance/valid/0002-bare-values/0003-true.json +1 -0
  114. data/spec/acceptance/valid/0002-bare-values/0004-number.json +1 -0
  115. data/spec/acceptance/valid/0002-bare-values/0005-string.json +1 -0
  116. data/spec/acceptance/valid/0003-literals/0001-false.json +1 -0
  117. data/spec/acceptance/valid/0003-literals/0002-null.json +1 -0
  118. data/spec/acceptance/valid/0003-literals/0003-true.json +1 -0
  119. data/spec/acceptance/valid/0004-numbers/0000-zero.json +1 -0
  120. data/spec/acceptance/valid/0004-numbers/0001-one.json +1 -0
  121. data/spec/acceptance/valid/0004-numbers/0002-two.json +1 -0
  122. data/spec/acceptance/valid/0004-numbers/0003-three.json +1 -0
  123. data/spec/acceptance/valid/0004-numbers/0004-four.json +1 -0
  124. data/spec/acceptance/valid/0004-numbers/0005-five.json +1 -0
  125. data/spec/acceptance/valid/0004-numbers/0006-six.json +1 -0
  126. data/spec/acceptance/valid/0004-numbers/0007-seven.json +1 -0
  127. data/spec/acceptance/valid/0004-numbers/0008-eight.json +1 -0
  128. data/spec/acceptance/valid/0004-numbers/0009-nine.json +1 -0
  129. data/spec/acceptance/valid/0004-numbers/0010-ten.json +1 -0
  130. data/spec/acceptance/valid/0004-numbers/0011-minus.json +1 -0
  131. data/spec/acceptance/valid/0004-numbers/0012-fraction.json +1 -0
  132. data/spec/acceptance/valid/0004-numbers/0013-exponent.json +1 -0
  133. data/spec/acceptance/valid/0004-numbers/0014-exponent-minus.json +1 -0
  134. data/spec/acceptance/valid/0004-numbers/0015-exponent-plus.json +1 -0
  135. data/spec/acceptance/valid/0004-numbers/0016-complex.json +1 -0
  136. data/spec/acceptance/valid/0004-numbers/0017-DBL_MAX.json +1 -0
  137. data/spec/acceptance/valid/0004-numbers/0018-DBL_MIN.json +1 -0
  138. data/spec/acceptance/valid/0004-numbers/0019-subnormal-number.json +1 -0
  139. data/spec/acceptance/valid/0004-numbers/0020-1E400.json +1 -0
  140. data/spec/acceptance/valid/0004-numbers/0021-pi.json +1 -0
  141. data/spec/acceptance/valid/0004-numbers/0022-UINT32_MAX.json +1 -0
  142. data/spec/acceptance/valid/0004-numbers/0023-UINT64_MAX.json +1 -0
  143. data/spec/acceptance/valid/0004-numbers/0024-INT64_MIN.json +1 -0
  144. data/spec/acceptance/valid/0004-numbers/0025-high-resolution-zero.json +1 -0
  145. data/spec/acceptance/valid/0005-strings/0001-empty.json +1 -0
  146. data/spec/acceptance/valid/0005-strings/0002-basic-latin.json +1 -0
  147. data/spec/acceptance/valid/0005-strings/0003-escapes.json +1 -0
  148. data/spec/acceptance/valid/0005-strings/0004-raw-unicode.json +1 -0
  149. data/spec/acceptance/valid/0005-strings/0005-escaped-unicode.json +1 -0
  150. data/spec/acceptance/valid/0005-strings/0006-escaped-NUL.json +1 -0
  151. data/spec/acceptance/valid/0005-strings/0007-escaped-invalid-unicode-still-valid-as-json.json +1 -0
  152. data/spec/acceptance/valid/0005-strings/0008-ruby-json-gem-cant-handle-this.json +1 -0
  153. data/spec/acceptance/valid/0005-strings/0009-unescaped-invalid-javascript-still-valid-as-json.json +1 -0
  154. data/spec/acceptance/valid/0006-m17n/0001-genesis.json +6 -0
  155. data/spec/acceptance/valid/0006-m17n/0002-heart-sutra.json +5 -0
  156. data/spec/acceptance/valid/0006-m17n/0003-escaped-valid-surrogate-pair.json +1 -0
  157. data/spec/acceptance/valid/0006-m17n/0004-unescaped-valid-supplementary-multilingual-plane.json +1 -0
  158. data/spec/acceptance/valid/0007-arrays/0000-empty.json +1 -0
  159. data/spec/acceptance/valid/0007-arrays/0001-one-element.json +1 -0
  160. data/spec/acceptance/valid/0007-arrays/0002-multiple-elements.json +33 -0
  161. data/spec/acceptance/valid/0007-arrays/0003-various-types.json +1 -0
  162. data/spec/acceptance/valid/0007-arrays/0004-nested.json +17 -0
  163. data/spec/acceptance/valid/0008-hashes/0000-empty.json +1 -0
  164. data/spec/acceptance/valid/0008-hashes/0001-onekey.json +1 -0
  165. data/spec/acceptance/valid/0008-hashes/0002-many-keys.json +5 -0
  166. data/spec/acceptance/valid/0008-hashes/0003-empty-key.json +3 -0
  167. data/spec/acceptance/valid/0008-hashes/0004-true-value.json +3 -0
  168. data/spec/acceptance/valid/0008-hashes/0005-false-value.json +3 -0
  169. data/spec/acceptance/valid/0008-hashes/0006-null-value.json +3 -0
  170. data/spec/acceptance/valid/0008-hashes/0007-string-value.json +3 -0
  171. data/spec/acceptance/valid/0008-hashes/0008-numeric-value.json +3 -0
  172. data/spec/acceptance/valid/0008-hashes/0009-array-value.json +8 -0
  173. data/spec/acceptance/valid/0008-hashes/0010-hash-value.json +20 -0
  174. data/spec/acceptance/valid/0008-hashes/0011-duplicate-key-in-different-representations.json +4 -0
  175. data/spec/acceptance/valid/0008-hashes/0011-duplicate-key.json +4 -0
  176. data/spec/spec_helper.rb +54 -0
  177. metadata +520 -0
@@ -0,0 +1,79 @@
1
+ #! /your/favourite/path/to/ruby
2
+ # -*- coding: utf-8 -*-
3
+
4
+ # Copyright (c) 2014 Urabe, Shyouhei. All rights reserved.
5
+ #
6
+ # Redistribution and use in source and binary forms, with or without
7
+ # modification, are permitted provided that the following conditions are met:
8
+ #
9
+ # - Redistributions of source code must retain the above copyright
10
+ # notice, this list of conditions and the following disclaimer.
11
+ #
12
+ # - Redistributions in binary form must reproduce the above copyright
13
+ # notice, this list of conditions and the following disclaimer in
14
+ # the documentation and/or other materials provided with the
15
+ # distribution.
16
+ #
17
+ # - Neither the name of Internet Society, IETF or IETF Trust, nor the
18
+ # names of specific contributors, may be used to endorse or promote
19
+ # products derived from this software without specific prior written
20
+ # permission.
21
+ #
22
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
23
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
+ # POSSIBILITY OF SUCH DAMAGE.
33
+
34
+ # The true, as defined in RFC7159 section 3.
35
+ class RFC7159::True < RFC7159::Value
36
+
37
+ # Parse the AST from parser, and convert into corrsponding value.
38
+ # @param [Array] ast the AST, generated by the parser
39
+ # @return [self] evaluated instance
40
+ # @raise [ArgumentError] malformed input
41
+ def self.from_ast ast
42
+ raise ArgumentError "garbage included: #{ast.inspect}" if ast.length > 1
43
+ raise ArgumentError "not a false: #{ast.inspect}" if ast[0] != :true
44
+ return new
45
+ end
46
+
47
+ # convert to Ruby's true
48
+ # @return [true] the ruby counter part.
49
+ def plain_old_ruby_object
50
+ return true
51
+ end
52
+
53
+ # JSON gem compat
54
+ # @return [::String] JSONified string representation
55
+ def to_json
56
+ return 'true'
57
+ end
58
+
59
+ # Equality. All true instances are equal each other, plus a TrueClass
60
+ # instance is also equal to this.
61
+ def == other
62
+ case other when TrueClass, self.class
63
+ true
64
+ else
65
+ false
66
+ end
67
+ end
68
+ end
69
+
70
+ #
71
+ # Local Variables:
72
+ # mode: ruby
73
+ # coding: utf-8-unix
74
+ # indent-tabs-mode: t
75
+ # tab-width: 3
76
+ # ruby-indent-level: 3
77
+ # fill-column: 79
78
+ # default-justification: full
79
+ # End:
@@ -0,0 +1,96 @@
1
+ #! /your/favourite/path/to/ruby
2
+ # -*- coding: utf-8 -*-
3
+
4
+ # Copyright (c) 2014 Urabe, Shyouhei. All rights reserved.
5
+ #
6
+ # Redistribution and use in source and binary forms, with or without
7
+ # modification, are permitted provided that the following conditions are met:
8
+ #
9
+ # - Redistributions of source code must retain the above copyright
10
+ # notice, this list of conditions and the following disclaimer.
11
+ #
12
+ # - Redistributions in binary form must reproduce the above copyright
13
+ # notice, this list of conditions and the following disclaimer in
14
+ # the documentation and/or other materials provided with the
15
+ # distribution.
16
+ #
17
+ # - Neither the name of Internet Society, IETF or IETF Trust, nor the
18
+ # names of specific contributors, may be used to endorse or promote
19
+ # products derived from this software without specific prior written
20
+ # permission.
21
+ #
22
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
23
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
+ # POSSIBILITY OF SUCH DAMAGE.
33
+
34
+ # @abstract
35
+ # The Values, as described in RFC7159 section 3.
36
+ #
37
+ # > value = false / null / true / object / array / number / string
38
+ class RFC7159::Value
39
+
40
+ # Parse the AST from parser, and convert into corrsponding values.
41
+ # @param [::Array] ast the AST, generated by the parser
42
+ # @return [Value] evaluated instance
43
+ # @raise [ArgumentError] malformed input
44
+ #
45
+ # @note the input MUST be a tree. No cyclic lists, nor even direct-acycric
46
+ # graphs are allowed by definition. Such input can cause undefined
47
+ # behaviour.
48
+ def self.from_ast ast
49
+ klass = case ast.first
50
+ when :false then RFC7159::False
51
+ when :null then RFC7159::Null
52
+ when :true then RFC7159::True
53
+ when :object then RFC7159::Object
54
+ when :array then RFC7159::Array
55
+ when :number then RFC7159::Number
56
+ when :string then RFC7159::String
57
+ end
58
+ return klass.from_ast ast
59
+ end
60
+
61
+ # convert to Ruby's value
62
+ def plain_old_ruby_object
63
+ raise RuntimeError, 'abstruct method called'
64
+ end
65
+
66
+ # @return [::String] the class in string
67
+ def inspect
68
+ sprintf "#<%p>", self.class
69
+ end
70
+
71
+ # For pretty print (require 'pp' beforehand)
72
+ # @param [PP] pp the pp
73
+ def pretty_print pp
74
+ pp.object_group self do end # nothing
75
+ end
76
+ end
77
+
78
+ # *WARN* order matters, do not move below.
79
+ require_relative 'false'
80
+ require_relative 'null'
81
+ require_relative 'true'
82
+ require_relative 'object'
83
+ require_relative 'array'
84
+ require_relative 'number'
85
+ require_relative 'string'
86
+
87
+ #
88
+ # Local Variables:
89
+ # mode: ruby
90
+ # coding: utf-8-unix
91
+ # indent-tabs-mode: t
92
+ # tab-width: 3
93
+ # ruby-indent-level: 3
94
+ # fill-column: 79
95
+ # default-justification: full
96
+ # End:
@@ -0,0 +1,48 @@
1
+ #! /your/favourite/path/to/ruby
2
+ # -*- coding: utf-8 -*-
3
+
4
+ # Copyright (c) 2014 Urabe, Shyouhei. All rights reserved.
5
+ #
6
+ # Redistribution and use in source and binary forms, with or without
7
+ # modification, are permitted provided that the following conditions are met:
8
+ #
9
+ # - Redistributions of source code must retain the above copyright
10
+ # notice, this list of conditions and the following disclaimer.
11
+ #
12
+ # - Redistributions in binary form must reproduce the above copyright
13
+ # notice, this list of conditions and the following disclaimer in
14
+ # the documentation and/or other materials provided with the
15
+ # distribution.
16
+ #
17
+ # - Neither the name of Internet Society, IETF or IETF Trust, nor the
18
+ # names of specific contributors, may be used to endorse or promote
19
+ # products derived from this software without specific prior written
20
+ # permission.
21
+ #
22
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
23
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
+ # POSSIBILITY OF SUCH DAMAGE.
33
+
34
+ require_relative '../RFC7159'
35
+
36
+ # The version
37
+ RFC7159::VERSION = 7159
38
+
39
+ #
40
+ # Local Variables:
41
+ # mode: ruby
42
+ # coding: utf-8-unix
43
+ # indent-tabs-mode: t
44
+ # tab-width: 3
45
+ # ruby-indent-level: 3
46
+ # fill-column: 79
47
+ # default-justification: full
48
+ # End:
@@ -0,0 +1,275 @@
1
+ #! /your/favourite/path/to/rspec
2
+ # -*- coding: utf-8 -*-
3
+
4
+ # Copyright (c) 2014 Urabe, Shyouhei. All rights reserved.
5
+ #
6
+ # Redistribution and use in source and binary forms, with or without
7
+ # modification, are permitted provided that the following conditions are met:
8
+ #
9
+ # - Redistributions of source code must retain the above copyright
10
+ # notice, this list of conditions and the following disclaimer.
11
+ #
12
+ # - Redistributions in binary form must reproduce the above copyright
13
+ # notice, this list of conditions and the following disclaimer in
14
+ # the documentation and/or other materials provided with the
15
+ # distribution.
16
+ #
17
+ # - Neither the name of Internet Society, IETF or IETF Trust, nor the
18
+ # names of specific contributors, may be used to endorse or promote
19
+ # products derived from this software without specific prior written
20
+ # permission.
21
+ #
22
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
23
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
+ # POSSIBILITY OF SUCH DAMAGE.
33
+
34
+ require_relative 'spec_helper'
35
+ require 'pathname'
36
+
37
+ describe RFC7159 do
38
+ this_dir = Pathname.new __dir__
39
+
40
+ describe '.load' do
41
+ the_dir = this_dir + 'acceptance/valid'
42
+ the_dir.find do |f|
43
+ case f.extname when '.json'
44
+ it "should accept: #{f.basename}" do
45
+ expect do
46
+ f.open 'r:utf-8' do |fp|
47
+ RFC7159.load fp
48
+ end
49
+ end.to_not raise_exception
50
+ end
51
+ end
52
+ end
53
+
54
+ the_dir = this_dir + 'acceptance/invalid'
55
+ the_dir.find do |f|
56
+ case f.extname when '.txt'
57
+ it "should reject: #{f.basename}" do
58
+ expect do
59
+ f.open 'r:utf-8' do |fp|
60
+ RFC7159.load fp
61
+ end
62
+ end.to raise_exception
63
+ end
64
+ end
65
+ end
66
+ end
67
+
68
+ describe '.dump' do
69
+ the_dir = this_dir + 'acceptance/valid'
70
+ the_dir.find do |f|
71
+ case f.extname when '.json'
72
+ it "should round-trip: #{f.basename}" do
73
+ str1 = f.open 'r:utf-8' do |fp| fp.read end
74
+ obj = RFC7159.load str1
75
+ str2 = RFC7159.dump obj
76
+ # not interested in indents
77
+ expect(str2.gsub(/\s+/, '')).to eq(str1.gsub(/\s+/, ''))
78
+ end
79
+ end
80
+ end
81
+
82
+ context 'from something outside JSON world' do
83
+ {
84
+ false => 'false',
85
+ true => 'true',
86
+ nil => 'null',
87
+ 0 => '0',
88
+ 0.5 => '0.5', # 0.5 has no error
89
+ 'foo' => '"foo"',
90
+ "\u{dead}" => '"\\uDEAD"', # invalid UTF8 to be valid escaped UTF8
91
+ [] => '[]',
92
+ [0] => '[0]',
93
+ {} => '{}',
94
+ {'1'=>1} => '{"1":1}',
95
+ {''=>''} => '{"":""}',
96
+ (0.0/0.0) => false,
97
+ (1.0/0.0) => false,
98
+ BigDecimal("NaN") => false,
99
+ // => false,
100
+ 0..1 => false,
101
+ {{}=>{}} => false,
102
+ {[]=>[]} => false,
103
+ {1=>1} => false,
104
+ {nil=>nil} => false,
105
+ Object.new => false,
106
+ Class.new => false,
107
+ Proc.new {} => false,
108
+ }.each_pair do |src, expected|
109
+ if expected
110
+ it { expect(RFC7159.dump src).to eq(expected) }
111
+ else
112
+ it { expect{RFC7159.dump src}.to raise_exception }
113
+ end
114
+ end
115
+ end
116
+ end
117
+
118
+ context 'versus' do
119
+ the_dir = this_dir + 'acceptance/valid'
120
+ the_targets = Array.new
121
+ the_dir.find do |f|
122
+ case f.extname when '.json'
123
+ begin
124
+ f.open 'r:utf-8' do |fp|
125
+ RFC7159.load fp, plain:true
126
+ end
127
+ rescue RuntimeError
128
+ # there are cases JSON can't be represented in PORO
129
+ # we ignore them here.
130
+ else
131
+ the_targets.push f
132
+ end
133
+ end
134
+ end
135
+ the_dir = this_dir + 'acceptance/invalid'
136
+ the_invalids = Array.new
137
+ the_dir.find do |f|
138
+ case f.extname when '.txt'
139
+ the_invalids.push f
140
+ end
141
+ end
142
+
143
+ require 'json'
144
+ require 'oj'
145
+ require 'yajl'
146
+ targets = {
147
+ JSON => {
148
+ load: -> str {
149
+ JSON.parse str
150
+ },
151
+ dump: -> obj {
152
+ begin
153
+ ret = JSON.generate obj
154
+ rescue
155
+ raise "JSON (not us) failed: #{$!.inspect}"
156
+ else
157
+ unless ret.valid_encoding?
158
+ raise "JSON (not us) is broken: generated #{ret.dump}"
159
+ end
160
+ return ret
161
+ end
162
+ },
163
+ },
164
+
165
+ Oj => {
166
+ load: -> str {
167
+ Oj.load str
168
+ },
169
+ dump: -> obj {
170
+ begin
171
+ ret = Oj.dump obj
172
+ rescue
173
+ raise "Oj (not us) failed: #{$!.inspect}"
174
+ else
175
+ unless ret.valid_encoding?
176
+ raise "Oj (not us) is broken: generated #{ret.dump}"
177
+ end
178
+ return ret
179
+ end
180
+ },
181
+ },
182
+
183
+ Yajl => {
184
+ load: -> str {
185
+ Yajl::Parser.parse str, allow_comments: false, symbolize_keys: false
186
+ },
187
+ dump: -> obj {
188
+ begin
189
+ ret = Yajl::Encoder.encode obj
190
+ rescue
191
+ raise "Yajl (not us) failed: #{$!.inspect}"
192
+ else
193
+ unless ret.valid_encoding?
194
+ raise "Yajl (not us) is broken: generated #{ret.dump}"
195
+ end
196
+ return ret
197
+ end
198
+ },
199
+ },
200
+ }
201
+
202
+ targets.each_pair do |klass, howto|
203
+ describe klass do
204
+ the_invalids.each do |f|
205
+ it "should reject: #{f.basename}" do
206
+ begin
207
+ theirs = f.open 'r:utf-8' do |fp|
208
+ howto[:load].(fp.read)
209
+ end
210
+ rescue
211
+ # ok
212
+ else
213
+ pending "#{klass} (not us) failed to reject: #{theirs.inspect}"
214
+ end
215
+ end
216
+ end
217
+
218
+ the_targets.each do |f|
219
+ context f.basename do
220
+ before :all do
221
+ str = f.open 'r:utf-8' do |fp| fp.read end
222
+ @ours = RFC7159.load str, plain: true
223
+ begin
224
+ @theirs = howto[:load].(str)
225
+ rescue
226
+ pending "#{klass} (not us) failed to accept: #{$!.inspect}"
227
+ end
228
+ # JSON.load sometimes generates Infinity and that's not JSONable
229
+ case @theirs when Array
230
+ case @theirs[0] when Float
231
+ if @theirs[0].infinite?
232
+ pending "#{klass} (not us) generates non-JSONables: #{@theirs.inspect}"
233
+ end
234
+ end
235
+ end
236
+ end
237
+
238
+ it "compare load" do
239
+ if @ours != @theirs
240
+ pending "They load differently: \n#{@ours.inspect} versus \n#{@theirs.inspect}"
241
+ end
242
+ end
243
+
244
+ it "compare dump ours" do
245
+ ours = RFC7159.dump @ours
246
+ theirs = howto[:dump].(@ours) rescue pending($!.message)
247
+ if ours.gsub(/\s+/, '') != theirs.gsub(/\s+/, '')
248
+ pending "They dump differently: \n#{ours.dump} versus \n#{theirs.dump}"
249
+ end
250
+ end
251
+
252
+ it "compare dump theirs" do
253
+ ours = RFC7159.dump @theirs
254
+ theirs = (howto[:dump].(@theirs) rescue pending($!.message))
255
+ if ours.gsub(/\s+/, '') != theirs.gsub(/\s+/, '')
256
+ pending "They dump differently: \n#{ours.dump} versus \n#{theirs.dump}"
257
+ end
258
+ end
259
+ end
260
+ end
261
+ end
262
+ end
263
+ end
264
+ end
265
+
266
+ #
267
+ # Local Variables:
268
+ # mode: ruby
269
+ # coding: utf-8-unix
270
+ # indent-tabs-mode: t
271
+ # tab-width: 3
272
+ # ruby-indent-level: 3
273
+ # fill-column: 79
274
+ # default-justification: full
275
+ # End: