summaryrefslogtreecommitdiff
path: root/lib/json/pure
diff options
context:
space:
mode:
Diffstat (limited to 'lib/json/pure')
-rw-r--r--lib/json/pure/generator.rb29
-rw-r--r--lib/json/pure/parser.rb9
2 files changed, 27 insertions, 11 deletions
diff --git a/lib/json/pure/generator.rb b/lib/json/pure/generator.rb
index 7c9b2ad..9141ae5 100644
--- a/lib/json/pure/generator.rb
+++ b/lib/json/pure/generator.rb
@@ -136,14 +136,15 @@ module JSON
# * *quirks_mode*: Enables quirks_mode for parser, that is for example
# generating single JSON values instead of documents is possible.
def initialize(opts = {})
- @indent = ''
- @space = ''
- @space_before = ''
- @object_nl = ''
- @array_nl = ''
- @allow_nan = false
- @ascii_only = false
- @quirks_mode = false
+ @indent = ''
+ @space = ''
+ @space_before = ''
+ @object_nl = ''
+ @array_nl = ''
+ @allow_nan = false
+ @ascii_only = false
+ @quirks_mode = false
+ @buffer_initial_length = 1024
configure opts
end
@@ -172,6 +173,16 @@ module JSON
# it's disabled.
attr_accessor :quirks_mode
+ # :stopdoc:
+ attr_reader :buffer_initial_length
+
+ def buffer_initial_length=(length)
+ if length > 0
+ @buffer_initial_length = length
+ end
+ end
+ # :startdoc:
+
# This integer returns the current depth data structure nesting in the
# generated JSON.
attr_accessor :depth
@@ -233,7 +244,7 @@ module JSON
# passed to the configure method.
def to_h
result = {}
- for iv in %w[indent space space_before object_nl array_nl allow_nan max_nesting ascii_only quirks_mode depth]
+ for iv in %w[indent space space_before object_nl array_nl allow_nan max_nesting ascii_only quirks_mode buffer_initial_length depth]
result[iv.intern] = instance_variable_get("@#{iv}")
end
result
diff --git a/lib/json/pure/parser.rb b/lib/json/pure/parser.rb
index e24aac1..84eb67f 100644
--- a/lib/json/pure/parser.rb
+++ b/lib/json/pure/parser.rb
@@ -73,7 +73,7 @@ module JSON
def initialize(source, opts = {})
opts ||= {}
unless @quirks_mode = opts[:quirks_mode]
- source = determine_encoding source
+ source = convert_encoding source
end
super source
if !opts.key?(:max_nesting) # defaults to 19
@@ -145,7 +145,12 @@ module JSON
private
- def determine_encoding(source)
+ def convert_encoding(source)
+ if source.respond_to?(:to_str)
+ source = source.to_str
+ else
+ raise TypeError, "#{source.inspect} is not like a string"
+ end
if defined?(::Encoding)
if source.encoding == ::Encoding::ASCII_8BIT
b = source[0, 4].bytes.to_a