oj 3.17.3 → 3.17.8
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 +4 -4
- data/CHANGELOG.md +146 -0
- data/ext/oj/cache.c +72 -39
- data/ext/oj/cache.h +5 -0
- data/ext/oj/circarray.c +6 -2
- data/ext/oj/code.c +38 -28
- data/ext/oj/code.h +2 -0
- data/ext/oj/compat.c +63 -31
- data/ext/oj/custom.c +43 -8
- data/ext/oj/dump.c +120 -41
- data/ext/oj/dump.h +10 -0
- data/ext/oj/dump_compat.c +19 -15
- data/ext/oj/dump_object.c +38 -12
- data/ext/oj/dump_strict.c +2 -1
- data/ext/oj/extconf.rb +7 -0
- data/ext/oj/fast.c +132 -72
- data/ext/oj/intern.c +80 -77
- data/ext/oj/mimic_json.c +5 -29
- data/ext/oj/object.c +47 -17
- data/ext/oj/odd.c +6 -2
- data/ext/oj/oj.c +393 -44
- data/ext/oj/oj.h +10 -3
- data/ext/oj/parse.c +75 -7
- data/ext/oj/parse.h +23 -2
- data/ext/oj/parser.c +219 -71
- data/ext/oj/parser.h +1 -1
- data/ext/oj/rails.c +188 -45
- data/ext/oj/reader.c +22 -6
- data/ext/oj/reader.h +1 -0
- data/ext/oj/resolve.c +16 -6
- data/ext/oj/resolve.h +9 -0
- data/ext/oj/rxclass.c +16 -0
- data/ext/oj/rxclass.h +1 -0
- data/ext/oj/safe.c +62 -48
- data/ext/oj/safe.h +14 -2
- data/ext/oj/saj.c +60 -17
- data/ext/oj/saj2.c +44 -28
- data/ext/oj/simd.h +5 -3
- data/ext/oj/sparse.c +23 -6
- data/ext/oj/stream_writer.c +19 -1
- data/ext/oj/strict.c +42 -4
- data/ext/oj/string_writer.c +40 -10
- data/ext/oj/usual.c +17 -5
- data/ext/oj/util.c +27 -0
- data/ext/oj/util.h +2 -1
- data/ext/oj/val_stack.c +11 -22
- data/ext/oj/val_stack.h +70 -30
- data/ext/oj/wab.c +17 -26
- data/lib/oj/generic_object.rb +82 -0
- data/lib/oj/json.rb +1 -71
- data/lib/oj/mimic.rb +0 -1
- data/lib/oj/version.rb +1 -1
- data/pages/Compatibility.md +1 -1
- data/pages/Custom.md +2 -3
- data/pages/InstallOptions.md +8 -6
- data/pages/Modes.md +1 -1
- data/pages/Options.md +5 -6
- data/pages/Parser.md +2 -2
- data/pages/Rails.md +6 -0
- data/pages/Security.md +21 -7
- metadata +17 -16
data/ext/oj/oj.c
CHANGED
|
@@ -299,7 +299,8 @@ static VALUE only_array_from_string(const char *str) {
|
|
|
299
299
|
* floats, 0 indicates use Ruby
|
|
300
300
|
* - *:float_format* [_String_] the C printf format string for printing floats.
|
|
301
301
|
* Default follows the float_precision and will be changed if float_precision is
|
|
302
|
-
* changed. The string can be no more than 6 bytes
|
|
302
|
+
* changed. The string can be no more than 6 bytes and can hold at most one
|
|
303
|
+
* directive, which must be one of aAeEfgG and take no argument of its own.
|
|
303
304
|
* - *:use_to_json* [_Boolean_|_nil_] call to_json() methods on dump, default is false
|
|
304
305
|
* - *:use_as_json* [_Boolean_|_nil_] call as_json() methods on dump, default is false
|
|
305
306
|
* - *:use_raw_json* [_Boolean_|_nil_] call raw_json() methods on dump, default is false
|
|
@@ -574,7 +575,8 @@ static VALUE get_def_opts(VALUE self) {
|
|
|
574
575
|
* when dumping the seconds portion of time.
|
|
575
576
|
* - *:float_format* [_String_] the C printf format string for printing floats.
|
|
576
577
|
* Default follows the float_precision and will be changed if float_precision
|
|
577
|
-
* is changed. The string can be no more than 6 bytes
|
|
578
|
+
* is changed. The string can be no more than 6 bytes and can hold at most one
|
|
579
|
+
* directive, which must be one of aAeEfgG and take no argument of its own.
|
|
578
580
|
* - *:float_precision* [_Fixnum_|_nil_] number of digits of precision when dumping floats, 0 indicates use Ruby.
|
|
579
581
|
* - *:use_to_json* [_Boolean_|_nil_] call to_json() methods on dump, default is false.
|
|
580
582
|
* - *:use_as_json* [_Boolean_|_nil_] call as_json() methods on dump, default is false.
|
|
@@ -606,9 +608,52 @@ static VALUE get_def_opts(VALUE self) {
|
|
|
606
608
|
* - *:trace* [_Boolean_] turn trace on or off.
|
|
607
609
|
* - *:safe* [_Boolean_] turn safe mimic on or off.
|
|
608
610
|
*/
|
|
611
|
+
static ID oj_default_options_keeper_id = 0;
|
|
612
|
+
|
|
613
|
+
// The default options are a plain C global so the Ruby objects in them, the
|
|
614
|
+
// classes of the hash_class, array_class, and ignore options and the regexps
|
|
615
|
+
// and the classes of the match_string option, can not be reached by the GC and
|
|
616
|
+
// are collected while the defaults still refer to them. Holding them in an
|
|
617
|
+
// array the GC does know about keeps them alive. The array is rebuilt whenever
|
|
618
|
+
// the defaults change and replaces the previous one so that only the objects
|
|
619
|
+
// the defaults are currently using are held.
|
|
620
|
+
static void keep_default_options(void) {
|
|
621
|
+
VALUE keep = rb_ary_new();
|
|
622
|
+
|
|
623
|
+
if (0 == oj_default_options_keeper_id) {
|
|
624
|
+
oj_default_options_keeper_id = rb_intern("@default_options_keeper");
|
|
625
|
+
}
|
|
626
|
+
if (Qnil != oj_default_options.hash_class) {
|
|
627
|
+
rb_ary_push(keep, oj_default_options.hash_class);
|
|
628
|
+
}
|
|
629
|
+
if (Qnil != oj_default_options.array_class) {
|
|
630
|
+
rb_ary_push(keep, oj_default_options.array_class);
|
|
631
|
+
}
|
|
632
|
+
if (NULL != oj_default_options.ignore) {
|
|
633
|
+
VALUE *vp;
|
|
634
|
+
|
|
635
|
+
for (vp = oj_default_options.ignore; Qnil != *vp; vp++) {
|
|
636
|
+
rb_ary_push(keep, *vp);
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
oj_rxclass_keep(&oj_default_options.str_rx, keep);
|
|
640
|
+
// An instance variable of the Oj module is a reference the GC can follow on
|
|
641
|
+
// every engine, unlike a C global.
|
|
642
|
+
rb_ivar_set(Oj, oj_default_options_keeper_id, keep);
|
|
643
|
+
}
|
|
644
|
+
|
|
609
645
|
static VALUE set_def_opts(VALUE self, VALUE opts) {
|
|
610
646
|
Check_Type(opts, T_HASH);
|
|
647
|
+
// A :match_string option replaces the regexps instead of adding to them so
|
|
648
|
+
// the ones the defaults are holding have to be freed here, the only place
|
|
649
|
+
// that owns them. A per call options struct only aliases them.
|
|
650
|
+
if (Qnil != rb_hash_lookup(opts, match_string_sym)) {
|
|
651
|
+
oj_rxclass_cleanup(&oj_default_options.str_rx);
|
|
652
|
+
oj_default_options.str_rx.head = NULL;
|
|
653
|
+
oj_default_options.str_rx.tail = NULL;
|
|
654
|
+
}
|
|
611
655
|
oj_parse_options(opts, &oj_default_options);
|
|
656
|
+
keep_default_options();
|
|
612
657
|
|
|
613
658
|
return Qnil;
|
|
614
659
|
}
|
|
@@ -662,6 +707,46 @@ bool set_yesno_options(VALUE key, VALUE value, Options copts) {
|
|
|
662
707
|
return false;
|
|
663
708
|
}
|
|
664
709
|
|
|
710
|
+
inline static bool in_set(const char *set, char c) {
|
|
711
|
+
return '\0' != c && NULL != strchr(set, c);
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
// The format is used with one double and nothing else, so a directive that
|
|
715
|
+
// asks for anything more takes it from whatever is left in the argument
|
|
716
|
+
// registers. %s reads it as a pointer and %n writes through it.
|
|
717
|
+
static void validate_float_format(const char *str, size_t len) {
|
|
718
|
+
const char *s = str;
|
|
719
|
+
const char *end = str + len;
|
|
720
|
+
int cnt = 0;
|
|
721
|
+
|
|
722
|
+
for (; s < end; s++) {
|
|
723
|
+
if ('%' != *s) {
|
|
724
|
+
continue;
|
|
725
|
+
}
|
|
726
|
+
s++;
|
|
727
|
+
if (s < end && '%' == *s) {
|
|
728
|
+
continue;
|
|
729
|
+
}
|
|
730
|
+
if (1 < ++cnt) {
|
|
731
|
+
rb_raise(rb_eArgError, ":float_format must not have more than one directive.");
|
|
732
|
+
}
|
|
733
|
+
for (; s < end && in_set("-+ #0", *s); s++) {
|
|
734
|
+
}
|
|
735
|
+
for (; s < end && '0' <= *s && *s <= '9'; s++) {
|
|
736
|
+
}
|
|
737
|
+
if (s < end && '.' == *s) {
|
|
738
|
+
for (s++; s < end && '0' <= *s && *s <= '9'; s++) {
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
if (s < end && 'l' == *s) {
|
|
742
|
+
s++;
|
|
743
|
+
}
|
|
744
|
+
if (end <= s || !in_set("aAeEfgG", *s)) {
|
|
745
|
+
rb_raise(rb_eArgError, ":float_format directive must be one of aAeEfgG and take no argument of its own.");
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
|
|
665
750
|
static const char *make_only_value(VALUE v) {
|
|
666
751
|
switch (rb_type(v)) {
|
|
667
752
|
case RUBY_T_NIL:
|
|
@@ -751,12 +836,14 @@ static const char *make_only_value(VALUE v) {
|
|
|
751
836
|
return NULL;
|
|
752
837
|
}
|
|
753
838
|
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
839
|
+
// Apply one key and value to copts. Returns true if the key named an option Oj
|
|
840
|
+
// knows and false if it did not. An unrecognized key is still ignored rather
|
|
841
|
+
// than an error; the return only lets a caller tell the two cases apart.
|
|
842
|
+
static bool parse_one_option(VALUE k, VALUE v, Options copts) {
|
|
843
|
+
size_t len;
|
|
757
844
|
|
|
758
845
|
if (set_yesno_options(k, v, copts)) {
|
|
759
|
-
return
|
|
846
|
+
return true;
|
|
760
847
|
}
|
|
761
848
|
if (oj_indent_sym == k) {
|
|
762
849
|
switch (rb_type(v)) {
|
|
@@ -882,7 +969,7 @@ static int parse_options_cb(VALUE k, VALUE v, VALUE opts) {
|
|
|
882
969
|
}
|
|
883
970
|
} else if (bigdecimal_load_sym == k) {
|
|
884
971
|
if (Qnil == v) {
|
|
885
|
-
return
|
|
972
|
+
return true;
|
|
886
973
|
}
|
|
887
974
|
|
|
888
975
|
if (bigdecimal_sym == v || Qtrue == v) {
|
|
@@ -898,7 +985,7 @@ static int parse_options_cb(VALUE k, VALUE v, VALUE opts) {
|
|
|
898
985
|
}
|
|
899
986
|
} else if (compat_bigdecimal_sym == k) {
|
|
900
987
|
if (Qnil == v) {
|
|
901
|
-
return
|
|
988
|
+
return true;
|
|
902
989
|
}
|
|
903
990
|
copts->compat_bigdec = (Qtrue == v);
|
|
904
991
|
} else if (oj_decimal_class_sym == k) {
|
|
@@ -910,9 +997,13 @@ static int parse_options_cb(VALUE k, VALUE v, VALUE opts) {
|
|
|
910
997
|
rb_raise(rb_eArgError, ":decimal_class must be BigDecimal or Float.");
|
|
911
998
|
}
|
|
912
999
|
} else if (create_id_sym == k) {
|
|
1000
|
+
// A per call copy of the options aliases the buffer the default
|
|
1001
|
+
// options own, so only the defaults may free it.
|
|
1002
|
+
bool owned = (&oj_default_options == copts && NULL != copts->create_id && oj_json_class != copts->create_id);
|
|
1003
|
+
|
|
913
1004
|
if (Qnil == v) {
|
|
914
|
-
if (
|
|
915
|
-
OJ_R_FREE((char *)
|
|
1005
|
+
if (owned) {
|
|
1006
|
+
OJ_R_FREE((char *)copts->create_id);
|
|
916
1007
|
}
|
|
917
1008
|
copts->create_id = NULL;
|
|
918
1009
|
copts->create_id_len = 0;
|
|
@@ -920,7 +1011,10 @@ static int parse_options_cb(VALUE k, VALUE v, VALUE opts) {
|
|
|
920
1011
|
const char *str = StringValuePtr(v);
|
|
921
1012
|
|
|
922
1013
|
len = RSTRING_LEN(v);
|
|
923
|
-
if (len != copts->create_id_len || 0 != strcmp(copts->create_id, str)) {
|
|
1014
|
+
if (NULL == copts->create_id || len != copts->create_id_len || 0 != strcmp(copts->create_id, str)) {
|
|
1015
|
+
if (owned) {
|
|
1016
|
+
OJ_R_FREE((char *)copts->create_id);
|
|
1017
|
+
}
|
|
924
1018
|
copts->create_id = OJ_R_ALLOC_N(char, len + 1);
|
|
925
1019
|
strcpy((char *)copts->create_id, str);
|
|
926
1020
|
copts->create_id_len = len;
|
|
@@ -986,7 +1080,7 @@ static int parse_options_cb(VALUE k, VALUE v, VALUE opts) {
|
|
|
986
1080
|
}
|
|
987
1081
|
} else if (nan_sym == k) {
|
|
988
1082
|
if (Qnil == v) {
|
|
989
|
-
return
|
|
1083
|
+
return true;
|
|
990
1084
|
}
|
|
991
1085
|
if (null_sym == v) {
|
|
992
1086
|
copts->dump_opts.nan_dump = NullNan;
|
|
@@ -1003,7 +1097,7 @@ static int parse_options_cb(VALUE k, VALUE v, VALUE opts) {
|
|
|
1003
1097
|
}
|
|
1004
1098
|
} else if (omit_nil_sym == k) {
|
|
1005
1099
|
if (Qnil == v) {
|
|
1006
|
-
return
|
|
1100
|
+
return true;
|
|
1007
1101
|
}
|
|
1008
1102
|
if (Qtrue == v) {
|
|
1009
1103
|
copts->dump_opts.omit_nil = true;
|
|
@@ -1014,7 +1108,7 @@ static int parse_options_cb(VALUE k, VALUE v, VALUE opts) {
|
|
|
1014
1108
|
}
|
|
1015
1109
|
} else if (omit_null_byte_sym == k) {
|
|
1016
1110
|
if (Qnil == v) {
|
|
1017
|
-
return
|
|
1111
|
+
return true;
|
|
1018
1112
|
}
|
|
1019
1113
|
if (Qtrue == v) {
|
|
1020
1114
|
copts->dump_opts.omit_null_byte = true;
|
|
@@ -1052,7 +1146,11 @@ static int parse_options_cb(VALUE k, VALUE v, VALUE opts) {
|
|
|
1052
1146
|
copts->array_class = v;
|
|
1053
1147
|
}
|
|
1054
1148
|
} else if (ignore_sym == k) {
|
|
1055
|
-
|
|
1149
|
+
// Only free when replacing the defaults; a per-call copy's ignore still
|
|
1150
|
+
// aliases the default-owned buffer, which oj_free_call_options() frees.
|
|
1151
|
+
if (&oj_default_options == copts) {
|
|
1152
|
+
OJ_R_FREE(copts->ignore);
|
|
1153
|
+
}
|
|
1056
1154
|
copts->ignore = NULL;
|
|
1057
1155
|
if (Qnil != v) {
|
|
1058
1156
|
size_t cnt;
|
|
@@ -1071,7 +1169,7 @@ static int parse_options_cb(VALUE k, VALUE v, VALUE opts) {
|
|
|
1071
1169
|
}
|
|
1072
1170
|
} else if (integer_range_sym == k) {
|
|
1073
1171
|
if (Qnil == v) {
|
|
1074
|
-
return
|
|
1172
|
+
return true;
|
|
1075
1173
|
}
|
|
1076
1174
|
if (rb_obj_class(v) == rb_cRange) {
|
|
1077
1175
|
VALUE min = rb_funcall(v, oj_begin_id, 0);
|
|
@@ -1102,7 +1200,7 @@ static int parse_options_cb(VALUE k, VALUE v, VALUE opts) {
|
|
|
1102
1200
|
}
|
|
1103
1201
|
} else if (symbol_keys_sym == k || oj_symbolize_names_sym == k) {
|
|
1104
1202
|
if (Qnil == v) {
|
|
1105
|
-
return
|
|
1203
|
+
return true;
|
|
1106
1204
|
}
|
|
1107
1205
|
copts->sym_key = (Qtrue == v) ? Yes : No;
|
|
1108
1206
|
|
|
@@ -1122,35 +1220,219 @@ static int parse_options_cb(VALUE k, VALUE v, VALUE opts) {
|
|
|
1122
1220
|
if (6 < RSTRING_LEN(v)) {
|
|
1123
1221
|
rb_raise(rb_eArgError, ":float_format must be 6 bytes or less.");
|
|
1124
1222
|
}
|
|
1223
|
+
validate_float_format(RSTRING_PTR(v), (size_t)RSTRING_LEN(v));
|
|
1125
1224
|
strncpy(copts->float_fmt, RSTRING_PTR(v), (size_t)RSTRING_LEN(v));
|
|
1126
1225
|
copts->float_fmt[RSTRING_LEN(v)] = '\0';
|
|
1127
1226
|
} else if (only_sym == k) {
|
|
1128
|
-
|
|
1227
|
+
// Only free when replacing the defaults; a per-call copy's pointer still
|
|
1228
|
+
// aliases the default-owned buffer, which oj_free_call_options() frees.
|
|
1229
|
+
if (&oj_default_options == copts && NULL != copts->dump_opts.only) {
|
|
1129
1230
|
OJ_R_FREE((void *)copts->dump_opts.only);
|
|
1130
|
-
copts->dump_opts.only = NULL;
|
|
1131
1231
|
}
|
|
1132
1232
|
copts->dump_opts.only = make_only_value(v);
|
|
1133
1233
|
} else if (except_sym == k) {
|
|
1134
|
-
if (NULL != copts->dump_opts.except) {
|
|
1234
|
+
if (&oj_default_options == copts && NULL != copts->dump_opts.except) {
|
|
1135
1235
|
OJ_R_FREE((void *)copts->dump_opts.except);
|
|
1136
|
-
copts->dump_opts.except = NULL;
|
|
1137
1236
|
}
|
|
1138
1237
|
copts->dump_opts.except = make_only_value(v);
|
|
1238
|
+
} else {
|
|
1239
|
+
return false;
|
|
1240
|
+
}
|
|
1241
|
+
return true;
|
|
1242
|
+
}
|
|
1243
|
+
|
|
1244
|
+
// Collects the results of parse_one_option() over every key in a hash.
|
|
1245
|
+
struct _optsParse {
|
|
1246
|
+
Options copts;
|
|
1247
|
+
bool consumed; // some key was an Oj option
|
|
1248
|
+
VALUE orig; // the caller's hash, never modified
|
|
1249
|
+
VALUE rest; // orig, or a copy of it without the Oj options
|
|
1250
|
+
};
|
|
1251
|
+
|
|
1252
|
+
static void drop_from_rest(struct _optsParse *op, VALUE k) {
|
|
1253
|
+
if (op->rest == op->orig) {
|
|
1254
|
+
op->rest = rb_hash_dup(op->orig);
|
|
1255
|
+
}
|
|
1256
|
+
rb_hash_delete(op->rest, k);
|
|
1257
|
+
}
|
|
1258
|
+
|
|
1259
|
+
static int parse_options_cb(VALUE k, VALUE v, VALUE opts) {
|
|
1260
|
+
struct _optsParse *op = (struct _optsParse *)opts;
|
|
1261
|
+
|
|
1262
|
+
if (parse_one_option(k, v, op->copts)) {
|
|
1263
|
+
op->consumed = true;
|
|
1264
|
+
// :only and :except are also read by ActiveSupport's as_json, which
|
|
1265
|
+
// compat mode reaches through to_json, so they are left in.
|
|
1266
|
+
if (only_sym != k && except_sym != k) {
|
|
1267
|
+
drop_from_rest(op, k);
|
|
1268
|
+
}
|
|
1139
1269
|
}
|
|
1140
1270
|
return ST_CONTINUE;
|
|
1141
1271
|
}
|
|
1142
1272
|
|
|
1143
|
-
void
|
|
1273
|
+
static void parse_options(VALUE ropts, Options copts, struct _optsParse *op) {
|
|
1274
|
+
op->copts = copts;
|
|
1275
|
+
op->consumed = false;
|
|
1276
|
+
op->orig = ropts;
|
|
1277
|
+
op->rest = ropts;
|
|
1278
|
+
|
|
1144
1279
|
if (T_HASH != rb_type(ropts)) {
|
|
1145
1280
|
return;
|
|
1146
1281
|
}
|
|
1147
|
-
rb_hash_foreach(ropts, parse_options_cb, (VALUE)
|
|
1282
|
+
rb_hash_foreach(ropts, parse_options_cb, (VALUE)op);
|
|
1283
|
+
if (Qnil != rb_hash_lookup(ropts, match_string_sym)) {
|
|
1284
|
+
op->consumed = true;
|
|
1285
|
+
drop_from_rest(op, match_string_sym);
|
|
1286
|
+
}
|
|
1148
1287
|
oj_parse_opt_match_string(&copts->str_rx, ropts);
|
|
1149
1288
|
|
|
1150
1289
|
copts->dump_opts.use = (0 < copts->dump_opts.indent_size || 0 < copts->dump_opts.after_size ||
|
|
1151
1290
|
0 < copts->dump_opts.before_size || 0 < copts->dump_opts.hash_size ||
|
|
1152
1291
|
0 < copts->dump_opts.array_size);
|
|
1153
|
-
|
|
1292
|
+
}
|
|
1293
|
+
|
|
1294
|
+
// Apply the options in ropts to copts and report whether any key was
|
|
1295
|
+
// recognized. A hash of nothing but unknown keys leaves copts untouched and
|
|
1296
|
+
// returns false.
|
|
1297
|
+
bool oj_parse_options_consumed(VALUE ropts, Options copts) {
|
|
1298
|
+
struct _optsParse op;
|
|
1299
|
+
|
|
1300
|
+
parse_options(ropts, copts, &op);
|
|
1301
|
+
|
|
1302
|
+
return op.consumed;
|
|
1303
|
+
}
|
|
1304
|
+
|
|
1305
|
+
// Apply the options in ropts to copts and return what is left for the
|
|
1306
|
+
// to_json and as_json methods that Oj.dump() calls: ropts itself when it
|
|
1307
|
+
// holds no Oj option, otherwise a copy without them. The json gem's
|
|
1308
|
+
// generator raises on options it does not know as of json 3.0, so Oj's own
|
|
1309
|
+
// must not reach it. ropts is never modified.
|
|
1310
|
+
VALUE oj_parse_options_rest(VALUE ropts, Options copts) {
|
|
1311
|
+
struct _optsParse op;
|
|
1312
|
+
|
|
1313
|
+
parse_options(ropts, copts, &op);
|
|
1314
|
+
|
|
1315
|
+
return op.rest;
|
|
1316
|
+
}
|
|
1317
|
+
|
|
1318
|
+
void oj_parse_options(VALUE ropts, Options copts) {
|
|
1319
|
+
struct _optsParse op;
|
|
1320
|
+
|
|
1321
|
+
parse_options(ropts, copts, &op);
|
|
1322
|
+
}
|
|
1323
|
+
|
|
1324
|
+
// Free the option buffers that oj_parse_options() allocated for a single call.
|
|
1325
|
+
// A per-call options struct is a copy of oj_default_options, so its create_id,
|
|
1326
|
+
// ignore, only, and except members initially alias the ones owned by the
|
|
1327
|
+
// defaults. Only free a member when it differs from the default, i.e. when it
|
|
1328
|
+
// was allocated for this call; the default-owned buffers must be left alone.
|
|
1329
|
+
void oj_free_call_options(Options copts) {
|
|
1330
|
+
if (NULL != copts->create_id && oj_default_options.create_id != copts->create_id) {
|
|
1331
|
+
OJ_R_FREE((char *)copts->create_id);
|
|
1332
|
+
copts->create_id = NULL;
|
|
1333
|
+
copts->create_id_len = 0;
|
|
1334
|
+
}
|
|
1335
|
+
if (NULL != copts->ignore && oj_default_options.ignore != copts->ignore) {
|
|
1336
|
+
OJ_R_FREE(copts->ignore);
|
|
1337
|
+
copts->ignore = NULL;
|
|
1338
|
+
}
|
|
1339
|
+
if (NULL != copts->dump_opts.only && oj_default_options.dump_opts.only != copts->dump_opts.only) {
|
|
1340
|
+
OJ_R_FREE((void *)copts->dump_opts.only);
|
|
1341
|
+
copts->dump_opts.only = NULL;
|
|
1342
|
+
}
|
|
1343
|
+
if (NULL != copts->dump_opts.except && oj_default_options.dump_opts.except != copts->dump_opts.except) {
|
|
1344
|
+
OJ_R_FREE((void *)copts->dump_opts.except);
|
|
1345
|
+
copts->dump_opts.except = NULL;
|
|
1346
|
+
}
|
|
1347
|
+
}
|
|
1348
|
+
|
|
1349
|
+
static char *dup_option_str(const char *str, size_t len) {
|
|
1350
|
+
char *buf = OJ_R_ALLOC_N(char, len + 1);
|
|
1351
|
+
|
|
1352
|
+
memcpy(buf, str, len);
|
|
1353
|
+
buf[len] = '\0';
|
|
1354
|
+
|
|
1355
|
+
return buf;
|
|
1356
|
+
}
|
|
1357
|
+
|
|
1358
|
+
// Give an options struct private copies of the option buffers it still shares
|
|
1359
|
+
// with the defaults so that it owns all of them.
|
|
1360
|
+
//
|
|
1361
|
+
// An options struct that outlives the call it was created in can not rely on
|
|
1362
|
+
// the default-owned buffers it aliases: Oj.default_options= is free to replace
|
|
1363
|
+
// them at any time, which would leave the alias dangling. Call this once, in
|
|
1364
|
+
// the call that creates such an object and after oj_parse_options(), then free
|
|
1365
|
+
// the buffers with oj_options_release() when the object is collected.
|
|
1366
|
+
void oj_options_take_ownership(Options copts) {
|
|
1367
|
+
if (NULL != copts->create_id && oj_default_options.create_id == copts->create_id) {
|
|
1368
|
+
copts->create_id = dup_option_str(copts->create_id, copts->create_id_len);
|
|
1369
|
+
}
|
|
1370
|
+
if (NULL != copts->ignore && oj_default_options.ignore == copts->ignore) {
|
|
1371
|
+
VALUE *vp;
|
|
1372
|
+
VALUE *buf;
|
|
1373
|
+
size_t cnt = 0;
|
|
1374
|
+
|
|
1375
|
+
for (vp = copts->ignore; Qnil != *vp; vp++) {
|
|
1376
|
+
cnt++;
|
|
1377
|
+
}
|
|
1378
|
+
buf = OJ_R_ALLOC_N(VALUE, cnt + 1);
|
|
1379
|
+
memcpy(buf, copts->ignore, sizeof(VALUE) * (cnt + 1));
|
|
1380
|
+
copts->ignore = buf;
|
|
1381
|
+
}
|
|
1382
|
+
if (NULL != copts->dump_opts.only && oj_default_options.dump_opts.only == copts->dump_opts.only) {
|
|
1383
|
+
copts->dump_opts.only = dup_option_str(copts->dump_opts.only, strlen(copts->dump_opts.only));
|
|
1384
|
+
}
|
|
1385
|
+
if (NULL != copts->dump_opts.except && oj_default_options.dump_opts.except == copts->dump_opts.except) {
|
|
1386
|
+
copts->dump_opts.except = dup_option_str(copts->dump_opts.except, strlen(copts->dump_opts.except));
|
|
1387
|
+
}
|
|
1388
|
+
}
|
|
1389
|
+
|
|
1390
|
+
// Free the option buffers of an options struct that owns all of them, i.e. one
|
|
1391
|
+
// that oj_options_take_ownership() was called on. Unlike oj_free_call_options()
|
|
1392
|
+
// this never looks at the defaults, so it is safe to call from a GC free
|
|
1393
|
+
// function, where the defaults may have been replaced since the object was
|
|
1394
|
+
// created.
|
|
1395
|
+
void oj_options_release(Options copts) {
|
|
1396
|
+
if (NULL != copts->create_id) {
|
|
1397
|
+
OJ_R_FREE((char *)copts->create_id);
|
|
1398
|
+
copts->create_id = NULL;
|
|
1399
|
+
copts->create_id_len = 0;
|
|
1400
|
+
}
|
|
1401
|
+
if (NULL != copts->ignore) {
|
|
1402
|
+
OJ_R_FREE(copts->ignore);
|
|
1403
|
+
copts->ignore = NULL;
|
|
1404
|
+
}
|
|
1405
|
+
if (NULL != copts->dump_opts.only) {
|
|
1406
|
+
OJ_R_FREE((void *)copts->dump_opts.only);
|
|
1407
|
+
copts->dump_opts.only = NULL;
|
|
1408
|
+
}
|
|
1409
|
+
if (NULL != copts->dump_opts.except) {
|
|
1410
|
+
OJ_R_FREE((void *)copts->dump_opts.except);
|
|
1411
|
+
copts->dump_opts.except = NULL;
|
|
1412
|
+
}
|
|
1413
|
+
// The str_rx chain is detached from the defaults when the owner is created
|
|
1414
|
+
// so the chain, if there is one, was built for this options struct alone.
|
|
1415
|
+
oj_rxclass_cleanup(&copts->str_rx);
|
|
1416
|
+
}
|
|
1417
|
+
|
|
1418
|
+
// Mark the Ruby objects held by an owned options struct. A long lived object
|
|
1419
|
+
// such as a writer or an encoder can be the only reference to the classes in
|
|
1420
|
+
// the options so without this they could be collected while the owner is still
|
|
1421
|
+
// alive leaving the options with dangling VALUEs.
|
|
1422
|
+
void oj_options_mark(Options copts) {
|
|
1423
|
+
if (Qnil != copts->hash_class) {
|
|
1424
|
+
rb_gc_mark(copts->hash_class);
|
|
1425
|
+
}
|
|
1426
|
+
if (Qnil != copts->array_class) {
|
|
1427
|
+
rb_gc_mark(copts->array_class);
|
|
1428
|
+
}
|
|
1429
|
+
if (NULL != copts->ignore) {
|
|
1430
|
+
VALUE *vp;
|
|
1431
|
+
|
|
1432
|
+
for (vp = copts->ignore; Qnil != *vp; vp++) {
|
|
1433
|
+
rb_gc_mark(*vp);
|
|
1434
|
+
}
|
|
1435
|
+
}
|
|
1154
1436
|
}
|
|
1155
1437
|
|
|
1156
1438
|
static int match_string_cb(VALUE key, VALUE value, VALUE rx) {
|
|
@@ -1307,7 +1589,7 @@ static VALUE load_file(int argc, VALUE *argv, VALUE self) {
|
|
|
1307
1589
|
if (1 > argc) {
|
|
1308
1590
|
rb_raise(rb_eArgError, "Wrong number of arguments to load().");
|
|
1309
1591
|
}
|
|
1310
|
-
path =
|
|
1592
|
+
path = StringValueCStr(*argv);
|
|
1311
1593
|
parse_info_init(&pi);
|
|
1312
1594
|
pi.options = oj_default_options;
|
|
1313
1595
|
pi.handler = Qnil;
|
|
@@ -1444,6 +1726,7 @@ static VALUE dump_ensure(VALUE a) {
|
|
|
1444
1726
|
volatile struct dump_arg *arg = (void *)a;
|
|
1445
1727
|
|
|
1446
1728
|
oj_out_free(arg->out);
|
|
1729
|
+
oj_free_call_options(arg->copts);
|
|
1447
1730
|
|
|
1448
1731
|
return Qnil;
|
|
1449
1732
|
}
|
|
@@ -1459,6 +1742,7 @@ static VALUE dump(int argc, VALUE *argv, VALUE self) {
|
|
|
1459
1742
|
struct dump_arg arg;
|
|
1460
1743
|
struct _out out;
|
|
1461
1744
|
struct _options copts = oj_default_options;
|
|
1745
|
+
VALUE to_json_argv[2];
|
|
1462
1746
|
|
|
1463
1747
|
if (1 > argc) {
|
|
1464
1748
|
rb_raise(rb_eArgError, "wrong number of arguments (0 for 1).");
|
|
@@ -1467,7 +1751,11 @@ static VALUE dump(int argc, VALUE *argv, VALUE self) {
|
|
|
1467
1751
|
copts.dump_opts.nan_dump = WordNan;
|
|
1468
1752
|
}
|
|
1469
1753
|
if (2 == argc) {
|
|
1470
|
-
|
|
1754
|
+
// The options hash is also what to_json and as_json receive, so
|
|
1755
|
+
// hand them the hash without the options Oj itself acted on.
|
|
1756
|
+
to_json_argv[0] = argv[0];
|
|
1757
|
+
to_json_argv[1] = oj_parse_options_rest(argv[1], &copts);
|
|
1758
|
+
argv = to_json_argv;
|
|
1471
1759
|
}
|
|
1472
1760
|
if (CompatMode == copts.mode && copts.escape_mode != ASCIIEsc) {
|
|
1473
1761
|
copts.escape_mode = JSONEsc;
|
|
@@ -1511,7 +1799,6 @@ static VALUE dump(int argc, VALUE *argv, VALUE self) {
|
|
|
1511
1799
|
static VALUE to_json(int argc, VALUE *argv, VALUE self) {
|
|
1512
1800
|
struct _out out;
|
|
1513
1801
|
struct _options copts = oj_default_options;
|
|
1514
|
-
VALUE rstr;
|
|
1515
1802
|
|
|
1516
1803
|
if (1 > argc) {
|
|
1517
1804
|
rb_raise(rb_eArgError, "wrong number of arguments (0 for 1).");
|
|
@@ -1530,16 +1817,71 @@ static VALUE to_json(int argc, VALUE *argv, VALUE self) {
|
|
|
1530
1817
|
out.omit_null_byte = copts.dump_opts.omit_null_byte;
|
|
1531
1818
|
// For obj.to_json or generate nan is not allowed but if called from dump
|
|
1532
1819
|
// it is.
|
|
1533
|
-
|
|
1820
|
+
return oj_dump_obj_to_str(*argv, &copts, &out, argc - 1, argv + 1);
|
|
1821
|
+
}
|
|
1534
1822
|
|
|
1535
|
-
|
|
1536
|
-
|
|
1823
|
+
struct option_buffers {
|
|
1824
|
+
const char *create_id;
|
|
1825
|
+
VALUE *ignore;
|
|
1826
|
+
const char *only;
|
|
1827
|
+
const char *except;
|
|
1828
|
+
};
|
|
1829
|
+
|
|
1830
|
+
struct write_call_arg {
|
|
1831
|
+
Options copts;
|
|
1832
|
+
int argc;
|
|
1833
|
+
VALUE *argv;
|
|
1834
|
+
struct option_buffers base;
|
|
1835
|
+
struct option_buffers own;
|
|
1836
|
+
bool owned;
|
|
1837
|
+
};
|
|
1838
|
+
|
|
1839
|
+
static void option_buffers_get(Options copts, struct option_buffers *bufs) {
|
|
1840
|
+
bufs->create_id = copts->create_id;
|
|
1841
|
+
bufs->ignore = copts->ignore;
|
|
1842
|
+
bufs->only = copts->dump_opts.only;
|
|
1843
|
+
bufs->except = copts->dump_opts.except;
|
|
1844
|
+
}
|
|
1845
|
+
|
|
1846
|
+
static void write_call_options(struct write_call_arg *arg) {
|
|
1847
|
+
oj_options_take_ownership(arg->copts);
|
|
1848
|
+
option_buffers_get(arg->copts, &arg->own);
|
|
1849
|
+
arg->owned = true;
|
|
1850
|
+
if (3 == arg->argc) {
|
|
1851
|
+
oj_parse_options(arg->argv[2], arg->copts);
|
|
1852
|
+
}
|
|
1853
|
+
}
|
|
1854
|
+
|
|
1855
|
+
static void free_call_buffer(const void *cur, const void *own, const void *base) {
|
|
1856
|
+
if (NULL != cur && base != cur) {
|
|
1857
|
+
OJ_R_FREE((void *)cur);
|
|
1858
|
+
}
|
|
1859
|
+
if (NULL != own && base != own && cur != own) {
|
|
1860
|
+
OJ_R_FREE((void *)own);
|
|
1537
1861
|
}
|
|
1538
|
-
|
|
1862
|
+
}
|
|
1539
1863
|
|
|
1540
|
-
|
|
1864
|
+
static VALUE write_call_ensure(VALUE a) {
|
|
1865
|
+
struct write_call_arg *arg = (struct write_call_arg *)a;
|
|
1866
|
+
Options copts = arg->copts;
|
|
1867
|
+
struct option_buffers none = {NULL, NULL, NULL, NULL};
|
|
1868
|
+
struct option_buffers *base = arg->owned ? &none : &arg->base;
|
|
1541
1869
|
|
|
1542
|
-
|
|
1870
|
+
free_call_buffer(copts->create_id, arg->own.create_id, base->create_id);
|
|
1871
|
+
free_call_buffer(copts->ignore, arg->own.ignore, base->ignore);
|
|
1872
|
+
free_call_buffer(copts->dump_opts.only, arg->own.only, base->only);
|
|
1873
|
+
free_call_buffer(copts->dump_opts.except, arg->own.except, base->except);
|
|
1874
|
+
|
|
1875
|
+
return Qnil;
|
|
1876
|
+
}
|
|
1877
|
+
|
|
1878
|
+
static VALUE to_file_body(VALUE a) {
|
|
1879
|
+
struct write_call_arg *arg = (struct write_call_arg *)a;
|
|
1880
|
+
|
|
1881
|
+
write_call_options(arg);
|
|
1882
|
+
oj_write_obj_to_file(arg->argv[1], StringValueCStr(*arg->argv), arg->copts);
|
|
1883
|
+
|
|
1884
|
+
return Qnil;
|
|
1543
1885
|
}
|
|
1544
1886
|
|
|
1545
1887
|
/* Document-method: to_file
|
|
@@ -1553,12 +1895,21 @@ static VALUE to_json(int argc, VALUE *argv, VALUE self) {
|
|
|
1553
1895
|
* - *:circular* [_Boolean_] allow circular references, default: false
|
|
1554
1896
|
*/
|
|
1555
1897
|
static VALUE to_file(int argc, VALUE *argv, VALUE self) {
|
|
1556
|
-
struct _options
|
|
1898
|
+
struct _options copts;
|
|
1899
|
+
struct write_call_arg arg = {&copts, argc, argv};
|
|
1557
1900
|
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1901
|
+
StringValueCStr(*argv);
|
|
1902
|
+
copts = oj_default_options;
|
|
1903
|
+
option_buffers_get(&copts, &arg.base);
|
|
1904
|
+
|
|
1905
|
+
return rb_ensure(to_file_body, (VALUE)&arg, write_call_ensure, (VALUE)&arg);
|
|
1906
|
+
}
|
|
1907
|
+
|
|
1908
|
+
static VALUE to_stream_body(VALUE a) {
|
|
1909
|
+
struct write_call_arg *arg = (struct write_call_arg *)a;
|
|
1910
|
+
|
|
1911
|
+
write_call_options(arg);
|
|
1912
|
+
oj_write_obj_to_stream(arg->argv[1], *arg->argv, arg->copts);
|
|
1562
1913
|
|
|
1563
1914
|
return Qnil;
|
|
1564
1915
|
}
|
|
@@ -1574,14 +1925,12 @@ static VALUE to_file(int argc, VALUE *argv, VALUE self) {
|
|
|
1574
1925
|
* - *:circular* [_Boolean_] allow circular references, default: false
|
|
1575
1926
|
*/
|
|
1576
1927
|
static VALUE to_stream(int argc, VALUE *argv, VALUE self) {
|
|
1577
|
-
struct _options
|
|
1928
|
+
struct _options copts = oj_default_options;
|
|
1929
|
+
struct write_call_arg arg = {&copts, argc, argv};
|
|
1578
1930
|
|
|
1579
|
-
|
|
1580
|
-
oj_parse_options(argv[2], &copts);
|
|
1581
|
-
}
|
|
1582
|
-
oj_write_obj_to_stream(argv[1], *argv, &copts);
|
|
1931
|
+
option_buffers_get(&copts, &arg.base);
|
|
1583
1932
|
|
|
1584
|
-
return
|
|
1933
|
+
return rb_ensure(to_stream_body, (VALUE)&arg, write_call_ensure, (VALUE)&arg);
|
|
1585
1934
|
}
|
|
1586
1935
|
|
|
1587
1936
|
/* Document-method: register_odd
|
data/ext/oj/oj.h
CHANGED
|
@@ -197,6 +197,7 @@ typedef struct _out {
|
|
|
197
197
|
bool allocated;
|
|
198
198
|
bool omit_nil;
|
|
199
199
|
bool omit_null_byte;
|
|
200
|
+
bool key_filter_off; // rails: suppress the :only/:except key filter for an as_json result
|
|
200
201
|
int argc;
|
|
201
202
|
VALUE *argv;
|
|
202
203
|
ROptTable ropts;
|
|
@@ -254,11 +255,18 @@ extern VALUE oj_compat_parse_cstr(int argc, VALUE *argv, char *json, size_t len)
|
|
|
254
255
|
extern VALUE oj_object_parse_cstr(int argc, VALUE *argv, char *json, size_t len);
|
|
255
256
|
extern VALUE oj_custom_parse_cstr(int argc, VALUE *argv, char *json, size_t len);
|
|
256
257
|
|
|
257
|
-
extern bool
|
|
258
|
-
extern void
|
|
258
|
+
extern bool oj_hash_has_key(VALUE hash, VALUE key);
|
|
259
|
+
extern void oj_parse_options(VALUE ropts, Options copts);
|
|
260
|
+
extern bool oj_parse_options_consumed(VALUE ropts, Options copts);
|
|
261
|
+
extern VALUE oj_parse_options_rest(VALUE ropts, Options copts);
|
|
262
|
+
extern void oj_free_call_options(Options copts);
|
|
263
|
+
extern void oj_options_take_ownership(Options copts);
|
|
264
|
+
extern void oj_options_release(Options copts);
|
|
265
|
+
extern void oj_options_mark(Options copts);
|
|
259
266
|
|
|
260
267
|
extern void oj_dump_obj_to_json(VALUE obj, Options copts, Out out);
|
|
261
268
|
extern void oj_dump_obj_to_json_using_params(VALUE obj, Options copts, Out out, int argc, VALUE *argv);
|
|
269
|
+
extern VALUE oj_dump_obj_to_str(VALUE obj, Options copts, Out out, int argc, VALUE *argv);
|
|
262
270
|
extern void oj_write_obj_to_file(VALUE obj, const char *path, Options copts);
|
|
263
271
|
extern void oj_write_obj_to_stream(VALUE obj, VALUE stream, Options copts);
|
|
264
272
|
extern void oj_dump_leaf_to_json(Leaf leaf, Options copts, Out out);
|
|
@@ -321,7 +329,6 @@ extern VALUE oj_max_nesting_sym;
|
|
|
321
329
|
extern VALUE oj_object_class_sym;
|
|
322
330
|
extern VALUE oj_object_nl_sym;
|
|
323
331
|
extern VALUE oj_quirks_mode_sym;
|
|
324
|
-
extern VALUE oj_skip_null_byte_sym;
|
|
325
332
|
extern VALUE oj_space_before_sym;
|
|
326
333
|
extern VALUE oj_space_sym;
|
|
327
334
|
extern VALUE oj_symbolize_names_sym;
|