smarter_json 0.5.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 +7 -0
- data/.gitignore +46 -0
- data/CHANGELOG.md +70 -0
- data/LICENSE.txt +21 -0
- data/README.md +110 -0
- data/Rakefile +22 -0
- data/docs/_introduction.md +48 -0
- data/docs/basic_read_api.md +72 -0
- data/docs/basic_write_api.md +91 -0
- data/docs/examples.md +140 -0
- data/docs/options.md +58 -0
- data/ext/smarter_json/extconf.rb +30 -0
- data/ext/smarter_json/smarter_json.c +1424 -0
- data/ext/smarter_json/smarter_json.h +9 -0
- data/ext/smarter_json/vendor/ryu.h +819 -0
- data/ext/smarter_json/vendor/ryu.md +22 -0
- data/lib/smarter_json/errors.rb +28 -0
- data/lib/smarter_json/generator.rb +117 -0
- data/lib/smarter_json/parser.rb +926 -0
- data/lib/smarter_json/version.rb +5 -0
- data/lib/smarter_json.rb +24 -0
- metadata +86 -0
data/docs/options.md
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
|
|
2
|
+
### Contents
|
|
3
|
+
|
|
4
|
+
* [Introduction](./_introduction.md)
|
|
5
|
+
* [The Basic Read API](./basic_read_api.md)
|
|
6
|
+
* [The Basic Write API](./basic_write_api.md)
|
|
7
|
+
* [**Configuration Options**](./options.md)
|
|
8
|
+
* [Examples](./examples.md)
|
|
9
|
+
|
|
10
|
+
--------------
|
|
11
|
+
|
|
12
|
+
# Configuration Options
|
|
13
|
+
|
|
14
|
+
## Reading
|
|
15
|
+
|
|
16
|
+
These options are passed to [`SmarterJSON.process`](./basic_read_api.md) and `SmarterJSON.process_file` as the second argument; anything you set overrides the defaults below.
|
|
17
|
+
|
|
18
|
+
| Option | Default | Explanation |
|
|
19
|
+
|-------------------|--------------|------------------------------------------------------------------------------------------------------------------------|
|
|
20
|
+
| `:symbolize_keys` | `false` | Return object keys as Symbols instead of Strings. |
|
|
21
|
+
| `:duplicate_key` | `:last_wins` | How to handle a key that repeats within one object: `:last_wins`, `:first_wins`, or `:raise`. |
|
|
22
|
+
| `:bigdecimal_load`| `:auto` | `:auto` keeps high-precision decimals as `BigDecimal` (matches Oj); `:float` forces every number to `Float`; `:bigdecimal` forces every decimal to `BigDecimal`. |
|
|
23
|
+
| `:acceleration` | `true` | Use the C extension when it is compiled and loadable; `false` forces the pure-Ruby parser. Both produce identical results. |
|
|
24
|
+
| `:encoding` | `nil` | Labels the input's encoding (e.g. `"UTF-8"`). It does **not** trigger a transcoding pass — see below. |
|
|
25
|
+
|
|
26
|
+
```ruby
|
|
27
|
+
SmarterJSON.process('{"a": 1}', symbolize_keys: true) # => {:a=>1}
|
|
28
|
+
SmarterJSON.process('{"a":1,"a":2}', duplicate_key: :raise) # raises SmarterJSON::ParseError
|
|
29
|
+
SmarterJSON.process(big_decimal_json, bigdecimal_load: :float) # every number as Float (fastest)
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### A note on `:encoding`
|
|
33
|
+
|
|
34
|
+
`:encoding` labels what the input *is* — it does not transcode. The parser works on the bytes in their native encoding and emits string values with the same encoding tag, the same way `smarter_csv` handles encodings. Bytes that are invalid for the claimed encoding raise `SmarterJSON::EncodingError` (a kind of `SmarterJSON::ParseError`). A UTF-8 BOM is handled automatically; UTF-16 / UTF-32 input is out of scope.
|
|
35
|
+
|
|
36
|
+
### A note on `:bigdecimal_load`
|
|
37
|
+
|
|
38
|
+
The default `:auto` preserves high-precision numbers as `BigDecimal`, matching Oj's default. That is intrinsically slower than producing `Float` on number-heavy files (e.g. `canada.json`). For raw speed when you don't need the extra precision, pass `bigdecimal_load: :float`.
|
|
39
|
+
|
|
40
|
+
## Writing
|
|
41
|
+
|
|
42
|
+
This option is passed to [`SmarterJSON.generate`](./basic_write_api.md) as the second argument.
|
|
43
|
+
|
|
44
|
+
| Option | Default | Explanation |
|
|
45
|
+
|------------|---------|-----------------------------------------------------------------------------------------------------------------------------|
|
|
46
|
+
| `:format` | `:json` | `:json` writes standard JSON (Hash → object, Array → array, scalar → scalar). `:ndjson` writes newline-delimited JSON: an Array becomes one element per line, any other value becomes a single line. |
|
|
47
|
+
|
|
48
|
+
Any other `:format` value raises `ArgumentError`.
|
|
49
|
+
|
|
50
|
+
```ruby
|
|
51
|
+
SmarterJSON.generate([1, 2, 3]) # => "[1,2,3]" (default :json — a single JSON array)
|
|
52
|
+
SmarterJSON.generate([1, 2, 3], format: :ndjson) # => "1\n2\n3\n" (one element per line)
|
|
53
|
+
SmarterJSON.generate({}, format: :bogus) # raises ArgumentError
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
---------------
|
|
57
|
+
|
|
58
|
+
PREVIOUS: [The Basic Write API](./basic_write_api.md) | NEXT: [Examples](./examples.md) | UP: [README](../README.md)
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "mkmf"
|
|
4
|
+
require "rbconfig"
|
|
5
|
+
|
|
6
|
+
# Ruby sometimes ships CFLAGS with "-g -O3"; drop the debug half so the
|
|
7
|
+
# extension is built optimized, not with debug info.
|
|
8
|
+
if RbConfig::MAKEFILE_CONFIG["CFLAGS"].include?("-g -O3")
|
|
9
|
+
RbConfig::MAKEFILE_CONFIG["CFLAGS"] = RbConfig::MAKEFILE_CONFIG["CFLAGS"].sub("-g -O3", "-O3 $(cflags)")
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
optflags = "-O3 -flto -fomit-frame-pointer -DNDEBUG".dup
|
|
13
|
+
# -march=native is skipped on arm64-darwin (Apple clang already targets the host).
|
|
14
|
+
optflags << " -march=native" unless RUBY_PLATFORM.start_with?("arm64-darwin")
|
|
15
|
+
# -fno-semantic-interposition: GCC/Clang only (not MSVC).
|
|
16
|
+
optflags << " -fno-semantic-interposition" unless RUBY_PLATFORM.include?("mswin")
|
|
17
|
+
|
|
18
|
+
CONFIG["optflags"] = optflags
|
|
19
|
+
CONFIG["debugflags"] = ""
|
|
20
|
+
|
|
21
|
+
# rb_enc_interned_str (Ruby 3.0+) lets us intern object keys straight from the
|
|
22
|
+
# input bytes; on older Rubies the C code falls back to a plain new string.
|
|
23
|
+
have_func("rb_enc_interned_str", "ruby.h")
|
|
24
|
+
|
|
25
|
+
# Pre-sized hashes (3.2+) and bulk insert (2.6+) for object building; the C code
|
|
26
|
+
# falls back to rb_hash_new + per-pair aset when these are unavailable.
|
|
27
|
+
have_func("rb_hash_new_capa", "ruby.h")
|
|
28
|
+
have_func("rb_hash_bulk_insert", "ruby.h")
|
|
29
|
+
|
|
30
|
+
create_makefile("smarter_json/smarter_json")
|