Skip to content

Commit 4b843b5

Browse files
committed
Remove generate restriction for […]/{…}
1 parent 280005f commit 4b843b5

4 files changed

Lines changed: 3 additions & 66 deletions

File tree

ext/json/ext/generator/generator.c

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -914,21 +914,6 @@ static VALUE cState_partial_generate(VALUE self, VALUE obj)
914914
return fbuffer_to_s(buffer);
915915
}
916916

917-
/*
918-
* This function returns true if string is either a JSON array or JSON object.
919-
* It might suffer from false positives, e. g. syntactically incorrect JSON in
920-
* the string or certain UTF-8 characters on the right hand side.
921-
*/
922-
static int isArrayOrObject(VALUE string)
923-
{
924-
long string_len = RSTRING_LEN(string);
925-
char *p = RSTRING_PTR(string), *q = p + string_len - 1;
926-
if (string_len < 2) return 0;
927-
for (; p < q && isspace((unsigned char)*p); p++);
928-
for (; q > p && isspace((unsigned char)*q); q--);
929-
return (*p == '[' && *q == ']') || (*p == '{' && *q == '}');
930-
}
931-
932917
/*
933918
* call-seq: generate(obj)
934919
*
@@ -940,9 +925,6 @@ static VALUE cState_generate(VALUE self, VALUE obj)
940925
{
941926
VALUE result = cState_partial_generate(self, obj);
942927
GET_STATE(self);
943-
if (!state->quirks_mode && !isArrayOrObject(result)) {
944-
rb_raise(eGeneratorError, "only generation of JSON objects or arrays allowed");
945-
}
946928
return result;
947929
}
948930

java/src/json/ext/GeneratorState.java

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -208,45 +208,13 @@ public IRubyObject initialize_copy(ThreadContext context, IRubyObject vOrig) {
208208
@JRubyMethod
209209
public IRubyObject generate(ThreadContext context, IRubyObject obj) {
210210
RubyString result = Generator.generateJson(context, obj, this);
211-
if (!quirksMode && !objectOrArrayLiteral(result)) {
212-
throw Utils.newException(context, Utils.M_GENERATOR_ERROR,
213-
"only generation of JSON objects or arrays allowed");
214-
}
215211
RuntimeInfo info = RuntimeInfo.forRuntime(context.getRuntime());
216212
if (info.encodingsSupported()) {
217213
result.force_encoding(context, info.utf8.get());
218214
}
219215
return result;
220216
}
221217

222-
/**
223-
* Ensures the given string is in the form "[...]" or "{...}", being
224-
* possibly surrounded by white space.
225-
* The string's encoding must be ASCII-compatible.
226-
* @param value
227-
* @return
228-
*/
229-
private static boolean objectOrArrayLiteral(RubyString value) {
230-
ByteList bl = value.getByteList();
231-
int len = bl.length();
232-
233-
for (int pos = 0; pos < len - 1; pos++) {
234-
int b = bl.get(pos);
235-
if (Character.isWhitespace(b)) continue;
236-
237-
// match the opening brace
238-
switch (b) {
239-
case '[':
240-
return matchClosingBrace(bl, pos, len, ']');
241-
case '{':
242-
return matchClosingBrace(bl, pos, len, '}');
243-
default:
244-
return false;
245-
}
246-
}
247-
return false;
248-
}
249-
250218
private static boolean matchClosingBrace(ByteList bl, int pos, int len,
251219
int brace) {
252220
for (int endPos = len - 1; endPos > pos; endPos--) {

lib/json/pure/generator.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -294,13 +294,6 @@ def generate(obj)
294294
result = obj.to_json(self)
295295
JSON.valid_utf8?(result) or raise GeneratorError,
296296
"source sequence #{result.inspect} is illegal/malformed utf-8"
297-
unless @quirks_mode
298-
unless result =~ /\A\s*\[/ && result =~ /\]\s*\Z/ ||
299-
result =~ /\A\s*\{/ && result =~ /\}\s*\Z/
300-
then
301-
raise GeneratorError, "only generation of JSON objects or arrays allowed"
302-
end
303-
end
304297
result
305298
end
306299

tests/test_json_generate.rb

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ def test_generate
5050
assert_equal('{"1":2}', json)
5151
parsed_json = parse(json)
5252
assert_equal({"1"=>2}, parsed_json)
53-
assert_raise(GeneratorError) { generate(666) }
54-
assert_equal '666', generate(666, :quirks_mode => true)
53+
assert_equal '666', generate(666)
5554
end
5655

5756
def test_generate_pretty
@@ -68,8 +67,7 @@ def test_generate_pretty
6867
EOT
6968
parsed_json = parse(json)
7069
assert_equal({"1"=>2}, parsed_json)
71-
assert_raise(GeneratorError) { pretty_generate(666) }
72-
assert_equal '666', pretty_generate(666, :quirks_mode => true)
70+
assert_equal '666', pretty_generate(666)
7371
end
7472

7573
def test_generate_custom
@@ -94,8 +92,7 @@ def test_fast_generate
9492
assert_equal('{"1":2}', json)
9593
parsed_json = parse(json)
9694
assert_equal({"1"=>2}, parsed_json)
97-
assert_raise(GeneratorError) { fast_generate(666) }
98-
assert_equal '666', fast_generate(666, :quirks_mode => true)
95+
assert_equal '666', fast_generate(666)
9996
end
10097

10198
def test_own_state
@@ -108,9 +105,6 @@ def test_own_state
108105
assert_equal('{"1":2}', json)
109106
parsed_json = parse(json)
110107
assert_equal({"1"=>2}, parsed_json)
111-
assert_raise(GeneratorError) { generate(666, state) }
112-
state.quirks_mode = true
113-
assert state.quirks_mode?
114108
assert_equal '666', generate(666, state)
115109
end
116110

0 commit comments

Comments
 (0)