Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

the return values of to_json and to_s must be a string

  • Loading branch information...
commit 1bdd9fc36d74eb48af470fd46937558a00895439 1 parent 09dfe41
@brianmario authored
Showing with 26 additions and 0 deletions.
  1. +2 −0  ext/yajl/yajl_ext.c
  2. +24 −0 spec/encoding/encoding_spec.rb
View
2  ext/yajl/yajl_ext.c
@@ -187,11 +187,13 @@ void yajl_encode_part(void * wrapper, VALUE obj, VALUE io) {
default:
if (rb_respond_to(obj, intern_to_json)) {
str = rb_funcall(obj, intern_to_json, 0);
+ Check_Type(str, T_STRING);
cptr = RSTRING_PTR(str);
len = RSTRING_LEN(str);
status = yajl_gen_number(w->encoder, cptr, len);
} else {
str = rb_funcall(obj, intern_to_s, 0);
+ Check_Type(str, T_STRING);
cptr = RSTRING_PTR(str);
len = RSTRING_LEN(str);
status = yajl_gen_string(w->encoder, (const unsigned char *)cptr, len);
View
24 spec/encoding/encoding_spec.rb
@@ -7,6 +7,18 @@ def to_json
end
end
+class TheMindKiller
+ def to_json
+ @wait_i_dont_exist_this_is_a_horrible_mistake_dont_do_it_aiiiiiiiiieeeeeee
+ end
+end
+
+class TheMindKillerDuce
+ def to_s
+ @wait_i_dont_exist_this_is_a_horrible_mistake_dont_do_it_aiiiiiiiiieeeeeee
+ end
+end
+
describe "Yajl JSON encoder" do
FILES = Dir[File.dirname(__FILE__)+'/../../benchmark/subjects/*.json']
@@ -244,4 +256,16 @@ def to_json
unsafe_encoder = Yajl::Encoder.new
unsafe_encoder.encode("</script>").should_not eql("\"<\\/script>\"")
end
+
+ it "return value of #to_json must be a string" do
+ lambda {
+ Yajl::Encoder.encode(TheMindKiller.new)
+ }.should raise_error(TypeError)
+ end
+
+ it "return value of #to_s must be a string" do
+ lambda {
+ Yajl::Encoder.encode(TheMindKillerDuce.new)
+ }.should raise_error(TypeError)
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.