json 2.7.4 → 2.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c75f2746cbbd315a24997d2bf37e0284a9b21a99d43d7a5abf35369415f9ed9f
4
- data.tar.gz: 57c78c979d0aad97c7d8a7d8602ea7c47824f1a62f8335681ea2803a62b09fba
3
+ metadata.gz: fc08bd5e3c83c4e731d37e18b5a32f9bf0049d896c483f1beb96af96f2daad18
4
+ data.tar.gz: ff215943fdccc5a5ed3f7390719aa5c43a18664e19f86d94226ff14c154cc354
5
5
  SHA512:
6
- metadata.gz: 1fb3fc2fe76bbcdde8f1dd6a501e8868c0cc8be64a59841ef3ffe436e1b8f99fbc0a4fe383c200531a722c21097f9c925d71fba1320c0d181abe45c6f7c74291
7
- data.tar.gz: 739a9039ed42e1ddb4a5ca37a96e23485bc3383bd11c557f83bc63df16e0366bdc15cca886ffa041f3cb46a87010dd69657c2df54eefaa89597a8a4b8c6ff6bd
6
+ metadata.gz: e3bc85653be385e76842fa3f69671fccdc4235a32806f554cf8ec4a9ad697996be97d3aef52823b3c1f8839e1cd9a7893ab87360ca76d59b4ff704c75fcb655e
7
+ data.tar.gz: f2f09188afd38972e15e1c80c2c8c4efd14aee954d18ff83d4a6a993bd0f9d9c84362232c17abbc9bf3ffdfce062c79c75a6fccf124bdbd3345a202b76d50a26
data/CHANGES.md CHANGED
@@ -1,5 +1,59 @@
1
1
  # Changes
2
2
 
3
+ ### 2025-02-10 (2.10.1)
4
+
5
+ * Fix a compatibility issue with `MultiJson.dump(obj, pretty: true)`: `no implicit conversion of false into Proc (TypeError)`.
6
+
7
+ ### 2025-02-10 (2.10.0)
8
+
9
+ * `strict: true` now accept symbols as values. Previously they'd only be accepted as hash keys.
10
+ * The C extension Parser has been entirely reimplemented from scratch.
11
+ * Introduced `JSON::Coder` as a new API allowing to customize how non native types are serialized in a non-global way.
12
+ * Introduced `JSON::Fragment` to allow assembling cached fragments in a safe way.
13
+ * The Java implementation of the generator received many optimizations.
14
+
15
+ ### 2024-12-18 (2.9.1)
16
+
17
+ * Fix support for Solaris 10.
18
+
19
+ ### 2024-12-03 (2.9.0)
20
+
21
+ * Fix C implementation of `script_safe` escaping to not confuse some other 3 wide characters with `\u2028` and `\u2029`.
22
+ e.g. `JSON.generate(["倩", "瀨"], script_safe: true)` would generate the wrong JSON.
23
+ * `JSON.dump(object, some_io)` now write into the IO in chunks while previously it would buffer the entire JSON before writing.
24
+ * `JSON::GeneratorError` now has a `#invalid_object` attribute, making it easier to understand why an object tree cannot be serialized.
25
+ * Numerous improvements to the JRuby extension.
26
+
27
+ ### 2024-11-14 (2.8.2)
28
+
29
+ * `JSON.load_file` explictly read the file as UTF-8.
30
+
31
+ ### 2024-11-06 (2.8.1)
32
+
33
+ * Fix the java packages to include the extension.
34
+
35
+ ### 2024-11-06 (2.8.0)
36
+
37
+ * Emit a deprecation warning when `JSON.load` create custom types without the `create_additions` option being explictly enabled.
38
+ * Prefer to use `JSON.unsafe_load(string)` or `JSON.load(string, create_additions: true)`.
39
+ * Emit a deprecation warning when serializing valid UTF-8 strings encoded in `ASCII_8BIT` aka `BINARY`.
40
+ * Bump required Ruby version to 2.7.
41
+ * Add support for optionally parsing trailing commas, via `allow_trailing_comma: true`, which in cunjunction with the
42
+ pre-existing support for comments, make it suitable to parse `jsonc` documents.
43
+ * Many performance improvements to `JSON.parse` and `JSON.load`, up to `1.7x` faster on real world documents.
44
+ * Some minor performance improvements to `JSON.dump` and `JSON.generate`.
45
+ * `JSON.pretty_generate` no longer include newline inside empty object and arrays.
46
+
47
+ ### 2024-11-04 (2.7.6)
48
+
49
+ * Fix a regression in JSON.generate when dealing with Hash keys that are string subclasses, call `to_json` on them.
50
+
51
+ ### 2024-10-25 (2.7.5)
52
+
53
+ * Fix a memory leak when `#to_json` methods raise an exception.
54
+ * Gracefully handle formatting configs being set to `nil` instead of `""`.
55
+ * Workaround another issue caused by conflicting versions of both `json_pure` and `json` being loaded.
56
+
3
57
  ### 2024-10-25 (2.7.4)
4
58
 
5
59
  * Workaround a bug in 3.4.8 and older https://github.com/rubygems/rubygems/pull/6490.
data/LEGAL CHANGED
@@ -6,55 +6,3 @@
6
6
  All the files in this distribution are covered under either the Ruby's
7
7
  license (see the file COPYING) or public-domain except some files
8
8
  mentioned below.
9
-
10
- == MIT License
11
- >>>
12
- Permission is hereby granted, free of charge, to any person obtaining
13
- a copy of this software and associated documentation files (the
14
- "Software"), to deal in the Software without restriction, including
15
- without limitation the rights to use, copy, modify, merge, publish,
16
- distribute, sublicense, and/or sell copies of the Software, and to
17
- permit persons to whom the Software is furnished to do so, subject to
18
- the following conditions:
19
-
20
- The above copyright notice and this permission notice shall be
21
- included in all copies or substantial portions of the Software.
22
-
23
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30
-
31
- == Old-style BSD license
32
- >>>
33
- Redistribution and use in source and binary forms, with or without
34
- modification, are permitted provided that the following conditions
35
- are met:
36
- 1. Redistributions of source code must retain the above copyright
37
- notice, this list of conditions and the following disclaimer.
38
- 2. Redistributions in binary form must reproduce the above copyright
39
- notice, this list of conditions and the following disclaimer in the
40
- documentation and/or other materials provided with the distribution.
41
- 3. Neither the name of the University nor the names of its contributors
42
- may be used to endorse or promote products derived from this software
43
- without specific prior written permission.
44
-
45
- THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46
- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47
- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48
- ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49
- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50
- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51
- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52
- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53
- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54
- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55
- SUCH DAMAGE.
56
-
57
- IMPORTANT NOTE::
58
-
59
- From ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change
60
- paragraph 3 above is now null and void.
data/README.md CHANGED
@@ -5,16 +5,10 @@
5
5
  ## Description
6
6
 
7
7
  This is an implementation of the JSON specification according to RFC 7159
8
- http://www.ietf.org/rfc/rfc7159.txt . There is two variants available:
8
+ http://www.ietf.org/rfc/rfc7159.txt .
9
9
 
10
- * A pure ruby variant, that relies on the `strscan` extensions, which is
11
- part of the ruby standard library.
12
- * The quite a bit faster native extension variant, which is in parts
13
- implemented in C or Java and comes with a parser generated by the [Ragel]
14
- state machine compiler.
15
-
16
- Both variants of the JSON generator generate UTF-8 character sequences by
17
- default. If an :ascii\_only option with a true value is given, they escape all
10
+ The JSON generator generate UTF-8 character sequences by default.
11
+ If an :ascii\_only option with a true value is given, they escape all
18
12
  non-ASCII and control characters with \uXXXX escape sequences, and support
19
13
  UTF-16 surrogate pairs in order to be able to generate the whole range of
20
14
  unicode code points.
@@ -27,10 +21,6 @@ endpoint.
27
21
 
28
22
  ## Installation
29
23
 
30
- It's recommended to use the extension variant of JSON, because it's faster than
31
- the pure ruby variant. If you cannot build it on your system, you can settle
32
- for the latter.
33
-
34
24
  Install the gem and add to the application's Gemfile by executing:
35
25
 
36
26
  $ bundle add json
@@ -39,13 +29,7 @@ If bundler is not being used to manage dependencies, install the gem by executin
39
29
 
40
30
  $ gem install json
41
31
 
42
-
43
- There is also a pure ruby json only variant of the gem, that can be installed
44
- with:
45
-
46
- $ gem install json_pure
47
-
48
- ## Usage
32
+ ## Basic Usage
49
33
 
50
34
  To use JSON you can
51
35
 
@@ -53,20 +37,6 @@ To use JSON you can
53
37
  require 'json'
54
38
  ```
55
39
 
56
- to load the installed variant (either the extension `'json'` or the pure
57
- variant `'json_pure'`). If you have installed the extension variant, you can
58
- pick either the extension variant or the pure variant by typing
59
-
60
- ```ruby
61
- require 'json/ext'
62
- ```
63
-
64
- or
65
-
66
- ```ruby
67
- require 'json/pure'
68
- ```
69
-
70
40
  Now you can parse a JSON document into a ruby data structure by calling
71
41
 
72
42
  ```ruby
@@ -82,50 +52,84 @@ You can also use the `pretty_generate` method (which formats the output more
82
52
  verbosely and nicely) or `fast_generate` (which doesn't do any of the security
83
53
  checks generate performs, e. g. nesting deepness checks).
84
54
 
85
- There are also the JSON and JSON[] methods which use parse on a String or
86
- generate a JSON document from an array or hash:
55
+ ## Casting non native types
56
+
57
+ JSON documents can only support Hashes, Arrays, Strings, Integers and Floats.
58
+
59
+ By default if you attempt to serialize something else, `JSON.generate` will
60
+ search for a `#to_json` method on that object:
87
61
 
88
62
  ```ruby
89
- document = JSON 'test' => 23 # => "{\"test\":23}"
90
- document = JSON['test' => 23] # => "{\"test\":23}"
63
+ Position = Struct.new(:latitude, :longitude) do
64
+ def to_json(state = nil, *)
65
+ JSON::State.from_state(state).generate({
66
+ latitude: latitude,
67
+ longitude: longitude,
68
+ })
69
+ end
70
+ end
71
+
72
+ JSON.generate([
73
+ Position.new(12323.234, 435345.233),
74
+ Position.new(23434.676, 159435.324),
75
+ ]) # => [{"latitude":12323.234,"longitude":435345.233},{"latitude":23434.676,"longitude":159435.324}]
91
76
  ```
92
77
 
93
- and
78
+ If a `#to_json` method isn't defined on the object, `JSON.generate` will fallback to call `#to_s`:
94
79
 
95
80
  ```ruby
96
- data = JSON '{"test":23}' # => {"test"=>23}
97
- data = JSON['{"test":23}'] # => {"test"=>23}
81
+ JSON.generate(Object.new) # => "#<Object:0x000000011e768b98>"
98
82
  ```
99
83
 
100
- You can choose to load a set of common additions to ruby core's objects if
101
- you
84
+ Both of these behavior can be disabled using the `strict: true` option:
102
85
 
103
86
  ```ruby
104
- require 'json/add/core'
87
+ JSON.generate(Object.new, strict: true) # => Object not allowed in JSON (JSON::GeneratorError)
88
+ JSON.generate(Position.new(1, 2)) # => Position not allowed in JSON (JSON::GeneratorError)
105
89
  ```
106
90
 
107
- After requiring this you can, e. g., serialise/deserialise Ruby ranges:
91
+ ## JSON::Coder
92
+
93
+ Since `#to_json` methods are global, it can sometimes be problematic if you need a given type to be
94
+ serialized in different ways in different locations.
95
+
96
+ Instead it is recommended to use the newer `JSON::Coder` API:
108
97
 
109
98
  ```ruby
110
- JSON JSON(1..10) # => 1..10
111
- ```
99
+ module MyApp
100
+ API_JSON_CODER = JSON::Coder.new do |object|
101
+ case object
102
+ when Time
103
+ object.iso8601(3)
104
+ else
105
+ object
106
+ end
107
+ end
108
+ end
112
109
 
113
- To find out how to add JSON support to other or your own classes, read the
114
- section "More Examples" below.
110
+ puts MyApp::API_JSON_CODER.dump(Time.now.utc) # => "2025-01-21T08:41:44.286Z"
111
+ ```
115
112
 
116
- ## Serializing exceptions
113
+ The provided block is called for all objects that don't have a native JSON equivalent, and
114
+ must return a Ruby object that has a native JSON equivalent.
117
115
 
118
- The JSON module doesn't extend `Exception` by default. If you convert an `Exception`
119
- object to JSON, it will by default only include the exception message.
116
+ ## Combining JSON fragments
120
117
 
121
- To include the full details, you must either load the `json/add/core` mentioned
122
- above, or specifically load the exception addition:
118
+ To combine JSON fragments into a bigger JSON document, you can use `JSON::Fragment`:
123
119
 
124
120
  ```ruby
125
- require 'json/add/exception'
121
+ posts_json = cache.fetch_multi(post_ids) do |post_id|
122
+ JSON.generate(Post.find(post_id))
123
+ end
124
+ posts_json.map! { |post_json| JSON::Fragment.new(post_json) }
125
+ JSON.generate({ posts: posts_json, count: posts_json.count })
126
126
  ```
127
127
 
128
- ## More Examples
128
+ ## Round-tripping arbitrary types
129
+
130
+ > [!CAUTION]
131
+ > You should never use `JSON.unsafe_load` nor `JSON.parse(str, create_additions: true)` to parse untrusted user input,
132
+ > as it can lead to remote code execution vulnerabilities.
129
133
 
130
134
  To create a JSON document from a ruby data structure, you can call
131
135
  `JSON.generate` like that:
@@ -191,7 +195,7 @@ JSON.parse json
191
195
  # => [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
192
196
  json = JSON.generate [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
193
197
  # => "[1,2,{\"a\":3.141},false,true,null,{\"json_class\":\"Range\",\"data\":[4,10,false]}]"
194
- JSON.parse json, :create_additions => true
198
+ JSON.unsafe_load json
195
199
  # => [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
196
200
  ```
197
201
 
@@ -4,78 +4,141 @@
4
4
  #include "ruby.h"
5
5
  #include "ruby/encoding.h"
6
6
 
7
+ /* shims */
8
+ /* This is the fallback definition from Ruby 3.4 */
9
+
10
+ #ifndef RBIMPL_STDBOOL_H
11
+ #if defined(__cplusplus)
12
+ # if defined(HAVE_STDBOOL_H) && (__cplusplus >= 201103L)
13
+ # include <cstdbool>
14
+ # endif
15
+ #elif defined(HAVE_STDBOOL_H)
16
+ # include <stdbool.h>
17
+ #elif !defined(HAVE__BOOL)
18
+ typedef unsigned char _Bool;
19
+ # define bool _Bool
20
+ # define true ((_Bool)+1)
21
+ # define false ((_Bool)+0)
22
+ # define __bool_true_false_are_defined
23
+ #endif
24
+ #endif
25
+
26
+ #ifndef RB_UNLIKELY
27
+ #define RB_UNLIKELY(expr) expr
28
+ #endif
29
+
30
+ #ifndef RB_LIKELY
31
+ #define RB_LIKELY(expr) expr
32
+ #endif
33
+
34
+ #ifndef MAYBE_UNUSED
35
+ # define MAYBE_UNUSED(x) x
36
+ #endif
37
+
38
+ enum fbuffer_type {
39
+ FBUFFER_HEAP_ALLOCATED = 0,
40
+ FBUFFER_STACK_ALLOCATED = 1,
41
+ };
42
+
7
43
  typedef struct FBufferStruct {
44
+ enum fbuffer_type type;
8
45
  unsigned long initial_length;
9
- char *ptr;
10
46
  unsigned long len;
11
47
  unsigned long capa;
48
+ char *ptr;
49
+ VALUE io;
12
50
  } FBuffer;
13
51
 
52
+ #define FBUFFER_STACK_SIZE 512
53
+ #define FBUFFER_IO_BUFFER_SIZE (16384 - 1)
14
54
  #define FBUFFER_INITIAL_LENGTH_DEFAULT 1024
15
55
 
16
- #define FBUFFER_PTR(fb) (fb->ptr)
17
- #define FBUFFER_LEN(fb) (fb->len)
18
- #define FBUFFER_CAPA(fb) (fb->capa)
56
+ #define FBUFFER_PTR(fb) ((fb)->ptr)
57
+ #define FBUFFER_LEN(fb) ((fb)->len)
58
+ #define FBUFFER_CAPA(fb) ((fb)->capa)
19
59
  #define FBUFFER_PAIR(fb) FBUFFER_PTR(fb), FBUFFER_LEN(fb)
20
60
 
21
- static FBuffer *fbuffer_alloc(unsigned long initial_length);
22
61
  static void fbuffer_free(FBuffer *fb);
23
- #ifndef JSON_GENERATOR
24
62
  static void fbuffer_clear(FBuffer *fb);
25
- #endif
26
63
  static void fbuffer_append(FBuffer *fb, const char *newstr, unsigned long len);
27
- #ifdef JSON_GENERATOR
28
64
  static void fbuffer_append_long(FBuffer *fb, long number);
29
- #endif
30
- static void fbuffer_append_char(FBuffer *fb, char newchr);
31
- #ifdef JSON_GENERATOR
32
- static VALUE fbuffer_to_s(FBuffer *fb);
33
- #endif
34
-
35
- #ifndef RB_UNLIKELY
36
- #define RB_UNLIKELY(expr) expr
37
- #endif
65
+ static inline void fbuffer_append_char(FBuffer *fb, char newchr);
66
+ static VALUE fbuffer_finalize(FBuffer *fb);
38
67
 
39
- static FBuffer *fbuffer_alloc(unsigned long initial_length)
68
+ static void fbuffer_stack_init(FBuffer *fb, unsigned long initial_length, char *stack_buffer, long stack_buffer_size)
40
69
  {
41
- FBuffer *fb;
42
- if (initial_length <= 0) initial_length = FBUFFER_INITIAL_LENGTH_DEFAULT;
43
- fb = ALLOC(FBuffer);
44
- memset((void *) fb, 0, sizeof(FBuffer));
45
- fb->initial_length = initial_length;
46
- return fb;
70
+ fb->initial_length = (initial_length > 0) ? initial_length : FBUFFER_INITIAL_LENGTH_DEFAULT;
71
+ if (stack_buffer) {
72
+ fb->type = FBUFFER_STACK_ALLOCATED;
73
+ fb->ptr = stack_buffer;
74
+ fb->capa = stack_buffer_size;
75
+ }
47
76
  }
48
77
 
49
78
  static void fbuffer_free(FBuffer *fb)
50
79
  {
51
- if (fb->ptr) ruby_xfree(fb->ptr);
52
- ruby_xfree(fb);
80
+ if (fb->ptr && fb->type == FBUFFER_HEAP_ALLOCATED) {
81
+ ruby_xfree(fb->ptr);
82
+ }
53
83
  }
54
84
 
55
- #ifndef JSON_GENERATOR
56
85
  static void fbuffer_clear(FBuffer *fb)
57
86
  {
58
87
  fb->len = 0;
59
88
  }
60
- #endif
61
89
 
62
- static inline void fbuffer_inc_capa(FBuffer *fb, unsigned long requested)
90
+ static void fbuffer_flush(FBuffer *fb)
63
91
  {
64
- if (RB_UNLIKELY(requested > fb->capa - fb->len)) {
65
- unsigned long required;
92
+ rb_io_write(fb->io, rb_utf8_str_new(fb->ptr, fb->len));
93
+ fbuffer_clear(fb);
94
+ }
66
95
 
67
- if (RB_UNLIKELY(!fb->ptr)) {
68
- fb->ptr = ALLOC_N(char, fb->initial_length);
69
- fb->capa = fb->initial_length;
96
+ static void fbuffer_realloc(FBuffer *fb, unsigned long required)
97
+ {
98
+ if (required > fb->capa) {
99
+ if (fb->type == FBUFFER_STACK_ALLOCATED) {
100
+ const char *old_buffer = fb->ptr;
101
+ fb->ptr = ALLOC_N(char, required);
102
+ fb->type = FBUFFER_HEAP_ALLOCATED;
103
+ MEMCPY(fb->ptr, old_buffer, char, fb->len);
104
+ } else {
105
+ REALLOC_N(fb->ptr, char, required);
70
106
  }
107
+ fb->capa = required;
108
+ }
109
+ }
71
110
 
72
- for (required = fb->capa; requested > required - fb->len; required <<= 1);
111
+ static void fbuffer_do_inc_capa(FBuffer *fb, unsigned long requested)
112
+ {
113
+ if (RB_UNLIKELY(fb->io)) {
114
+ if (fb->capa < FBUFFER_IO_BUFFER_SIZE) {
115
+ fbuffer_realloc(fb, FBUFFER_IO_BUFFER_SIZE);
116
+ } else {
117
+ fbuffer_flush(fb);
118
+ }
73
119
 
74
- if (required > fb->capa) {
75
- REALLOC_N(fb->ptr, char, required);
76
- fb->capa = required;
120
+ if (RB_LIKELY(requested < fb->capa)) {
121
+ return;
77
122
  }
78
123
  }
124
+
125
+ unsigned long required;
126
+
127
+ if (RB_UNLIKELY(!fb->ptr)) {
128
+ fb->ptr = ALLOC_N(char, fb->initial_length);
129
+ fb->capa = fb->initial_length;
130
+ }
131
+
132
+ for (required = fb->capa; requested > required - fb->len; required <<= 1);
133
+
134
+ fbuffer_realloc(fb, required);
135
+ }
136
+
137
+ static inline void fbuffer_inc_capa(FBuffer *fb, unsigned long requested)
138
+ {
139
+ if (RB_UNLIKELY(requested > fb->capa - fb->len)) {
140
+ fbuffer_do_inc_capa(fb, requested);
141
+ }
79
142
  }
80
143
 
81
144
  static void fbuffer_append(FBuffer *fb, const char *newstr, unsigned long len)
@@ -87,7 +150,6 @@ static void fbuffer_append(FBuffer *fb, const char *newstr, unsigned long len)
87
150
  }
88
151
  }
89
152
 
90
- #ifdef JSON_GENERATOR
91
153
  static void fbuffer_append_str(FBuffer *fb, VALUE str)
92
154
  {
93
155
  const char *newstr = StringValuePtr(str);
@@ -97,50 +159,47 @@ static void fbuffer_append_str(FBuffer *fb, VALUE str)
97
159
 
98
160
  fbuffer_append(fb, newstr, len);
99
161
  }
100
- #endif
101
162
 
102
- static void fbuffer_append_char(FBuffer *fb, char newchr)
163
+ static inline void fbuffer_append_char(FBuffer *fb, char newchr)
103
164
  {
104
165
  fbuffer_inc_capa(fb, 1);
105
166
  *(fb->ptr + fb->len) = newchr;
106
167
  fb->len++;
107
168
  }
108
169
 
109
- #ifdef JSON_GENERATOR
110
- static void freverse(char *start, char *end)
111
- {
112
- char c;
113
-
114
- while (end > start) {
115
- c = *end, *end-- = *start, *start++ = c;
116
- }
117
- }
118
-
119
170
  static long fltoa(long number, char *buf)
120
171
  {
121
- static char digits[] = "0123456789";
172
+ static const char digits[] = "0123456789";
122
173
  long sign = number;
123
174
  char* tmp = buf;
124
175
 
125
176
  if (sign < 0) number = -number;
126
- do *tmp++ = digits[number % 10]; while (number /= 10);
127
- if (sign < 0) *tmp++ = '-';
128
- freverse(buf, tmp - 1);
129
- return tmp - buf;
177
+ do *tmp-- = digits[number % 10]; while (number /= 10);
178
+ if (sign < 0) *tmp-- = '-';
179
+ return buf - tmp;
130
180
  }
131
181
 
182
+ #define LONG_BUFFER_SIZE 20
132
183
  static void fbuffer_append_long(FBuffer *fb, long number)
133
184
  {
134
- char buf[20];
135
- unsigned long len = fltoa(number, buf);
136
- fbuffer_append(fb, buf, len);
185
+ char buf[LONG_BUFFER_SIZE];
186
+ char *buffer_end = buf + LONG_BUFFER_SIZE;
187
+ long len = fltoa(number, buffer_end - 1);
188
+ fbuffer_append(fb, buffer_end - len, len);
137
189
  }
138
190
 
139
- static VALUE fbuffer_to_s(FBuffer *fb)
191
+ static VALUE fbuffer_finalize(FBuffer *fb)
140
192
  {
141
- VALUE result = rb_utf8_str_new(FBUFFER_PTR(fb), FBUFFER_LEN(fb));
142
- fbuffer_free(fb);
143
- return result;
193
+ if (fb->io) {
194
+ fbuffer_flush(fb);
195
+ fbuffer_free(fb);
196
+ rb_io_flush(fb->io);
197
+ return fb->io;
198
+ } else {
199
+ VALUE result = rb_utf8_str_new(FBUFFER_PTR(fb), FBUFFER_LEN(fb));
200
+ fbuffer_free(fb);
201
+ return result;
202
+ }
144
203
  }
145
- #endif
204
+
146
205
  #endif