Permalink
Browse files

Merge branch 'master' of gate.dyna.ping.de:/git/json

  • Loading branch information...
2 parents 334a4dd + 34e9774 commit 519437ab428d67f4b4105e2a94f2f2c1601fdcf5 @flori committed Mar 26, 2012
Showing with 20 additions and 11 deletions.
  1. +1 −1 README.rdoc
  2. +12 −8 ext/json/ext/generator/generator.c
  3. +1 −0 ext/json/ext/generator/generator.h
  4. +6 −2 lib/json/pure/generator.rb
View
2 README.rdoc
@@ -1,4 +1,4 @@
-= JSON implementation for Ruby http://travis-ci.org/flori/json.png?branch=master
+= JSON implementation for Ruby {<img src="https://secure.travis-ci.org/flori/json.png" />}[http://travis-ci.org/flori/json]
== Description
View
20 ext/json/ext/generator/generator.c
@@ -852,6 +852,16 @@ static VALUE cState_partial_generate(VALUE self, VALUE obj)
return fbuffer_to_s(buffer);
}
+static int isArrayOrObject(VALUE string)
+{
+ long string_len = RSTRING_LEN(string);
+ char c, *p = RSTRING_PTR(string), *q = p + string_len - 1;
+ if (string_len < 2) return 0;
+ for (; p < q && isspace(*p); p++);
+ for (; q > p && isspace(*q); q--);
+ return *p == '[' && *q == ']' || *p == '{' && *q == '}';
+}
+
/*
* call-seq: generate(obj)
*
@@ -862,15 +872,9 @@ static VALUE cState_partial_generate(VALUE self, VALUE obj)
static VALUE cState_generate(VALUE self, VALUE obj)
{
VALUE result = cState_partial_generate(self, obj);
- VALUE re, args[2];
GET_STATE(self);
- if (!state->quirks_mode) {
- args[0] = rb_str_new2("\\A\\s*(?:\\[.*\\]|\\{.*\\})\\s*\\Z");
- args[1] = CRegexp_MULTILINE;
- re = rb_class_new_instance(2, args, rb_cRegexp);
- if (NIL_P(rb_funcall(re, i_match, 1, result))) {
- rb_raise(eGeneratorError, "only generation of JSON objects or arrays allowed");
- }
+ if (!state->quirks_mode && !isArrayOrObject(result)) {
+ rb_raise(eGeneratorError, "only generation of JSON objects or arrays allowed");
}
return result;
}
View
1 ext/json/ext/generator/generator.h
@@ -4,6 +4,7 @@
#include <string.h>
#include <assert.h>
#include <math.h>
+#include <ctype.h>
#include "ruby.h"
View
8 lib/json/pure/generator.rb
@@ -255,8 +255,12 @@ def to_h
# GeneratorError exception.
def generate(obj)
result = obj.to_json(self)
- if !@quirks_mode && result !~ /\A\s*(?:\[.*\]|\{.*\})\s*\Z/m
- raise GeneratorError, "only generation of JSON objects or arrays allowed"
+ unless @quirks_mode
+ unless result =~ /\A\s*\[/ && result =~ /\]\s*\Z/ ||
+ result =~ /\A\s*\{/ && result =~ /\}\s*\Z/
+ then
+ raise GeneratorError, "only generation of JSON objects or arrays allowed"
+ end
end
result
end

0 comments on commit 519437a

Please sign in to comment.