Commit graph

295 commits

Author SHA1 Message Date
Jean Boussier
4cd893b048 [flori/json] Optimize key type check in json_object_i
Rather than checking the class we can check the type.
This is very subtly different for String subclasses, but I think it's
OK.

We also save on checking the type again in the fast path.

772a0201ab
2024-10-03 14:20:34 +09:00
Jean Boussier
57282c62a0 [flori/json] Optimize fbuffer_inc_capa
On my `JSON.dump` benchmark it shows up as 6% of runtime, compared
to 40% for `convert_UTF8_to_JSON`.

Since the vast majority of the time this function is called we
still have some buffer capacity, we might as well check that
first and skip the expensive loop etc.

With this change my profiler now report this function as 0.7%,
so almost 10x better.

a7206bf2db
2024-10-03 14:20:34 +09:00
Jean Boussier
630c681321 [flori/json] JSON.dump: avoid redundant UTF-8 validation
Given that we called `rb_enc_str_asciionly_p`, if the string encoding
isn't valid UTF-8, we can't know it very cheaply by checking the
encoding and coderange that was just computed by Ruby, rather than
to do it ourselves.

Also Ruby might have already computed that earlier.

4b04c469d5
2024-10-03 14:20:34 +09:00
Jean Boussier
d612f9fd34 [flori/json] Remove outdated ifdef checks
`json` requires Ruby 2.3, so `HAVE_RUBY_ENCODING_H` and `HAVE_RB_ENC_RAISE`
are always true.

5c8dc6b70a
2024-09-03 11:51:51 +09:00
Benoit Daloze
9c4a28b826 [flori/json] Use the pure-Ruby generator on TruffleRuby as it is much faster
* Using the benchmark from https://github.com/flori/json/pull/580
$ ruby benchmarks/bench.rb dump pure
JSON::Pure::Generator
truffleruby 24.0.0, like ruby 3.2.2, Oracle GraalVM Native [x86_64-linux]
Warming up --------------------------------------
      JSON.dump(obj)   116.000 i/100ms
      JSON.dump(obj)   235.000 i/100ms
      JSON.dump(obj)   317.000 i/100ms
      JSON.dump(obj)   372.000 i/100ms
      JSON.dump(obj)   374.000 i/100ms
Calculating -------------------------------------
      JSON.dump(obj)      3.735k (± 0.9%) i/s  (267.76 μs/i) -     18.700k in   5.007526s
      JSON.dump(obj)      3.738k (± 0.7%) i/s  (267.49 μs/i) -     18.700k in   5.002252s
      JSON.dump(obj)      3.743k (± 0.7%) i/s  (267.18 μs/i) -     19.074k in   5.096375s
      JSON.dump(obj)      3.747k (± 0.5%) i/s  (266.87 μs/i) -     19.074k in   5.090463s
      JSON.dump(obj)      3.746k (± 0.5%) i/s  (266.96 μs/i) -     19.074k in   5.092069s
$ ruby benchmarks/bench.rb dump ext
JSON::Ext::Generator
truffleruby 24.0.0, like ruby 3.2.2, Oracle GraalVM Native [x86_64-linux]
Warming up --------------------------------------
      JSON.dump(obj)    19.000 i/100ms
      JSON.dump(obj)    18.000 i/100ms
      JSON.dump(obj)    18.000 i/100ms
      JSON.dump(obj)    18.000 i/100ms
      JSON.dump(obj)    21.000 i/100ms
Calculating -------------------------------------
      JSON.dump(obj)    221.260 (±10.8%) i/s    (4.52 ms/i) -      1.092k in   5.004381s
      JSON.dump(obj)    221.983 (± 8.1%) i/s    (4.50 ms/i) -      1.113k in   5.055574s
      JSON.dump(obj)    221.446 (± 8.6%) i/s    (4.52 ms/i) -      1.113k in   5.073167s
      JSON.dump(obj)    226.452 (± 7.9%) i/s    (4.42 ms/i) -      1.134k in   5.048568s
      JSON.dump(obj)    227.795 (± 8.3%) i/s    (4.39 ms/i) -      1.134k in   5.025187s

8256455cdc
2024-06-04 14:44:43 +09:00
Jean Boussier
c5ae432ec8
[flori/json] Cleanup useless ifdef
The json gem now requires Ruby 2.3, so there is no point keeping
compatibility code for older releases that don't have the
TypedData API.

45c86e153f
2024-06-04 12:23:48 +09:00
卜部昌平
c844968b72 ruby tool/update-deps --fix 2024-04-27 21:55:28 +09:00
Hiroshi SHIBATA
ab65148c78 [flori/json] Bump up 2.7.2
036944acc6
2024-04-04 12:46:02 +09:00
Hiroshi SHIBATA
514b415d90 [flori/json] Warn to install ostruct if json couldn't load it
fff285968d
2024-04-04 12:46:02 +09:00
tompng
066bd28cff [flori/json] Autoload GenericObject to avoid require ostruct warning in Ruby 3.4
b507f9e404
2024-04-04 12:46:01 +09:00
Peter Zhu
6e34386794
[flori/json] Fix memory leak when exception is raised during JSON generation
If an exception is raised the FBuffer is leaked.

For example, the following script leaks memory:

    o = Object.new
    def o.to_json(a) = raise

    10.times do
      100_000.times do
        begin
          JSON(o)
        rescue
        end
      end

      puts `ps -o rss= -p #{$$}`
    end

Before:

    31824
    35696
    40240
    44304
    47424
    50944
    54000
    58384
    62416
    65296

After:

    24416
    24640
    24640
    24736
    24736
    24736
    24736
    24736
    24736
    24736

44df509dc2
2024-03-27 08:24:28 +09:00
Hiroshi SHIBATA
7c8f9603b1 [flori/json] Make OpenStruct support as optional
202ffe2335
2024-01-31 14:56:00 +09:00
John Hawthorn
ea5776e7e4 [flori/json] Use rb_sym2str instead of SYM2ID
This avoids pinning an id to the symbol used if a dynamic symbol is
passed in as a hash key.

rb_sym2str is available in Ruby 2.2+ and json depends on >= 2.3.

5cbafb8dbe
2023-12-25 21:12:49 +09:00
Hiroshi SHIBATA
eecae51502 [flori/json] Bump up 2.7.1
a1af7a308c
2023-12-05 15:09:56 +09:00
Hiroshi SHIBATA
a399c1f26d
Removed obsoleted version definition 2023-12-05 12:04:35 +09:00
Hiroshi SHIBATA
bf5368493b [flori/json] Detect json version from version.rb
3ef57b5b39
2023-12-05 12:04:11 +09:00
Hiroshi SHIBATA
abc3d124f7 [flori/json] The modern Ruby uses utf-8 encodings by default
11b31210ac
2023-12-05 12:04:10 +09:00
Burdette Lamar
c8faaf4c7e [flori/json] [DOC] RDoc for additions
(https://github.com/flori/json/pull/557)

* RDoc for additions

* Update lib/json/add/time.rb

Co-authored-by: Hiroshi SHIBATA <hsbt@ruby-lang.org>

---------

3f2efd60f7

Co-authored-by: Hiroshi SHIBATA <hsbt@ruby-lang.org>
2023-12-05 12:04:09 +09:00
tompng
70740deea7 [flori/json] Fix JSON.dump overload combination
41c2712a3b
2023-12-05 12:04:08 +09:00
Takashi Kokubun
e6b35e8a6d [flori/json] Overload kwargs in JSON.dump
936f280f9f
2023-12-05 12:04:08 +09:00
Jean Boussier
a22ed89438 [flori/json] JSON.dump: handle unenclosed hashes regression
Fix: https://github.com/flori/json/issues/553

We can never add keyword arguments to `dump` otherwise
existing code using unenclosed hash will break.

8e0076a3f2
2023-12-05 12:04:07 +09:00
Hiroshi SHIBATA
86045fca24
Manually merged from flori/json
> https://github.com/flori/json/pull/525
  > Rename escape_slash in script_safe and also escape E+2028 and E+2029

  Co-authored-by: Jean Boussier <jean.boussier@gmail.com>

  > https://github.com/flori/json/pull/454
  > Remove unnecessary initialization of create_id in JSON.parse()

  Co-authored-by: Watson <watson1978@gmail.com>
2023-12-01 16:47:06 +09:00
Jean Boussier
0dfeb17296
Rename escape_slash in script_safe and also escape E+2028 and E+2029
It is rather common to directly interpolate JSON string inside
<script> tags in HTML as to provide configuration or parameters to a
script.

However this may lead to XSS vulnerabilities, to prevent that 3
characters need to be escaped:

  - `/` (forward slash)
  - `U+2028` (LINE SEPARATOR)
  - `U+2029` (PARAGRAPH SEPARATOR)

The forward slash need to be escaped to prevent closing the script
tag early, and the other two are valid JSON but invalid Javascript
and can be used to break JS parsing.

Given that the intent of escaping forward slash is the same than escaping
U+2028 and U+2029, I chos to rename and repurpose the existing `escape_slash`
option.
2023-12-01 16:47:06 +09:00
Jean Boussier
698cb84062
Use ruby_xfree to free buffers
They are allocated with ruby_xmalloc, they should be freed with
ruby_xfree.
2023-12-01 16:47:06 +09:00
Hiroshi SHIBATA
32f289d118
[flori/json] Bump up 2.7.0
ca546128f2
2023-12-01 16:47:06 +09:00
John Hawthorn
4b770527c2
[flori/json] Fix "unexpected token" offset for Infinity
Previously in the JSON::Ext parser, when we encountered an "Infinity"
token (and weren't allowing NaN/Infinity) we would try to display the
"unexpected token" at the character before.

42ac170712
2023-12-01 16:47:06 +09:00
BurdetteLamar
d12e881009
[flori/json] Enhanced RDoc for Range extensions
ec47749b53
2023-11-08 09:04:28 +09:00
Nobuyoshi Nakada
19486ebd72 [flori/json] Re-generate parser.c
82a75ba98e
2023-07-19 00:02:58 +09:00
Nobuyoshi Nakada
768668a4de [flori/json] Remove unnecessary code
In `JSON#generate` and `JSON#fast_generate`:

- When the given `opts` is a `JSON::State` the variable is set to
  `nil`.
- But it will be never used as the next `if` blocks will not be
  executed.
- `JSON::State#configure` does the conversion to `Hash`, the
  conversions in the `if` block are just duplication.
- `JSON::State.new` does the same thing with `configure` when an
  argument is given.

5d9ab87f8e
2023-07-19 00:02:58 +09:00
Nobuyoshi Nakada
104089ce02 [flori/json] [DOC] Remove duplicate sentence
ed242667b4
2023-07-19 00:02:58 +09:00
Nobuyoshi Nakada
f1f84ca71c [flori/json] Remove HAVE_RB_SCAN_ARGS_OPTIONAL_HASH check
This macro is defined since ruby 2.1, which is older than the required
ruby version.

dd1d54e78a
2023-07-19 00:02:58 +09:00
Hiroshi SHIBATA
56c8dab468 [flori/json] Skip BigDecimal tests when it's missing to load
3dd36c6077
2023-07-18 12:25:55 +09:00
Dimitar Haralanov
9977462fd9 [flori/json] Rename JSON::ParseError to JSON:ParserError
20b80ca317
2023-07-18 12:25:54 +09:00
Ufuk Kayserilioglu
12dfd9d1c9
[flori/json] Call super in included hook
The C extension defines an `included` hook for the
`JSON::Ext::Generator::GeneratorMethods::String` module but neglects to
call `super` in the hook. This can break the functionality of various
other code that rely on the fact that `included` on `Module` will always
be called.

cd8bbe56a3
2023-05-24 09:37:30 +09:00
Matt Valentine-House
5e4b80177e Update the depend files 2023-02-28 09:09:00 -08:00
Matt Valentine-House
f38c6552f9 Remove intern/gc.h from Make deps 2023-02-27 10:11:56 -08:00
Nobuyoshi Nakada
899ea35035
Extract include/ruby/internal/attr/packed_struct.h
Split `PACKED_STRUCT` and `PACKED_STRUCT_UNALIGNED` macros into the
macros bellow:
* `RBIMPL_ATTR_PACKED_STRUCT_BEGIN`
* `RBIMPL_ATTR_PACKED_STRUCT_END`
* `RBIMPL_ATTR_PACKED_STRUCT_UNALIGNED_BEGIN`
* `RBIMPL_ATTR_PACKED_STRUCT_UNALIGNED_END`
2023-02-08 12:34:13 +09:00
Yusuke Nakamura
a22ed3e99d
Fix homepage url in gemspec
GitHub Pages now hosted on *.github.io
2023-01-23 17:06:15 +09:00
Hiroshi SHIBATA
3b45498d3e Merge json-2.6.3 2022-12-09 16:36:22 +09:00
Jean Boussier
66b52f046f [flori/json] Stop including the parser source __LINE__ in exceptions
It makes testing for JSON errors very tedious. You either have
to use a Regexp or to regularly update all your assertions
when JSON is upgraded.

de9eb1d28e
2022-07-29 19:10:10 +09:00
Florian Frank
9245b28d34
[flori/json] Bump version to 2.6.2
5de358f655
2022-05-20 17:49:13 +09:00
Andrew Bromwich
a15d0e267a
[flori/json] Fix parser bug for empty string allocation
When `HAVE_RB_ENC_INTERNED_STR` is enabled it is possible to
pass through a null pointer to `rb_enc_interned_str` resulting
in a segfault

Fixes #495

b59368a8c2
2022-05-20 17:49:13 +09:00
Hiroshi SHIBATA
767f3904ee
[flori/json] Doc: Improve documentation on JSON#parse and JSON#parse!
75ada77b96

Co-authored-by: Bruno Gomes da Silva <brunojabs@gmail.com>
2022-05-20 17:49:13 +09:00
Peter Zhu
2d5ecd60a5 [Feature #18249] Update dependencies 2022-02-22 09:55:21 -05:00
Nobuyoshi Nakada
ac152b3cac
Update dependencies 2021-11-21 16:21:18 +09:00
Hiroshi SHIBATA
1be2875e1d
[flori/json] Bump version to v2.6.1
2db5894cfa
2021-10-24 09:25:20 +09:00
Josef Šimánek
4cbce79438
[flori/json] Bump JSON::VERSION to 2.6.0.
da94d9f059
2021-10-24 09:25:01 +09:00
Hiroshi SHIBATA
91c2069dcf [flori/json] Bump up json version to 2.6.0
1942689b67
2021-10-14 17:04:37 +09:00
卜部昌平
5c167a9778 ruby tool/update-deps --fix 2021-10-05 14:18:23 +09:00
Hiroshi SHIBATA
a889ab486e
Update the latest version of json.gemspec from flori/json 2021-07-29 15:57:58 +09:00