json 2.7.2 → 2.8.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/BSDL +22 -0
- data/CHANGES.md +73 -17
- data/LEGAL +60 -0
- data/README.md +15 -236
- data/ext/json/ext/fbuffer/fbuffer.h +76 -79
- data/ext/json/ext/generator/extconf.rb +8 -2
- data/ext/json/ext/generator/generator.c +776 -816
- data/ext/json/ext/parser/extconf.rb +7 -27
- data/ext/json/ext/parser/parser.c +1697 -670
- data/ext/json/ext/parser/parser.rl +832 -338
- data/json.gemspec +45 -49
- data/lib/json/add/bigdecimal.rb +2 -2
- data/lib/json/add/complex.rb +1 -1
- data/lib/json/add/core.rb +1 -1
- data/lib/json/add/date.rb +1 -1
- data/lib/json/add/date_time.rb +1 -1
- data/lib/json/add/exception.rb +1 -1
- data/lib/json/add/ostruct.rb +1 -1
- data/lib/json/add/range.rb +1 -1
- data/lib/json/add/rational.rb +1 -1
- data/lib/json/add/regexp.rb +1 -1
- data/lib/json/add/struct.rb +1 -1
- data/lib/json/add/symbol.rb +1 -2
- data/lib/json/add/time.rb +3 -10
- data/lib/json/common.rb +273 -95
- data/lib/json/ext/generator/state.rb +105 -0
- data/lib/json/ext.rb +13 -5
- data/lib/json/generic_object.rb +1 -1
- data/lib/json/{pure → truffle_ruby}/generator.rb +228 -120
- data/lib/json/version.rb +3 -7
- data/lib/json.rb +16 -21
- metadata +19 -21
- data/ext/json/ext/generator/depend +0 -1
- data/ext/json/ext/generator/generator.h +0 -177
- data/ext/json/ext/parser/depend +0 -1
- data/ext/json/ext/parser/parser.h +0 -96
- data/ext/json/extconf.rb +0 -3
- data/lib/json/pure/parser.rb +0 -337
- data/lib/json/pure.rb +0 -15
- /data/{LICENSE → COPYING} +0 -0
@@ -1,177 +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
|
-
#ifdef HAVE_RUBY_RE_H
|
10
|
-
#include "ruby/re.h"
|
11
|
-
#else
|
12
|
-
#include "re.h"
|
13
|
-
#endif
|
14
|
-
|
15
|
-
#ifndef rb_intern_str
|
16
|
-
#define rb_intern_str(string) SYM2ID(rb_str_intern(string))
|
17
|
-
#endif
|
18
|
-
|
19
|
-
#ifndef rb_obj_instance_variables
|
20
|
-
#define rb_obj_instance_variables(object) rb_funcall(object, rb_intern("instance_variables"), 0)
|
21
|
-
#endif
|
22
|
-
|
23
|
-
#define option_given_p(opts, key) RTEST(rb_funcall(opts, i_key_p, 1, key))
|
24
|
-
|
25
|
-
/* unicode definitions */
|
26
|
-
|
27
|
-
#define UNI_STRICT_CONVERSION 1
|
28
|
-
|
29
|
-
typedef unsigned long UTF32; /* at least 32 bits */
|
30
|
-
typedef unsigned short UTF16; /* at least 16 bits */
|
31
|
-
typedef unsigned char UTF8; /* typically 8 bits */
|
32
|
-
|
33
|
-
#define UNI_REPLACEMENT_CHAR (UTF32)0x0000FFFD
|
34
|
-
#define UNI_MAX_BMP (UTF32)0x0000FFFF
|
35
|
-
#define UNI_MAX_UTF16 (UTF32)0x0010FFFF
|
36
|
-
#define UNI_MAX_UTF32 (UTF32)0x7FFFFFFF
|
37
|
-
#define UNI_MAX_LEGAL_UTF32 (UTF32)0x0010FFFF
|
38
|
-
|
39
|
-
#define UNI_SUR_HIGH_START (UTF32)0xD800
|
40
|
-
#define UNI_SUR_HIGH_END (UTF32)0xDBFF
|
41
|
-
#define UNI_SUR_LOW_START (UTF32)0xDC00
|
42
|
-
#define UNI_SUR_LOW_END (UTF32)0xDFFF
|
43
|
-
|
44
|
-
static const int halfShift = 10; /* used for shifting by 10 bits */
|
45
|
-
|
46
|
-
static const UTF32 halfBase = 0x0010000UL;
|
47
|
-
static const UTF32 halfMask = 0x3FFUL;
|
48
|
-
|
49
|
-
static unsigned char isLegalUTF8(const UTF8 *source, unsigned long length);
|
50
|
-
static void unicode_escape(char *buf, UTF16 character);
|
51
|
-
static void unicode_escape_to_buffer(FBuffer *buffer, char buf[6], UTF16 character);
|
52
|
-
static void convert_UTF8_to_JSON_ASCII(FBuffer *buffer, VALUE string, char script_safe);
|
53
|
-
static void convert_UTF8_to_JSON(FBuffer *buffer, VALUE string, char script_safe);
|
54
|
-
static char *fstrndup(const char *ptr, unsigned long len);
|
55
|
-
|
56
|
-
/* ruby api and some helpers */
|
57
|
-
|
58
|
-
typedef struct JSON_Generator_StateStruct {
|
59
|
-
char *indent;
|
60
|
-
long indent_len;
|
61
|
-
char *space;
|
62
|
-
long space_len;
|
63
|
-
char *space_before;
|
64
|
-
long space_before_len;
|
65
|
-
char *object_nl;
|
66
|
-
long object_nl_len;
|
67
|
-
char *array_nl;
|
68
|
-
long array_nl_len;
|
69
|
-
FBuffer *array_delim;
|
70
|
-
FBuffer *object_delim;
|
71
|
-
FBuffer *object_delim2;
|
72
|
-
long max_nesting;
|
73
|
-
char allow_nan;
|
74
|
-
char ascii_only;
|
75
|
-
char script_safe;
|
76
|
-
char strict;
|
77
|
-
long depth;
|
78
|
-
long buffer_initial_length;
|
79
|
-
} JSON_Generator_State;
|
80
|
-
|
81
|
-
#define GET_STATE_TO(self, state) \
|
82
|
-
TypedData_Get_Struct(self, JSON_Generator_State, &JSON_Generator_State_type, state)
|
83
|
-
|
84
|
-
#define GET_STATE(self) \
|
85
|
-
JSON_Generator_State *state; \
|
86
|
-
GET_STATE_TO(self, state)
|
87
|
-
|
88
|
-
#define GENERATE_JSON(type) \
|
89
|
-
FBuffer *buffer; \
|
90
|
-
VALUE Vstate; \
|
91
|
-
JSON_Generator_State *state; \
|
92
|
-
\
|
93
|
-
rb_scan_args(argc, argv, "01", &Vstate); \
|
94
|
-
Vstate = cState_from_state_s(cState, Vstate); \
|
95
|
-
TypedData_Get_Struct(Vstate, JSON_Generator_State, &JSON_Generator_State_type, state); \
|
96
|
-
buffer = cState_prepare_buffer(Vstate); \
|
97
|
-
generate_json_##type(buffer, Vstate, state, self); \
|
98
|
-
return fbuffer_to_s(buffer)
|
99
|
-
|
100
|
-
static VALUE mHash_to_json(int argc, VALUE *argv, VALUE self);
|
101
|
-
static VALUE mArray_to_json(int argc, VALUE *argv, VALUE self);
|
102
|
-
#ifdef RUBY_INTEGER_UNIFICATION
|
103
|
-
static VALUE mInteger_to_json(int argc, VALUE *argv, VALUE self);
|
104
|
-
#else
|
105
|
-
static VALUE mFixnum_to_json(int argc, VALUE *argv, VALUE self);
|
106
|
-
static VALUE mBignum_to_json(int argc, VALUE *argv, VALUE self);
|
107
|
-
#endif
|
108
|
-
static VALUE mFloat_to_json(int argc, VALUE *argv, VALUE self);
|
109
|
-
static VALUE mString_included_s(VALUE self, VALUE modul);
|
110
|
-
static VALUE mString_to_json(int argc, VALUE *argv, VALUE self);
|
111
|
-
static VALUE mString_to_json_raw_object(VALUE self);
|
112
|
-
static VALUE mString_to_json_raw(int argc, VALUE *argv, VALUE self);
|
113
|
-
static VALUE mString_Extend_json_create(VALUE self, VALUE o);
|
114
|
-
static VALUE mTrueClass_to_json(int argc, VALUE *argv, VALUE self);
|
115
|
-
static VALUE mFalseClass_to_json(int argc, VALUE *argv, VALUE self);
|
116
|
-
static VALUE mNilClass_to_json(int argc, VALUE *argv, VALUE self);
|
117
|
-
static VALUE mObject_to_json(int argc, VALUE *argv, VALUE self);
|
118
|
-
static void State_free(void *state);
|
119
|
-
static VALUE cState_s_allocate(VALUE klass);
|
120
|
-
static VALUE cState_configure(VALUE self, VALUE opts);
|
121
|
-
static VALUE cState_to_h(VALUE self);
|
122
|
-
static void generate_json(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
|
123
|
-
static void generate_json_object(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
|
124
|
-
static void generate_json_array(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
|
125
|
-
static void generate_json_string(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
|
126
|
-
static void generate_json_null(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
|
127
|
-
static void generate_json_false(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
|
128
|
-
static void generate_json_true(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
|
129
|
-
#ifdef RUBY_INTEGER_UNIFICATION
|
130
|
-
static void generate_json_integer(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
|
131
|
-
#endif
|
132
|
-
static void generate_json_fixnum(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
|
133
|
-
static void generate_json_bignum(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
|
134
|
-
static void generate_json_float(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
|
135
|
-
static VALUE cState_partial_generate(VALUE self, VALUE obj);
|
136
|
-
static VALUE cState_generate(VALUE self, VALUE obj);
|
137
|
-
static VALUE cState_initialize(int argc, VALUE *argv, VALUE self);
|
138
|
-
static VALUE cState_from_state_s(VALUE self, VALUE opts);
|
139
|
-
static VALUE cState_indent(VALUE self);
|
140
|
-
static VALUE cState_indent_set(VALUE self, VALUE indent);
|
141
|
-
static VALUE cState_space(VALUE self);
|
142
|
-
static VALUE cState_space_set(VALUE self, VALUE space);
|
143
|
-
static VALUE cState_space_before(VALUE self);
|
144
|
-
static VALUE cState_space_before_set(VALUE self, VALUE space_before);
|
145
|
-
static VALUE cState_object_nl(VALUE self);
|
146
|
-
static VALUE cState_object_nl_set(VALUE self, VALUE object_nl);
|
147
|
-
static VALUE cState_array_nl(VALUE self);
|
148
|
-
static VALUE cState_array_nl_set(VALUE self, VALUE array_nl);
|
149
|
-
static VALUE cState_max_nesting(VALUE self);
|
150
|
-
static VALUE cState_max_nesting_set(VALUE self, VALUE depth);
|
151
|
-
static VALUE cState_allow_nan_p(VALUE self);
|
152
|
-
static VALUE cState_ascii_only_p(VALUE self);
|
153
|
-
static VALUE cState_depth(VALUE self);
|
154
|
-
static VALUE cState_depth_set(VALUE self, VALUE depth);
|
155
|
-
static VALUE cState_script_safe(VALUE self);
|
156
|
-
static VALUE cState_script_safe_set(VALUE self, VALUE depth);
|
157
|
-
static VALUE cState_strict(VALUE self);
|
158
|
-
static VALUE cState_strict_set(VALUE self, VALUE strict);
|
159
|
-
static FBuffer *cState_prepare_buffer(VALUE self);
|
160
|
-
#ifndef ZALLOC
|
161
|
-
#define ZALLOC(type) ((type *)ruby_zalloc(sizeof(type)))
|
162
|
-
static inline void *ruby_zalloc(size_t n)
|
163
|
-
{
|
164
|
-
void *p = ruby_xmalloc(n);
|
165
|
-
memset(p, 0, n);
|
166
|
-
return p;
|
167
|
-
}
|
168
|
-
#endif
|
169
|
-
#ifdef TypedData_Make_Struct
|
170
|
-
static const rb_data_type_t JSON_Generator_State_type;
|
171
|
-
#define NEW_TYPEDDATA_WRAPPER 1
|
172
|
-
#else
|
173
|
-
#define TypedData_Make_Struct(klass, type, ignore, json) Data_Make_Struct(klass, type, NULL, State_free, json)
|
174
|
-
#define TypedData_Get_Struct(self, JSON_Generator_State, ignore, json) Data_Get_Struct(self, JSON_Generator_State, json)
|
175
|
-
#endif
|
176
|
-
|
177
|
-
#endif
|
data/ext/json/ext/parser/depend
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
parser.o: parser.c parser.h $(srcdir)/../fbuffer/fbuffer.h
|
@@ -1,96 +0,0 @@
|
|
1
|
-
#ifndef _PARSER_H_
|
2
|
-
#define _PARSER_H_
|
3
|
-
|
4
|
-
#include "ruby.h"
|
5
|
-
|
6
|
-
#ifndef HAVE_RUBY_RE_H
|
7
|
-
#include "re.h"
|
8
|
-
#endif
|
9
|
-
|
10
|
-
#ifdef HAVE_RUBY_ST_H
|
11
|
-
#include "ruby/st.h"
|
12
|
-
#else
|
13
|
-
#include "st.h"
|
14
|
-
#endif
|
15
|
-
|
16
|
-
#ifndef MAYBE_UNUSED
|
17
|
-
# define MAYBE_UNUSED(x) x
|
18
|
-
#endif
|
19
|
-
|
20
|
-
#define option_given_p(opts, key) RTEST(rb_funcall(opts, i_key_p, 1, key))
|
21
|
-
|
22
|
-
/* unicode */
|
23
|
-
|
24
|
-
typedef unsigned long UTF32; /* at least 32 bits */
|
25
|
-
typedef unsigned short UTF16; /* at least 16 bits */
|
26
|
-
typedef unsigned char UTF8; /* typically 8 bits */
|
27
|
-
|
28
|
-
#define UNI_REPLACEMENT_CHAR (UTF32)0x0000FFFD
|
29
|
-
#define UNI_SUR_HIGH_START (UTF32)0xD800
|
30
|
-
#define UNI_SUR_HIGH_END (UTF32)0xDBFF
|
31
|
-
#define UNI_SUR_LOW_START (UTF32)0xDC00
|
32
|
-
#define UNI_SUR_LOW_END (UTF32)0xDFFF
|
33
|
-
|
34
|
-
typedef struct JSON_ParserStruct {
|
35
|
-
VALUE Vsource;
|
36
|
-
char *source;
|
37
|
-
long len;
|
38
|
-
char *memo;
|
39
|
-
VALUE create_id;
|
40
|
-
int max_nesting;
|
41
|
-
int allow_nan;
|
42
|
-
int parsing_name;
|
43
|
-
int symbolize_names;
|
44
|
-
int freeze;
|
45
|
-
VALUE object_class;
|
46
|
-
VALUE array_class;
|
47
|
-
VALUE decimal_class;
|
48
|
-
int create_additions;
|
49
|
-
VALUE match_string;
|
50
|
-
FBuffer *fbuffer;
|
51
|
-
} JSON_Parser;
|
52
|
-
|
53
|
-
#define GET_PARSER \
|
54
|
-
GET_PARSER_INIT; \
|
55
|
-
if (!json->Vsource) rb_raise(rb_eTypeError, "uninitialized instance")
|
56
|
-
#define GET_PARSER_INIT \
|
57
|
-
JSON_Parser *json; \
|
58
|
-
TypedData_Get_Struct(self, JSON_Parser, &JSON_Parser_type, json)
|
59
|
-
|
60
|
-
#define MinusInfinity "-Infinity"
|
61
|
-
#define EVIL 0x666
|
62
|
-
|
63
|
-
static UTF32 unescape_unicode(const unsigned char *p);
|
64
|
-
static int convert_UTF32_to_UTF8(char *buf, UTF32 ch);
|
65
|
-
static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting);
|
66
|
-
static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting);
|
67
|
-
static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *result);
|
68
|
-
static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *result);
|
69
|
-
static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting);
|
70
|
-
static VALUE json_string_unescape(char *string, char *stringEnd, int intern, int symbolize);
|
71
|
-
static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *result);
|
72
|
-
static VALUE convert_encoding(VALUE source);
|
73
|
-
static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self);
|
74
|
-
static VALUE cParser_parse(VALUE self);
|
75
|
-
static void JSON_mark(void *json);
|
76
|
-
static void JSON_free(void *json);
|
77
|
-
static VALUE cJSON_parser_s_allocate(VALUE klass);
|
78
|
-
static VALUE cParser_source(VALUE self);
|
79
|
-
#ifndef ZALLOC
|
80
|
-
#define ZALLOC(type) ((type *)ruby_zalloc(sizeof(type)))
|
81
|
-
static inline void *ruby_zalloc(size_t n)
|
82
|
-
{
|
83
|
-
void *p = ruby_xmalloc(n);
|
84
|
-
memset(p, 0, n);
|
85
|
-
return p;
|
86
|
-
}
|
87
|
-
#endif
|
88
|
-
#ifdef TypedData_Make_Struct
|
89
|
-
static const rb_data_type_t JSON_Parser_type;
|
90
|
-
#define NEW_TYPEDDATA_WRAPPER 1
|
91
|
-
#else
|
92
|
-
#define TypedData_Make_Struct(klass, type, ignore, json) Data_Make_Struct(klass, type, NULL, JSON_free, json)
|
93
|
-
#define TypedData_Get_Struct(self, JSON_Parser, ignore, json) Data_Get_Struct(self, JSON_Parser, json)
|
94
|
-
#endif
|
95
|
-
|
96
|
-
#endif
|
data/ext/json/extconf.rb
DELETED
data/lib/json/pure/parser.rb
DELETED
@@ -1,337 +0,0 @@
|
|
1
|
-
#frozen_string_literal: false
|
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
|
-
[^*/]| # normal chars
|
43
|
-
/[^*]| # slashes that do not start a nested comment
|
44
|
-
\*[^/]| # asterisks that do not end this comment
|
45
|
-
/(?=\*/) # single slash before this comment's end
|
46
|
-
)*
|
47
|
-
\*/ # the End of this comment
|
48
|
-
|[ \t\r\n]+ # whitespaces: space, horizontal tab, lf, cr
|
49
|
-
)+
|
50
|
-
)mx
|
51
|
-
|
52
|
-
UNPARSED = Object.new.freeze
|
53
|
-
|
54
|
-
# Creates a new JSON::Pure::Parser instance for the string _source_.
|
55
|
-
#
|
56
|
-
# It will be configured by the _opts_ hash. _opts_ can have the following
|
57
|
-
# keys:
|
58
|
-
# * *max_nesting*: The maximum depth of nesting allowed in the parsed data
|
59
|
-
# structures. Disable depth checking with :max_nesting => false|nil|0,
|
60
|
-
# it defaults to 100.
|
61
|
-
# * *allow_nan*: If set to true, allow NaN, Infinity and -Infinity in
|
62
|
-
# defiance of RFC 7159 to be parsed by the Parser. This option defaults
|
63
|
-
# to false.
|
64
|
-
# * *freeze*: If set to true, all parsed objects will be frozen. Parsed
|
65
|
-
# string will be deduplicated if possible.
|
66
|
-
# * *symbolize_names*: If set to true, returns symbols for the names
|
67
|
-
# (keys) in a JSON object. Otherwise strings are returned, which is
|
68
|
-
# also the default. It's not possible to use this option in
|
69
|
-
# conjunction with the *create_additions* option.
|
70
|
-
# * *create_additions*: If set to true, the Parser creates
|
71
|
-
# additions when a matching class and create_id are found. This
|
72
|
-
# option defaults to false.
|
73
|
-
# * *object_class*: Defaults to Hash
|
74
|
-
# * *array_class*: Defaults to Array
|
75
|
-
# * *decimal_class*: Specifies which class to use instead of the default
|
76
|
-
# (Float) when parsing decimal numbers. This class must accept a single
|
77
|
-
# string argument in its constructor.
|
78
|
-
def initialize(source, opts = {})
|
79
|
-
opts ||= {}
|
80
|
-
source = convert_encoding source
|
81
|
-
super source
|
82
|
-
if !opts.key?(:max_nesting) # defaults to 100
|
83
|
-
@max_nesting = 100
|
84
|
-
elsif opts[:max_nesting]
|
85
|
-
@max_nesting = opts[:max_nesting]
|
86
|
-
else
|
87
|
-
@max_nesting = 0
|
88
|
-
end
|
89
|
-
@allow_nan = !!opts[:allow_nan]
|
90
|
-
@symbolize_names = !!opts[:symbolize_names]
|
91
|
-
@freeze = !!opts[:freeze]
|
92
|
-
if opts.key?(:create_additions)
|
93
|
-
@create_additions = !!opts[:create_additions]
|
94
|
-
else
|
95
|
-
@create_additions = false
|
96
|
-
end
|
97
|
-
@symbolize_names && @create_additions and raise ArgumentError,
|
98
|
-
'options :symbolize_names and :create_additions cannot be used '\
|
99
|
-
'in conjunction'
|
100
|
-
@create_id = @create_additions ? JSON.create_id : nil
|
101
|
-
@object_class = opts[:object_class] || Hash
|
102
|
-
@array_class = opts[:array_class] || Array
|
103
|
-
@decimal_class = opts[:decimal_class]
|
104
|
-
@match_string = opts[:match_string]
|
105
|
-
end
|
106
|
-
|
107
|
-
alias source string
|
108
|
-
|
109
|
-
def reset
|
110
|
-
super
|
111
|
-
@current_nesting = 0
|
112
|
-
end
|
113
|
-
|
114
|
-
# Parses the current JSON string _source_ and returns the
|
115
|
-
# complete data structure as a result.
|
116
|
-
def parse
|
117
|
-
reset
|
118
|
-
obj = nil
|
119
|
-
while !eos? && skip(IGNORE) do end
|
120
|
-
if eos?
|
121
|
-
raise ParserError, "source is not valid JSON!"
|
122
|
-
else
|
123
|
-
obj = parse_value
|
124
|
-
UNPARSED.equal?(obj) and raise ParserError,
|
125
|
-
"source is not valid JSON!"
|
126
|
-
obj.freeze if @freeze
|
127
|
-
end
|
128
|
-
while !eos? && skip(IGNORE) do end
|
129
|
-
eos? or raise ParserError, "source is not valid JSON!"
|
130
|
-
obj
|
131
|
-
end
|
132
|
-
|
133
|
-
private
|
134
|
-
|
135
|
-
def convert_encoding(source)
|
136
|
-
if source.respond_to?(:to_str)
|
137
|
-
source = source.to_str
|
138
|
-
else
|
139
|
-
raise TypeError,
|
140
|
-
"#{source.inspect} is not like a string"
|
141
|
-
end
|
142
|
-
if source.encoding != ::Encoding::ASCII_8BIT
|
143
|
-
source = source.encode(::Encoding::UTF_8)
|
144
|
-
source.force_encoding(::Encoding::ASCII_8BIT)
|
145
|
-
end
|
146
|
-
source
|
147
|
-
end
|
148
|
-
|
149
|
-
# Unescape characters in strings.
|
150
|
-
UNESCAPE_MAP = Hash.new { |h, k| h[k] = k.chr }
|
151
|
-
UNESCAPE_MAP.update({
|
152
|
-
?" => '"',
|
153
|
-
?\\ => '\\',
|
154
|
-
?/ => '/',
|
155
|
-
?b => "\b",
|
156
|
-
?f => "\f",
|
157
|
-
?n => "\n",
|
158
|
-
?r => "\r",
|
159
|
-
?t => "\t",
|
160
|
-
?u => nil,
|
161
|
-
})
|
162
|
-
|
163
|
-
EMPTY_8BIT_STRING = ''
|
164
|
-
if ::String.method_defined?(:encode)
|
165
|
-
EMPTY_8BIT_STRING.force_encoding Encoding::ASCII_8BIT
|
166
|
-
end
|
167
|
-
|
168
|
-
STR_UMINUS = ''.respond_to?(:-@)
|
169
|
-
def parse_string
|
170
|
-
if scan(STRING)
|
171
|
-
return '' if self[1].empty?
|
172
|
-
string = self[1].gsub(%r((?:\\[\\bfnrt"/]|(?:\\u(?:[A-Fa-f\d]{4}))+|\\[\x20-\xff]))n) do |c|
|
173
|
-
if u = UNESCAPE_MAP[$&[1]]
|
174
|
-
u
|
175
|
-
else # \uXXXX
|
176
|
-
bytes = EMPTY_8BIT_STRING.dup
|
177
|
-
i = 0
|
178
|
-
while c[6 * i] == ?\\ && c[6 * i + 1] == ?u
|
179
|
-
bytes << c[6 * i + 2, 2].to_i(16) << c[6 * i + 4, 2].to_i(16)
|
180
|
-
i += 1
|
181
|
-
end
|
182
|
-
JSON.iconv('utf-8', 'utf-16be', bytes).force_encoding(::Encoding::ASCII_8BIT)
|
183
|
-
end
|
184
|
-
end
|
185
|
-
if string.respond_to?(:force_encoding)
|
186
|
-
string.force_encoding(::Encoding::UTF_8)
|
187
|
-
end
|
188
|
-
|
189
|
-
if @freeze
|
190
|
-
if STR_UMINUS
|
191
|
-
string = -string
|
192
|
-
else
|
193
|
-
string.freeze
|
194
|
-
end
|
195
|
-
end
|
196
|
-
|
197
|
-
if @create_additions and @match_string
|
198
|
-
for (regexp, klass) in @match_string
|
199
|
-
klass.json_creatable? or next
|
200
|
-
string =~ regexp and return klass.json_create(string)
|
201
|
-
end
|
202
|
-
end
|
203
|
-
string
|
204
|
-
else
|
205
|
-
UNPARSED
|
206
|
-
end
|
207
|
-
rescue => e
|
208
|
-
raise ParserError, "Caught #{e.class} at '#{peek(20)}': #{e}"
|
209
|
-
end
|
210
|
-
|
211
|
-
def parse_value
|
212
|
-
case
|
213
|
-
when scan(FLOAT)
|
214
|
-
if @decimal_class then
|
215
|
-
if @decimal_class == BigDecimal then
|
216
|
-
BigDecimal(self[1])
|
217
|
-
else
|
218
|
-
@decimal_class.new(self[1]) || Float(self[1])
|
219
|
-
end
|
220
|
-
else
|
221
|
-
Float(self[1])
|
222
|
-
end
|
223
|
-
when scan(INTEGER)
|
224
|
-
Integer(self[1])
|
225
|
-
when scan(TRUE)
|
226
|
-
true
|
227
|
-
when scan(FALSE)
|
228
|
-
false
|
229
|
-
when scan(NULL)
|
230
|
-
nil
|
231
|
-
when !UNPARSED.equal?(string = parse_string)
|
232
|
-
string
|
233
|
-
when scan(ARRAY_OPEN)
|
234
|
-
@current_nesting += 1
|
235
|
-
ary = parse_array
|
236
|
-
@current_nesting -= 1
|
237
|
-
ary
|
238
|
-
when scan(OBJECT_OPEN)
|
239
|
-
@current_nesting += 1
|
240
|
-
obj = parse_object
|
241
|
-
@current_nesting -= 1
|
242
|
-
obj
|
243
|
-
when @allow_nan && scan(NAN)
|
244
|
-
NaN
|
245
|
-
when @allow_nan && scan(INFINITY)
|
246
|
-
Infinity
|
247
|
-
when @allow_nan && scan(MINUS_INFINITY)
|
248
|
-
MinusInfinity
|
249
|
-
else
|
250
|
-
UNPARSED
|
251
|
-
end
|
252
|
-
end
|
253
|
-
|
254
|
-
def parse_array
|
255
|
-
raise NestingError, "nesting of #@current_nesting is too deep" if
|
256
|
-
@max_nesting.nonzero? && @current_nesting > @max_nesting
|
257
|
-
result = @array_class.new
|
258
|
-
delim = false
|
259
|
-
loop do
|
260
|
-
case
|
261
|
-
when eos?
|
262
|
-
raise ParserError, "unexpected end of string while parsing array"
|
263
|
-
when !UNPARSED.equal?(value = parse_value)
|
264
|
-
delim = false
|
265
|
-
result << value
|
266
|
-
skip(IGNORE)
|
267
|
-
if scan(COLLECTION_DELIMITER)
|
268
|
-
delim = true
|
269
|
-
elsif match?(ARRAY_CLOSE)
|
270
|
-
;
|
271
|
-
else
|
272
|
-
raise ParserError, "expected ',' or ']' in array at '#{peek(20)}'!"
|
273
|
-
end
|
274
|
-
when scan(ARRAY_CLOSE)
|
275
|
-
if delim
|
276
|
-
raise ParserError, "expected next element in array at '#{peek(20)}'!"
|
277
|
-
end
|
278
|
-
break
|
279
|
-
when skip(IGNORE)
|
280
|
-
;
|
281
|
-
else
|
282
|
-
raise ParserError, "unexpected token in array at '#{peek(20)}'!"
|
283
|
-
end
|
284
|
-
end
|
285
|
-
result
|
286
|
-
end
|
287
|
-
|
288
|
-
def parse_object
|
289
|
-
raise NestingError, "nesting of #@current_nesting is too deep" if
|
290
|
-
@max_nesting.nonzero? && @current_nesting > @max_nesting
|
291
|
-
result = @object_class.new
|
292
|
-
delim = false
|
293
|
-
loop do
|
294
|
-
case
|
295
|
-
when eos?
|
296
|
-
raise ParserError, "unexpected end of string while parsing object"
|
297
|
-
when !UNPARSED.equal?(string = parse_string)
|
298
|
-
skip(IGNORE)
|
299
|
-
unless scan(PAIR_DELIMITER)
|
300
|
-
raise ParserError, "expected ':' in object at '#{peek(20)}'!"
|
301
|
-
end
|
302
|
-
skip(IGNORE)
|
303
|
-
unless UNPARSED.equal?(value = parse_value)
|
304
|
-
result[@symbolize_names ? string.to_sym : string] = value
|
305
|
-
delim = false
|
306
|
-
skip(IGNORE)
|
307
|
-
if scan(COLLECTION_DELIMITER)
|
308
|
-
delim = true
|
309
|
-
elsif match?(OBJECT_CLOSE)
|
310
|
-
;
|
311
|
-
else
|
312
|
-
raise ParserError, "expected ',' or '}' in object at '#{peek(20)}'!"
|
313
|
-
end
|
314
|
-
else
|
315
|
-
raise ParserError, "expected value in object at '#{peek(20)}'!"
|
316
|
-
end
|
317
|
-
when scan(OBJECT_CLOSE)
|
318
|
-
if delim
|
319
|
-
raise ParserError, "expected next name, value pair in object at '#{peek(20)}'!"
|
320
|
-
end
|
321
|
-
if @create_additions and klassname = result[@create_id]
|
322
|
-
klass = JSON.deep_const_get klassname
|
323
|
-
break unless klass and klass.json_creatable?
|
324
|
-
result = klass.json_create(result)
|
325
|
-
end
|
326
|
-
break
|
327
|
-
when skip(IGNORE)
|
328
|
-
;
|
329
|
-
else
|
330
|
-
raise ParserError, "unexpected token in object at '#{peek(20)}'!"
|
331
|
-
end
|
332
|
-
end
|
333
|
-
result
|
334
|
-
end
|
335
|
-
end
|
336
|
-
end
|
337
|
-
end
|
data/lib/json/pure.rb
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
require 'json/common'
|
2
|
-
|
3
|
-
module JSON
|
4
|
-
# This module holds all the modules/classes that implement JSON's
|
5
|
-
# functionality in pure ruby.
|
6
|
-
module Pure
|
7
|
-
require 'json/pure/parser'
|
8
|
-
require 'json/pure/generator'
|
9
|
-
$DEBUG and warn "Using Pure library for JSON."
|
10
|
-
JSON.parser = Parser
|
11
|
-
JSON.generator = Generator
|
12
|
-
end
|
13
|
-
|
14
|
-
JSON_LOADED = true unless defined?(::JSON::JSON_LOADED)
|
15
|
-
end
|
/data/{LICENSE → COPYING}
RENAMED
File without changes
|