/Users/jwebb/src/bigjason/elster/lib/elster/streamer.rb in elster-0.0.6 vs /Users/jwebb/src/bigjason/elster/lib/elster/streamer.rb in elster-0.0.7
- old
+ new
@@ -42,10 +42,14 @@
# json.add("HUGE!")
# end
#
# { "mistake" : [ "HUGE!" ] }
def key(key, value=nil, &block)
+ if @current_type == :array
+ raise JsonContainerTypeError, "Attempted to write an object `key` value inside a JSON array."
+ end
+
if @item_count > 0
write ","
else
@current_type = :object
begin_section
@@ -80,10 +84,14 @@
# json.set(:name, "Wiggens")
# end
#
# [ 1, { "name" : "Wiggens" } ]
def add(value=nil, &block)
+ if @current_type == :object
+ raise JsonContainerTypeError, "Attempted to add an array value inside a JSON object."
+ end
+
if @item_count > 0
write ","
else
@current_type = :array
begin_section
@@ -117,18 +125,18 @@
when FalseClass
encode_boolean(value)
when nil
encode_nil(value)
else
- MultiJson.dump(value)
+ encode_generic(value)
end
end
def encode_generic(value)
MultiJson.dump(value)
end
- SAFE_RE = Regexp.new("\\A[#{Regexp.escape((32..126).select{|c| c != 34}.map{|c| c.chr}.join)}]+\\z")
+ SAFE_RE = Regexp.new("\\A[#{Regexp.escape((32..126).select{|c| ![34, 92].include?(c)}.map{|c| c.chr}.join)}]+\\z")
def encode_string(value)
if value =~ SAFE_RE
"\"#{value}\""
else
encode_generic(value.to_s)