json 2.7.3 → 2.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,129 +0,0 @@
1
- #ifndef _GENERATOR_H_
2
- #define _GENERATOR_H_
3
-
4
- #include <math.h>
5
- #include <ctype.h>
6
-
7
- #include "ruby.h"
8
-
9
- /* This is the fallback definition from Ruby 3.4 */
10
- #ifndef RBIMPL_STDBOOL_H
11
- #if defined(__cplusplus)
12
- # if defined(HAVE_STDBOOL_H) && (__cplusplus >= 201103L)
13
- # include <cstdbool>
14
- # endif
15
- #elif defined(HAVE_STDBOOL_H)
16
- # include <stdbool.h>
17
- #elif !defined(HAVE__BOOL)
18
- typedef unsigned char _Bool;
19
- # define bool _Bool
20
- # define true ((_Bool)+1)
21
- # define false ((_Bool)+0)
22
- # define __bool_true_false_are_defined
23
- #endif
24
- #endif
25
-
26
- static char *fstrndup(const char *ptr, unsigned long len);
27
-
28
- /* ruby api and some helpers */
29
-
30
- typedef struct JSON_Generator_StateStruct {
31
- char *indent;
32
- long indent_len;
33
- char *space;
34
- long space_len;
35
- char *space_before;
36
- long space_before_len;
37
- char *object_nl;
38
- long object_nl_len;
39
- char *array_nl;
40
- long array_nl_len;
41
- long max_nesting;
42
- char allow_nan;
43
- char ascii_only;
44
- char script_safe;
45
- char strict;
46
- long depth;
47
- long buffer_initial_length;
48
- } JSON_Generator_State;
49
-
50
- #define GET_STATE_TO(self, state) \
51
- TypedData_Get_Struct(self, JSON_Generator_State, &JSON_Generator_State_type, state)
52
-
53
- #define GET_STATE(self) \
54
- JSON_Generator_State *state; \
55
- GET_STATE_TO(self, state)
56
-
57
- #define GENERATE_JSON(type) \
58
- FBuffer *buffer; \
59
- VALUE Vstate; \
60
- JSON_Generator_State *state; \
61
- \
62
- rb_scan_args(argc, argv, "01", &Vstate); \
63
- Vstate = cState_from_state_s(cState, Vstate); \
64
- TypedData_Get_Struct(Vstate, JSON_Generator_State, &JSON_Generator_State_type, state); \
65
- buffer = cState_prepare_buffer(Vstate); \
66
- generate_json_##type(buffer, Vstate, state, self); \
67
- return fbuffer_to_s(buffer)
68
-
69
- static VALUE mHash_to_json(int argc, VALUE *argv, VALUE self);
70
- static VALUE mArray_to_json(int argc, VALUE *argv, VALUE self);
71
- #ifdef RUBY_INTEGER_UNIFICATION
72
- static VALUE mInteger_to_json(int argc, VALUE *argv, VALUE self);
73
- #else
74
- static VALUE mFixnum_to_json(int argc, VALUE *argv, VALUE self);
75
- static VALUE mBignum_to_json(int argc, VALUE *argv, VALUE self);
76
- #endif
77
- static VALUE mFloat_to_json(int argc, VALUE *argv, VALUE self);
78
- static VALUE mString_included_s(VALUE self, VALUE modul);
79
- static VALUE mString_to_json(int argc, VALUE *argv, VALUE self);
80
- static VALUE mString_to_json_raw_object(VALUE self);
81
- static VALUE mString_to_json_raw(int argc, VALUE *argv, VALUE self);
82
- static VALUE mString_Extend_json_create(VALUE self, VALUE o);
83
- static VALUE mTrueClass_to_json(int argc, VALUE *argv, VALUE self);
84
- static VALUE mFalseClass_to_json(int argc, VALUE *argv, VALUE self);
85
- static VALUE mNilClass_to_json(int argc, VALUE *argv, VALUE self);
86
- static VALUE mObject_to_json(int argc, VALUE *argv, VALUE self);
87
- static void State_free(void *state);
88
- static VALUE cState_s_allocate(VALUE klass);
89
- static void generate_json(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
90
- static void generate_json_object(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
91
- static void generate_json_array(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
92
- static void generate_json_string(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
93
- static void generate_json_null(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
94
- static void generate_json_false(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
95
- static void generate_json_true(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
96
- #ifdef RUBY_INTEGER_UNIFICATION
97
- static void generate_json_integer(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
98
- #endif
99
- static void generate_json_fixnum(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
100
- static void generate_json_bignum(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
101
- static void generate_json_float(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
102
- static VALUE cState_partial_generate(VALUE self, VALUE obj);
103
- static VALUE cState_generate(VALUE self, VALUE obj);
104
- static VALUE cState_from_state_s(VALUE self, VALUE opts);
105
- static VALUE cState_indent(VALUE self);
106
- static VALUE cState_indent_set(VALUE self, VALUE indent);
107
- static VALUE cState_space(VALUE self);
108
- static VALUE cState_space_set(VALUE self, VALUE space);
109
- static VALUE cState_space_before(VALUE self);
110
- static VALUE cState_space_before_set(VALUE self, VALUE space_before);
111
- static VALUE cState_object_nl(VALUE self);
112
- static VALUE cState_object_nl_set(VALUE self, VALUE object_nl);
113
- static VALUE cState_array_nl(VALUE self);
114
- static VALUE cState_array_nl_set(VALUE self, VALUE array_nl);
115
- static VALUE cState_max_nesting(VALUE self);
116
- static VALUE cState_max_nesting_set(VALUE self, VALUE depth);
117
- static VALUE cState_allow_nan_p(VALUE self);
118
- static VALUE cState_ascii_only_p(VALUE self);
119
- static VALUE cState_depth(VALUE self);
120
- static VALUE cState_depth_set(VALUE self, VALUE depth);
121
- static VALUE cState_script_safe(VALUE self);
122
- static VALUE cState_script_safe_set(VALUE self, VALUE depth);
123
- static VALUE cState_strict(VALUE self);
124
- static VALUE cState_strict_set(VALUE self, VALUE strict);
125
- static FBuffer *cState_prepare_buffer(VALUE self);
126
-
127
- static const rb_data_type_t JSON_Generator_State_type;
128
-
129
- #endif
@@ -1,60 +0,0 @@
1
- #ifndef _PARSER_H_
2
- #define _PARSER_H_
3
-
4
- #include "ruby.h"
5
-
6
- #ifndef MAYBE_UNUSED
7
- # define MAYBE_UNUSED(x) x
8
- #endif
9
-
10
- #define option_given_p(opts, key) (rb_hash_lookup2(opts, key, Qundef) != Qundef)
11
-
12
- typedef struct JSON_ParserStruct {
13
- VALUE Vsource;
14
- char *source;
15
- long len;
16
- char *memo;
17
- VALUE create_id;
18
- int max_nesting;
19
- int allow_nan;
20
- int parsing_name;
21
- int symbolize_names;
22
- int freeze;
23
- VALUE object_class;
24
- VALUE array_class;
25
- VALUE decimal_class;
26
- int create_additions;
27
- VALUE match_string;
28
- FBuffer *fbuffer;
29
- } JSON_Parser;
30
-
31
- #define GET_PARSER \
32
- GET_PARSER_INIT; \
33
- if (!json->Vsource) rb_raise(rb_eTypeError, "uninitialized instance")
34
- #define GET_PARSER_INIT \
35
- JSON_Parser *json; \
36
- TypedData_Get_Struct(self, JSON_Parser, &JSON_Parser_type, json)
37
-
38
- #define MinusInfinity "-Infinity"
39
- #define EVIL 0x666
40
-
41
- static uint32_t unescape_unicode(const unsigned char *p);
42
- static int convert_UTF32_to_UTF8(char *buf, uint32_t ch);
43
- static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting);
44
- static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting);
45
- static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *result);
46
- static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *result);
47
- static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting);
48
- static VALUE json_string_unescape(char *string, char *stringEnd, int intern, int symbolize);
49
- static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *result);
50
- static VALUE convert_encoding(VALUE source);
51
- static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self);
52
- static VALUE cParser_parse(VALUE self);
53
- static void JSON_mark(void *json);
54
- static void JSON_free(void *json);
55
- static VALUE cJSON_parser_s_allocate(VALUE klass);
56
- static VALUE cParser_source(VALUE self);
57
-
58
- static const rb_data_type_t JSON_Parser_type;
59
-
60
- #endif
@@ -1,331 +0,0 @@
1
- #frozen_string_literal: true
2
- require 'strscan'
3
-
4
- module JSON
5
- module Pure
6
- # This class implements the JSON parser that is used to parse a JSON string
7
- # into a Ruby data structure.
8
- class Parser < StringScanner
9
- STRING = /" ((?:[^\x0-\x1f"\\] |
10
- # escaped special characters:
11
- \\["\\\/bfnrt] |
12
- \\u[0-9a-fA-F]{4} |
13
- # match all but escaped special characters:
14
- \\[\x20-\x21\x23-\x2e\x30-\x5b\x5d-\x61\x63-\x65\x67-\x6d\x6f-\x71\x73\x75-\xff])*)
15
- "/nx
16
- INTEGER = /(-?0|-?[1-9]\d*)/
17
- FLOAT = /(-?
18
- (?:0|[1-9]\d*)
19
- (?:
20
- \.\d+(?i:e[+-]?\d+) |
21
- \.\d+ |
22
- (?i:e[+-]?\d+)
23
- )
24
- )/x
25
- NAN = /NaN/
26
- INFINITY = /Infinity/
27
- MINUS_INFINITY = /-Infinity/
28
- OBJECT_OPEN = /\{/
29
- OBJECT_CLOSE = /\}/
30
- ARRAY_OPEN = /\[/
31
- ARRAY_CLOSE = /\]/
32
- PAIR_DELIMITER = /:/
33
- COLLECTION_DELIMITER = /,/
34
- TRUE = /true/
35
- FALSE = /false/
36
- NULL = /null/
37
- IGNORE = %r(
38
- (?:
39
- //[^\n\r]*[\n\r]| # line comments
40
- /\* # c-style comments
41
- (?:
42
- [\s\S]*? # any char, repeated lazily
43
- )
44
- \*/ # the End of this comment
45
- |[ \t\r\n]+ # whitespaces: space, horizontal tab, lf, cr
46
- )+
47
- )mx
48
-
49
- UNPARSED = Object.new.freeze
50
-
51
- # Creates a new JSON::Pure::Parser instance for the string _source_.
52
- #
53
- # It will be configured by the _opts_ hash. _opts_ can have the following
54
- # keys:
55
- # * *max_nesting*: The maximum depth of nesting allowed in the parsed data
56
- # structures. Disable depth checking with :max_nesting => false|nil|0,
57
- # it defaults to 100.
58
- # * *allow_nan*: If set to true, allow NaN, Infinity and -Infinity in
59
- # defiance of RFC 7159 to be parsed by the Parser. This option defaults
60
- # to false.
61
- # * *freeze*: If set to true, all parsed objects will be frozen. Parsed
62
- # string will be deduplicated if possible.
63
- # * *symbolize_names*: If set to true, returns symbols for the names
64
- # (keys) in a JSON object. Otherwise strings are returned, which is
65
- # also the default. It's not possible to use this option in
66
- # conjunction with the *create_additions* option.
67
- # * *create_additions*: If set to true, the Parser creates
68
- # additions when a matching class and create_id are found. This
69
- # option defaults to false.
70
- # * *object_class*: Defaults to Hash. If another type is provided, it will be used
71
- # instead of Hash to represent JSON objects. The type must respond to
72
- # +new+ without arguments, and return an object that respond to +[]=+.
73
- # * *array_class*: Defaults to Array If another type is provided, it will be used
74
- # instead of Hash to represent JSON arrays. The type must respond to
75
- # +new+ without arguments, and return an object that respond to +<<+.
76
- # * *decimal_class*: Specifies which class to use instead of the default
77
- # (Float) when parsing decimal numbers. This class must accept a single
78
- # string argument in its constructor.
79
- def initialize(source, opts = nil)
80
- opts ||= {}
81
- source = convert_encoding source
82
- super source
83
- if !opts.key?(:max_nesting) # defaults to 100
84
- @max_nesting = 100
85
- elsif opts[:max_nesting]
86
- @max_nesting = opts[:max_nesting]
87
- else
88
- @max_nesting = 0
89
- end
90
- @allow_nan = !!opts[:allow_nan]
91
- @symbolize_names = !!opts[:symbolize_names]
92
- @freeze = !!opts[:freeze]
93
- if opts.key?(:create_additions)
94
- @create_additions = !!opts[:create_additions]
95
- else
96
- @create_additions = false
97
- end
98
- @symbolize_names && @create_additions and raise ArgumentError,
99
- 'options :symbolize_names and :create_additions cannot be used '\
100
- 'in conjunction'
101
- @create_id = @create_additions ? JSON.create_id : nil
102
- @object_class = opts[:object_class] || Hash
103
- @array_class = opts[:array_class] || Array
104
- @decimal_class = opts[:decimal_class]
105
- @match_string = opts[:match_string]
106
- end
107
-
108
- alias source string
109
-
110
- def reset
111
- super
112
- @current_nesting = 0
113
- end
114
-
115
- # Parses the current JSON string _source_ and returns the
116
- # complete data structure as a result.
117
- def parse
118
- reset
119
- obj = nil
120
- while !eos? && skip(IGNORE) do end
121
- if eos?
122
- raise ParserError, "source is not valid JSON!"
123
- else
124
- obj = parse_value
125
- UNPARSED.equal?(obj) and raise ParserError,
126
- "source is not valid JSON!"
127
- obj.freeze if @freeze
128
- end
129
- while !eos? && skip(IGNORE) do end
130
- eos? or raise ParserError, "source is not valid JSON!"
131
- obj
132
- end
133
-
134
- private
135
-
136
- def convert_encoding(source)
137
- if source.respond_to?(:to_str)
138
- source = source.to_str
139
- else
140
- raise TypeError,
141
- "#{source.inspect} is not like a string"
142
- end
143
- if source.encoding != ::Encoding::ASCII_8BIT
144
- source = source.encode(::Encoding::UTF_8)
145
- source.force_encoding(::Encoding::ASCII_8BIT)
146
- end
147
- source
148
- end
149
-
150
- # Unescape characters in strings.
151
- UNESCAPE_MAP = Hash.new { |h, k| h[k] = k.chr }
152
- UNESCAPE_MAP.update({
153
- ?" => '"',
154
- ?\\ => '\\',
155
- ?/ => '/',
156
- ?b => "\b",
157
- ?f => "\f",
158
- ?n => "\n",
159
- ?r => "\r",
160
- ?t => "\t",
161
- ?u => nil,
162
- })
163
-
164
- STR_UMINUS = ''.respond_to?(:-@)
165
- def parse_string
166
- if scan(STRING)
167
- return '' if self[1].empty?
168
- string = self[1].gsub(%r((?:\\[\\bfnrt"/]|(?:\\u(?:[A-Fa-f\d]{4}))+|\\[\x20-\xff]))n) do |c|
169
- if u = UNESCAPE_MAP[$&[1]]
170
- u
171
- else # \uXXXX
172
- bytes = ''.b
173
- i = 0
174
- while c[6 * i] == ?\\ && c[6 * i + 1] == ?u
175
- bytes << c[6 * i + 2, 2].to_i(16) << c[6 * i + 4, 2].to_i(16)
176
- i += 1
177
- end
178
- bytes.encode(Encoding::UTF_8, Encoding::UTF_16BE).force_encoding(::Encoding::BINARY)
179
- end
180
- end
181
- string.force_encoding(::Encoding::UTF_8)
182
-
183
- if @freeze
184
- if STR_UMINUS
185
- string = -string
186
- else
187
- string.freeze
188
- end
189
- end
190
-
191
- if @create_additions and @match_string
192
- for (regexp, klass) in @match_string
193
- klass.json_creatable? or next
194
- string =~ regexp and return klass.json_create(string)
195
- end
196
- end
197
- string
198
- else
199
- UNPARSED
200
- end
201
- rescue => e
202
- raise ParserError, "Caught #{e.class} at '#{peek(20)}': #{e}"
203
- end
204
-
205
- def parse_value
206
- case
207
- when scan(FLOAT)
208
- if @decimal_class then
209
- if @decimal_class == BigDecimal then
210
- BigDecimal(self[1])
211
- else
212
- @decimal_class.new(self[1]) || Float(self[1])
213
- end
214
- else
215
- Float(self[1])
216
- end
217
- when scan(INTEGER)
218
- Integer(self[1])
219
- when scan(TRUE)
220
- true
221
- when scan(FALSE)
222
- false
223
- when scan(NULL)
224
- nil
225
- when !UNPARSED.equal?(string = parse_string)
226
- string
227
- when scan(ARRAY_OPEN)
228
- @current_nesting += 1
229
- ary = parse_array
230
- @current_nesting -= 1
231
- ary
232
- when scan(OBJECT_OPEN)
233
- @current_nesting += 1
234
- obj = parse_object
235
- @current_nesting -= 1
236
- obj
237
- when @allow_nan && scan(NAN)
238
- NaN
239
- when @allow_nan && scan(INFINITY)
240
- Infinity
241
- when @allow_nan && scan(MINUS_INFINITY)
242
- MinusInfinity
243
- else
244
- UNPARSED
245
- end
246
- end
247
-
248
- def parse_array
249
- raise NestingError, "nesting of #@current_nesting is too deep" if
250
- @max_nesting.nonzero? && @current_nesting > @max_nesting
251
- result = @array_class.new
252
- delim = false
253
- loop do
254
- case
255
- when eos?
256
- raise ParserError, "unexpected end of string while parsing array"
257
- when !UNPARSED.equal?(value = parse_value)
258
- delim = false
259
- result << value
260
- skip(IGNORE)
261
- if scan(COLLECTION_DELIMITER)
262
- delim = true
263
- elsif match?(ARRAY_CLOSE)
264
- ;
265
- else
266
- raise ParserError, "expected ',' or ']' in array at '#{peek(20)}'!"
267
- end
268
- when scan(ARRAY_CLOSE)
269
- if delim
270
- raise ParserError, "expected next element in array at '#{peek(20)}'!"
271
- end
272
- break
273
- when skip(IGNORE)
274
- ;
275
- else
276
- raise ParserError, "unexpected token in array at '#{peek(20)}'!"
277
- end
278
- end
279
- result
280
- end
281
-
282
- def parse_object
283
- raise NestingError, "nesting of #@current_nesting is too deep" if
284
- @max_nesting.nonzero? && @current_nesting > @max_nesting
285
- result = @object_class.new
286
- delim = false
287
- loop do
288
- case
289
- when eos?
290
- raise ParserError, "unexpected end of string while parsing object"
291
- when !UNPARSED.equal?(string = parse_string)
292
- skip(IGNORE)
293
- unless scan(PAIR_DELIMITER)
294
- raise ParserError, "expected ':' in object at '#{peek(20)}'!"
295
- end
296
- skip(IGNORE)
297
- unless UNPARSED.equal?(value = parse_value)
298
- result[@symbolize_names ? string.to_sym : string] = value
299
- delim = false
300
- skip(IGNORE)
301
- if scan(COLLECTION_DELIMITER)
302
- delim = true
303
- elsif match?(OBJECT_CLOSE)
304
- ;
305
- else
306
- raise ParserError, "expected ',' or '}' in object at '#{peek(20)}'!"
307
- end
308
- else
309
- raise ParserError, "expected value in object at '#{peek(20)}'!"
310
- end
311
- when scan(OBJECT_CLOSE)
312
- if delim
313
- raise ParserError, "expected next name, value pair in object at '#{peek(20)}'!"
314
- end
315
- if @create_additions and klassname = result[@create_id]
316
- klass = JSON.deep_const_get klassname
317
- break unless klass and klass.json_creatable?
318
- result = klass.json_create(result)
319
- end
320
- break
321
- when skip(IGNORE)
322
- ;
323
- else
324
- raise ParserError, "unexpected token in object at '#{peek(20)}'!"
325
- end
326
- end
327
- result
328
- end
329
- end
330
- end
331
- end
data/lib/json/pure.rb DELETED
@@ -1,16 +0,0 @@
1
- # frozen_string_literal: true
2
- require 'json/common'
3
-
4
- module JSON
5
- # This module holds all the modules/classes that implement JSON's
6
- # functionality in pure ruby.
7
- module Pure
8
- require 'json/pure/parser'
9
- require 'json/pure/generator'
10
- $DEBUG and warn "Using Pure library for JSON."
11
- JSON.parser = Parser
12
- JSON.generator = Generator
13
- end
14
-
15
- JSON_LOADED = true unless defined?(::JSON::JSON_LOADED)
16
- end