ext/oj/mimic_json.c in oj-3.14.2 vs ext/oj/mimic_json.c in oj-3.14.3
- old
+ new
@@ -1,11 +1,11 @@
// Copyright (c) 2012, 2017 Peter Ohler. All rights reserved.
// Licensed under the MIT License. See LICENSE file in the project root for license details.
-#include "mem.h"
#include "dump.h"
#include "encode.h"
+#include "mem.h"
#include "oj.h"
#include "parse.h"
extern const char oj_json_class[];
@@ -347,16 +347,15 @@
* Returns [_Object_]
*/
static VALUE mimic_dump_load(int argc, VALUE *argv, VALUE self) {
if (1 > argc) {
rb_raise(rb_eArgError, "wrong number of arguments (0 for 1)");
- } else if (T_STRING == rb_type(*argv)) {
+ }
+ if (T_STRING == rb_type(*argv)) {
return mimic_load(argc, argv, self);
- } else {
- return mimic_dump(argc, argv, self);
}
- return Qnil;
+ return mimic_dump(argc, argv, self);
}
static VALUE mimic_generate_core(int argc, VALUE *argv, Options copts) {
struct _out out;
VALUE rstr;
@@ -366,12 +365,12 @@
}
memset(out.stack_buffer, 0, sizeof(out.stack_buffer));
oj_out_init(&out);
- out.omit_nil = copts->dump_opts.omit_nil;
- out.caller = CALLER_GENERATE;
+ out.omit_nil = copts->dump_opts.omit_nil;
+ out.caller = CALLER_GENERATE;
// For obj.to_json or generate nan is not allowed but if called from dump
// it is.
copts->dump_opts.nan_dump = RaiseNan;
copts->mode = CompatMode;
copts->to_json = Yes;
@@ -387,14 +386,12 @@
oj_dump_obj_to_json_using_params(*argv, copts, &out, argc - 1, argv + 1);
} else {
VALUE active_hack[1];
if (Qundef == state_class) {
- rb_warn(
- "Oj::Rails.mimic_JSON was called implicitly. "
- "Call it explicitly beforehand if you want to remove this warning."
- );
+ rb_warn("Oj::Rails.mimic_JSON was called implicitly. "
+ "Call it explicitly beforehand if you want to remove this warning.");
oj_define_mimic_json(0, NULL, Qnil);
}
active_hack[0] = rb_funcall(state_class, oj_new_id, 0);
oj_dump_obj_to_json_using_params(*argv, copts, &out, 1, active_hack);
}
@@ -465,11 +462,11 @@
if (0 == argc) {
rb_raise(rb_eArgError, "wrong number of arguments (0))");
}
if (1 == argc || Qnil == argv[1]) {
h = rb_hash_new();
- } else {
+ } else {
h = argv[1];
}
if (!oj_hash_has_key(h, oj_indent_sym)) {
rb_hash_aset(h, oj_indent_sym, rb_str_new2(" "));
}
@@ -484,14 +481,12 @@
}
if (!oj_hash_has_key(h, oj_array_nl_sym)) {
rb_hash_aset(h, oj_array_nl_sym, rb_str_new2("\n"));
}
if (Qundef == state_class) {
- rb_warn(
- "Oj::Rails.mimic_JSON was called implicitly. "
- "Call it explicitly beforehand if you want to remove this warning."
- );
+ rb_warn("Oj::Rails.mimic_JSON was called implicitly. "
+ "Call it explicitly beforehand if you want to remove this warning.");
oj_define_mimic_json(0, NULL, Qnil);
}
rargs[1] = rb_funcall(state_class, oj_new_id, 1, h);
copts.str_rx.head = NULL;
@@ -656,27 +651,26 @@
* Sets the create_id tag to look for in JSON document. That key triggers the
* creation of a class with the same name.
*
* - *id* [_nil_|String] new create_id
*
- * Returns [_String_] the id.
+ * Returns [_nil_|_String_] the id.
*/
static VALUE mimic_set_create_id(VALUE self, VALUE id) {
- Check_Type(id, T_STRING);
-
if (NULL != oj_default_options.create_id) {
if (oj_json_class != oj_default_options.create_id) {
OJ_R_FREE((char *)oj_default_options.create_id);
}
oj_default_options.create_id = NULL;
oj_default_options.create_id_len = 0;
}
if (Qnil != id) {
- size_t len = RSTRING_LEN(id) + 1;
+ const char *ptr = StringValueCStr(id);
+ size_t len = RSTRING_LEN(id) + 1;
oj_default_options.create_id = OJ_R_ALLOC_N(char, len);
- strcpy((char *)oj_default_options.create_id, StringValuePtr(id));
+ strcpy((char *)oj_default_options.create_id, ptr);
oj_default_options.create_id_len = len - 1;
}
return id;
}
@@ -761,12 +755,12 @@
copts.str_rx.head = NULL;
copts.str_rx.tail = NULL;
oj_out_init(&out);
- out.omit_nil = copts.dump_opts.omit_nil;
- copts.mode = CompatMode;
- copts.to_json = No;
+ out.omit_nil = copts.dump_opts.omit_nil;
+ copts.mode = CompatMode;
+ copts.to_json = No;
if (1 <= argc && Qnil != argv[0]) {
oj_parse_mimic_dump_options(argv[0], &copts);
}
// To be strict the mimic_object_to_json_options should be used but people
// seem to prefer the option of changing that.