Skip to content

Commit 56a05e2

Browse files
authored
Fix rubocop (#884)
* Address more rubocop warnings
1 parent b50f308 commit 56a05e2

57 files changed

Lines changed: 1727 additions & 3556 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.rubocop.yml

Lines changed: 420 additions & 2 deletions
Large diffs are not rendered by default.

.rubocop_todo.yml

Lines changed: 0 additions & 2216 deletions
This file was deleted.

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## 3.15.0 - 2023-06-03
4+
5+
- Added `omit_null_byte` option when dumping.
6+
37
## 3.14.3 - 2023-04-07
48

59
- Fixed compat parse with optimized Hash when parsing a JSON::GenericObject.

Gemfile

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
source "https://rubygems.org"
1+
# frozen_string_literal: true
2+
3+
source 'https://rubygems.org'
24

35
gemspec
46

57
group :development do
6-
gem "rubocop", "~> 1.47", require: false
7-
gem "rubocop-minitest", "~> 0.28.0", require: false
8+
gem 'minitest', '~> 5'
9+
gem 'rake-compiler', '>= 0.9', '< 2.0'
10+
gem 'rubocop', '~> 1.47', require: false
11+
gem 'rubocop-minitest', '~> 0.28.0', require: false
12+
gem 'test-unit', '~> 3.0'
813
end

Rakefile

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env rake
2+
# frozen_string_literal: true
23

34
require 'bundler/gem_tasks'
45
require 'rake/extensiontask'
@@ -17,21 +18,21 @@ end
1718
=end
1819

1920
task :test_all => [:clean, :compile] do
20-
STDOUT.flush
21+
$stdout.flush
2122
exitcode = 0
2223
status = 0
2324

24-
cmds = "ruby test/tests.rb -v && ruby test/tests_mimic.rb -v && ruby test/tests_mimic_addition.rb -v"
25+
cmds = 'ruby test/tests.rb -v && ruby test/tests_mimic.rb -v && ruby test/tests_mimic_addition.rb -v'
2526

26-
STDOUT.syswrite "\n#{'#'*90}\n#{cmds}\n"
27+
$stdout.syswrite "\n#{'#'*90}\n#{cmds}\n"
2728
Bundler.with_original_env do
2829
status = system(cmds)
2930
end
3031
exitcode = 1 unless status
3132

3233
Dir.glob('test/json_gem/*_test.rb').each do |file|
3334
cmd = "bundle exec ruby -Itest #{file}"
34-
STDOUT.syswrite "\n#{'#'*90}\n#{cmd}\n"
35+
$stdout.syswrite "\n#{'#'*90}\n#{cmd}\n"
3536
Bundler.with_original_env do
3637
ENV['REAL_JSON_GEM'] = '1'
3738
status = system(cmd)
@@ -46,7 +47,7 @@ end
4647
task :default => :test_all
4748

4849
begin
49-
require "rails/version"
50+
require 'rails/version'
5051

5152
Rake::TestTask.new "activesupport#{Rails::VERSION::MAJOR}" do |t|
5253
t.libs << 'test'
@@ -56,12 +57,13 @@ begin
5657
end
5758
Rake::Task[:test_all].enhance ["activesupport#{Rails::VERSION::MAJOR}"]
5859

59-
Rake::TestTask.new "activerecord" do |t|
60+
Rake::TestTask.new 'activerecord' do |t|
6061
t.libs << 'test'
6162
t.pattern = 'test/activerecord/*_test.rb'
6263
t.warning = true
6364
t.verbose = true
6465
end
65-
Rake::Task[:test_all].enhance ["activerecord"]
66-
rescue LoadError
66+
Rake::Task[:test_all].enhance ['activerecord']
67+
rescue LoadError => e
68+
puts "Rake failed #{e}"
6769
end

ext/oj/dump.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,7 @@ void oj_dump_cstr(const char *str, size_t cnt, bool is_sym, bool escape1, Out ou
857857
break;
858858
case '6': // control characters
859859
if (*(uint8_t *)str < 0x80) {
860-
if (0 == (uint8_t)*str && Yes == out->opts->skip_null_byte) {
860+
if (0 == (uint8_t)*str && out->opts->dump_opts.omit_null_byte) {
861861
break;
862862
}
863863
APPEND_CHARS(out->cur, "\\u00", 4);

ext/oj/mimic_json.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,6 @@ static struct _options mimic_object_to_json_options = {0, // indent
723723
"%0.16g", // float_fmt
724724
Qnil, // hash_class
725725
Qnil, // array_class
726-
No, // skip_null_byte
727726
{
728727
// dump_opts
729728
false, // use
@@ -739,6 +738,7 @@ static struct _options mimic_object_to_json_options = {0, // indent
739738
0, // array_size
740739
RaiseNan, // nan_dump
741740
false, // omit_nil
741+
false, // omit_null_byte
742742
100, // max_depth
743743
},
744744
{

ext/oj/oj.c

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ VALUE oj_nanosecond_sym;
9797
VALUE oj_object_class_sym;
9898
VALUE oj_quirks_mode_sym;
9999
VALUE oj_safe_sym;
100-
VALUE oj_skip_null_byte_sym;
101100
VALUE oj_symbolize_names_sym;
102101
VALUE oj_trace_sym;
103102

@@ -136,6 +135,7 @@ static VALUE newline_sym;
136135
static VALUE nilnil_sym;
137136
static VALUE null_sym;
138137
static VALUE object_sym;
138+
static VALUE omit_null_byte_sym;
139139
static VALUE omit_nil_sym;
140140
static VALUE rails_sym;
141141
static VALUE raise_sym;
@@ -208,7 +208,6 @@ struct _options oj_default_options = {
208208
"%0.15g", // float_fmt
209209
Qnil, // hash_class
210210
Qnil, // array_class
211-
No, // skip_null_byte
212211
{
213212
// dump_opts
214213
false, // use
@@ -224,6 +223,7 @@ struct _options oj_default_options = {
224223
0, // array_size
225224
AutoNan, // nan_dump
226225
false, // omit_nil
226+
false, // omit_null_byte
227227
MAX_DEPTH, // max_depth
228228
},
229229
{
@@ -293,6 +293,7 @@ struct _options oj_default_options = {
293293
*used
294294
* - *:array_class* [_Class_|_nil_] Class to use instead of Array on load
295295
* - *:omit_nil* [_true_|_false_] if true Hash and Object attributes with nil values are omitted
296+
* - *:omit_null_byte* [_true_|_false_] if true null bytes in strings will be omitted when dumping
296297
* - *:ignore* [_nil_|_Array_] either nil or an Array of classes to ignore when dumping
297298
* - *:ignore_under* [_Boolean_] if true then attributes that start with _ are ignored when dumping in
298299
*object or custom mode.
@@ -301,7 +302,6 @@ struct _options oj_default_options = {
301302
* - *:integer_range* [_Range_] Dump integers outside range as strings.
302303
* - *:trace* [_true,_|_false_] Trace all load and dump calls, default is false (trace is off)
303304
* - *:safe* [_true,_|_false_] Safe mimic breaks JSON mimic to be safer, default is false (safe is
304-
* - *:skip_null_byte* [_true_|_false_] if true null bytes in strings will be omitted when dumping
305305
*off)
306306
*
307307
* Return [_Hash_] all current option settings.
@@ -387,11 +387,6 @@ static VALUE get_def_opts(VALUE self) {
387387
opts,
388388
cache_keys_sym,
389389
(Yes == oj_default_options.cache_keys) ? Qtrue : ((No == oj_default_options.cache_keys) ? Qfalse : Qnil));
390-
rb_hash_aset(opts,
391-
oj_skip_null_byte_sym,
392-
(Yes == oj_default_options.skip_null_byte)
393-
? Qtrue
394-
: ((No == oj_default_options.skip_null_byte) ? Qfalse : Qnil));
395390

396391
switch (oj_default_options.mode) {
397392
case StrictMode: rb_hash_aset(opts, mode_sym, strict_sym); break;
@@ -468,6 +463,7 @@ static VALUE get_def_opts(VALUE self) {
468463
default: rb_hash_aset(opts, nan_sym, auto_sym); break;
469464
}
470465
rb_hash_aset(opts, omit_nil_sym, oj_default_options.dump_opts.omit_nil ? Qtrue : Qfalse);
466+
rb_hash_aset(opts, omit_null_byte_sym, oj_default_options.dump_opts.omit_null_byte ? Qtrue : Qfalse);
471467
rb_hash_aset(opts, oj_hash_class_sym, oj_default_options.hash_class);
472468
rb_hash_aset(opts, oj_array_class_sym, oj_default_options.array_class);
473469

@@ -594,7 +590,6 @@ bool set_yesno_options(VALUE key, VALUE value, Options copts) {
594590
{ignore_under_sym, &copts->ignore_under},
595591
{oj_create_additions_sym, &copts->create_ok},
596592
{cache_keys_sym, &copts->cache_keys},
597-
{oj_skip_null_byte_sym, &copts->skip_null_byte},
598593
{Qnil, 0}};
599594
YesNoOpt o;
600595

@@ -875,6 +870,17 @@ static int parse_options_cb(VALUE k, VALUE v, VALUE opts) {
875870
} else {
876871
rb_raise(rb_eArgError, ":omit_nil must be true or false.");
877872
}
873+
} else if (omit_null_byte_sym == k) {
874+
if (Qnil == v) {
875+
return ST_CONTINUE;
876+
}
877+
if (Qtrue == v) {
878+
copts->dump_opts.omit_null_byte = true;
879+
} else if (Qfalse == v) {
880+
copts->dump_opts.omit_null_byte = false;
881+
} else {
882+
rb_raise(rb_eArgError, ":omit_null_byte must be true or false.");
883+
}
878884
} else if (oj_ascii_only_sym == k) {
879885
// This is here only for backwards compatibility with the original Oj.
880886
if (Qtrue == v) {
@@ -1289,6 +1295,7 @@ static VALUE dump(int argc, VALUE *argv, VALUE self) {
12891295
oj_out_init(arg.out);
12901296

12911297
arg.out->omit_nil = copts.dump_opts.omit_nil;
1298+
arg.out->omit_null_byte = copts.dump_opts.omit_null_byte;
12921299
arg.out->caller = CALLER_DUMP;
12931300

12941301
return rb_ensure(dump_body, (VALUE)&arg, dump_ensure, (VALUE)&arg);
@@ -1337,6 +1344,7 @@ static VALUE to_json(int argc, VALUE *argv, VALUE self) {
13371344
oj_out_init(&out);
13381345

13391346
out.omit_nil = copts.dump_opts.omit_nil;
1347+
out.omit_null_byte = copts.dump_opts.omit_null_byte;
13401348
// For obj.to_json or generate nan is not allowed but if called from dump
13411349
// it is.
13421350
oj_dump_obj_to_json_using_params(*argv, &copts, &out, argc - 1, argv + 1);
@@ -1977,8 +1985,8 @@ void Init_oj(void) {
19771985
rb_gc_register_address(&oj_quirks_mode_sym);
19781986
oj_safe_sym = ID2SYM(rb_intern("safe"));
19791987
rb_gc_register_address(&oj_safe_sym);
1980-
oj_skip_null_byte_sym = ID2SYM(rb_intern("skip_null_byte"));
1981-
rb_gc_register_address(&oj_skip_null_byte_sym);
1988+
omit_null_byte_sym = ID2SYM(rb_intern("omit_null_byte"));
1989+
rb_gc_register_address(&omit_null_byte_sym);
19821990
oj_space_before_sym = ID2SYM(rb_intern("space_before"));
19831991
rb_gc_register_address(&oj_space_before_sym);
19841992
oj_space_sym = ID2SYM(rb_intern("space"));

ext/oj/oj.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ typedef struct _dumpOpts {
124124
uint8_t array_size;
125125
char nan_dump; // NanDump
126126
bool omit_nil;
127+
bool omit_null_byte;
127128
int max_depth;
128129
} *DumpOpts;
129130

@@ -165,7 +166,6 @@ typedef struct _options {
165166
char float_fmt[7]; // float format for dumping, if empty use Ruby
166167
VALUE hash_class; // class to use in place of Hash on load
167168
VALUE array_class; // class to use in place of Array on load
168-
char skip_null_byte; // YesNo
169169
struct _dumpOpts dump_opts;
170170
struct _rxClass str_rx;
171171
VALUE *ignore; // Qnil terminated array of classes or NULL
@@ -200,6 +200,7 @@ typedef struct _out {
200200
uint32_t hash_cnt;
201201
bool allocated;
202202
bool omit_nil;
203+
bool omit_null_byte;
203204
int argc;
204205
VALUE *argv;
205206
DumpCaller caller; // used for the mimic json only

lib/oj.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# frozen_string_literal: true
2+
3+
# Oj module is defined in oj.c.
14
module Oj
25
end
36

0 commit comments

Comments
 (0)