1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
|
// 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 <errno.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "dump.h"
#include "trace.h"
// Workaround in case INFINITY is not defined in math.h or if the OS is CentOS
#define OJ_INFINITY (1.0 / 0.0)
typedef unsigned long ulong;
static const char inf_val[] = INF_VAL;
static const char ninf_val[] = NINF_VAL;
static const char nan_val[] = NAN_VAL;
static void raise_strict(VALUE obj) {
rb_raise(rb_eTypeError, "Failed to dump %s Object to JSON in strict mode.\n", rb_class2name(rb_obj_class(obj)));
}
// Removed dependencies on math due to problems with CentOS 5.4.
static void dump_float(VALUE obj, int depth, Out out, bool as_ok) {
char buf[64];
char* b;
double d = rb_num2dbl(obj);
int cnt = 0;
if (0.0 == d) {
b = buf;
*b++ = '0';
*b++ = '.';
*b++ = '0';
*b++ = '\0';
cnt = 3;
} else {
NanDump nd = out->opts->dump_opts.nan_dump;
if (AutoNan == nd) {
nd = RaiseNan;
}
if (OJ_INFINITY == d) {
switch (nd) {
case RaiseNan:
case WordNan: raise_strict(obj); break;
case NullNan:
strcpy(buf, "null");
cnt = 4;
break;
case HugeNan:
default:
strcpy(buf, inf_val);
cnt = sizeof(inf_val) - 1;
break;
}
} else if (-OJ_INFINITY == d) {
switch (nd) {
case RaiseNan:
case WordNan: raise_strict(obj); break;
case NullNan:
strcpy(buf, "null");
cnt = 4;
break;
case HugeNan:
default:
strcpy(buf, ninf_val);
cnt = sizeof(ninf_val) - 1;
break;
}
} else if (isnan(d)) {
switch (nd) {
case RaiseNan:
case WordNan: raise_strict(obj); break;
case NullNan:
strcpy(buf, "null");
cnt = 4;
break;
case HugeNan:
default:
strcpy(buf, nan_val);
cnt = sizeof(nan_val) - 1;
break;
}
} else if (d == (double)(long long int)d) {
cnt = snprintf(buf, sizeof(buf), "%.1f", d);
} else if (0 == out->opts->float_prec) {
volatile VALUE rstr = oj_safe_string_convert(obj);
cnt = (int)RSTRING_LEN(rstr);
if ((int)sizeof(buf) <= cnt) {
cnt = sizeof(buf) - 1;
}
memcpy(buf, RSTRING_PTR(rstr), cnt);
buf[cnt] = '\0';
} else {
cnt = oj_dump_float_printf(buf, sizeof(buf), obj, d, out->opts->float_fmt);
}
}
assure_size(out, cnt);
APPEND_CHARS(out->cur, buf, cnt);
*out->cur = '\0';
}
static void dump_array(VALUE a, int depth, Out out, bool as_ok) {
size_t size;
int i, cnt;
int d2 = depth + 1;
if (Yes == out->opts->circular) {
if (0 > oj_check_circular(a, out)) {
oj_dump_nil(Qnil, 0, out, false);
return;
}
}
cnt = (int)RARRAY_LEN(a);
*out->cur++ = '[';
size = 2;
assure_size(out, size);
if (0 == cnt) {
*out->cur++ = ']';
} else {
if (out->opts->dump_opts.use) {
size = d2 * out->opts->dump_opts.indent_size + out->opts->dump_opts.array_size + 1;
} else {
size = d2 * out->indent + 2;
}
assure_size(out, size * cnt);
cnt--;
for (i = 0; i <= cnt; i++) {
if (out->opts->dump_opts.use) {
if (0 < out->opts->dump_opts.array_size) {
APPEND_CHARS(out->cur, out->opts->dump_opts.array_nl, out->opts->dump_opts.array_size);
}
if (0 < out->opts->dump_opts.indent_size) {
int i;
for (i = d2; 0 < i; i--) {
APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
}
}
} else {
fill_indent(out, d2);
}
if (NullMode == out->opts->mode) {
oj_dump_null_val(RARRAY_AREF(a, i), d2, out);
} else {
oj_dump_strict_val(RARRAY_AREF(a, i), d2, out);
}
if (i < cnt) {
*out->cur++ = ',';
}
}
size = depth * out->indent + 1;
assure_size(out, size);
if (out->opts->dump_opts.use) {
// printf("*** d2: %u indent: %u '%s'\n", d2, out->opts->dump_opts->indent_size,
// out->opts->dump_opts->indent);
if (0 < out->opts->dump_opts.array_size) {
APPEND_CHARS(out->cur, out->opts->dump_opts.array_nl, out->opts->dump_opts.array_size);
}
if (0 < out->opts->dump_opts.indent_size) {
int i;
for (i = depth; 0 < i; i--) {
APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
}
}
} else {
fill_indent(out, depth);
}
*out->cur++ = ']';
}
*out->cur = '\0';
}
static int hash_cb(VALUE key, VALUE value, VALUE ov) {
Out out = (Out)ov;
int depth = out->depth;
long size;
int rtype = rb_type(key);
if (rtype != T_STRING && rtype != T_SYMBOL) {
rb_raise(rb_eTypeError,
"In :strict and :null mode all Hash keys must be Strings or Symbols, not %s.\n",
rb_class2name(rb_obj_class(key)));
}
if (out->omit_nil && Qnil == value) {
return ST_CONTINUE;
}
if (!out->opts->dump_opts.use) {
size = depth * out->indent + 1;
assure_size(out, size);
fill_indent(out, depth);
if (rtype == T_STRING) {
oj_dump_str(key, 0, out, false);
} else {
oj_dump_sym(key, 0, out, false);
}
*out->cur++ = ':';
} else {
size = depth * out->opts->dump_opts.indent_size + out->opts->dump_opts.hash_size + 1;
assure_size(out, size);
if (0 < out->opts->dump_opts.hash_size) {
APPEND_CHARS(out->cur, out->opts->dump_opts.hash_nl, out->opts->dump_opts.hash_size);
}
if (0 < out->opts->dump_opts.indent_size) {
int i;
for (i = depth; 0 < i; i--) {
APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
}
}
if (rtype == T_STRING) {
oj_dump_str(key, 0, out, false);
} else {
oj_dump_sym(key, 0, out, false);
}
size = out->opts->dump_opts.before_size + out->opts->dump_opts.after_size + 2;
assure_size(out, size);
if (0 < out->opts->dump_opts.before_size) {
APPEND_CHARS(out->cur, out->opts->dump_opts.before_sep, out->opts->dump_opts.before_size);
}
*out->cur++ = ':';
if (0 < out->opts->dump_opts.after_size) {
APPEND_CHARS(out->cur, out->opts->dump_opts.after_sep, out->opts->dump_opts.after_size);
}
}
if (NullMode == out->opts->mode) {
oj_dump_null_val(value, depth, out);
} else {
oj_dump_strict_val(value, depth, out);
}
out->depth = depth;
*out->cur++ = ',';
return ST_CONTINUE;
}
static void dump_hash(VALUE obj, int depth, Out out, bool as_ok) {
int cnt;
size_t size;
if (Yes == out->opts->circular) {
if (0 > oj_check_circular(obj, out)) {
oj_dump_nil(Qnil, 0, out, false);
return;
}
}
cnt = (int)RHASH_SIZE(obj);
size = depth * out->indent + 2;
assure_size(out, 2);
*out->cur++ = '{';
if (0 == cnt) {
*out->cur++ = '}';
} else {
out->depth = depth + 1;
rb_hash_foreach(obj, hash_cb, (VALUE)out);
if (',' == *(out->cur - 1)) {
out->cur--; // backup to overwrite last comma
}
if (!out->opts->dump_opts.use) {
assure_size(out, size);
fill_indent(out, depth);
} else {
size = depth * out->opts->dump_opts.indent_size + out->opts->dump_opts.hash_size + 1;
assure_size(out, size);
if (0 < out->opts->dump_opts.hash_size) {
APPEND_CHARS(out->cur, out->opts->dump_opts.hash_nl, out->opts->dump_opts.hash_size);
}
if (0 < out->opts->dump_opts.indent_size) {
int i;
for (i = depth; 0 < i; i--) {
APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
}
}
}
*out->cur++ = '}';
}
*out->cur = '\0';
}
static void dump_data_strict(VALUE obj, int depth, Out out, bool as_ok) {
VALUE clas = rb_obj_class(obj);
if (oj_bigdecimal_class == clas) {
volatile VALUE rstr = oj_safe_string_convert(obj);
oj_dump_raw(RSTRING_PTR(rstr), (int)RSTRING_LEN(rstr), out);
} else {
raise_strict(obj);
}
}
static void dump_data_null(VALUE obj, int depth, Out out, bool as_ok) {
VALUE clas = rb_obj_class(obj);
if (oj_bigdecimal_class == clas) {
volatile VALUE rstr = oj_safe_string_convert(obj);
oj_dump_raw(RSTRING_PTR(rstr), (int)RSTRING_LEN(rstr), out);
} else {
oj_dump_nil(Qnil, depth, out, false);
}
}
static DumpFunc strict_funcs[] = {
NULL, // RUBY_T_NONE = 0x00,
dump_data_strict, // RUBY_T_OBJECT = 0x01,
NULL, // RUBY_T_CLASS = 0x02,
NULL, // RUBY_T_MODULE = 0x03,
dump_float, // RUBY_T_FLOAT = 0x04,
oj_dump_str, // RUBY_T_STRING = 0x05,
NULL, // RUBY_T_REGEXP = 0x06,
dump_array, // RUBY_T_ARRAY = 0x07,
dump_hash, // RUBY_T_HASH = 0x08,
NULL, // RUBY_T_STRUCT = 0x09,
oj_dump_bignum, // RUBY_T_BIGNUM = 0x0a,
NULL, // RUBY_T_FILE = 0x0b,
dump_data_strict, // RUBY_T_DATA = 0x0c,
NULL, // RUBY_T_MATCH = 0x0d,
NULL, // RUBY_T_COMPLEX = 0x0e,
NULL, // RUBY_T_RATIONAL = 0x0f,
NULL, // 0x10
oj_dump_nil, // RUBY_T_NIL = 0x11,
oj_dump_true, // RUBY_T_TRUE = 0x12,
oj_dump_false, // RUBY_T_FALSE = 0x13,
oj_dump_sym, // RUBY_T_SYMBOL = 0x14,
oj_dump_fixnum, // RUBY_T_FIXNUM = 0x15,
};
void oj_dump_strict_val(VALUE obj, int depth, Out out) {
int type = rb_type(obj);
TRACE(out->opts->trace, "dump", obj, depth, TraceIn);
if (MAX_DEPTH < depth) {
rb_raise(rb_eNoMemError, "Too deeply nested.\n");
}
if (0 < type && type <= RUBY_T_FIXNUM) {
DumpFunc f = strict_funcs[type];
if (NULL != f) {
f(obj, depth, out, false);
TRACE(out->opts->trace, "dump", obj, depth, TraceOut);
return;
}
}
raise_strict(obj);
}
static DumpFunc null_funcs[] = {
NULL, // RUBY_T_NONE = 0x00,
dump_data_null, // RUBY_T_OBJECT = 0x01,
NULL, // RUBY_T_CLASS = 0x02,
NULL, // RUBY_T_MODULE = 0x03,
dump_float, // RUBY_T_FLOAT = 0x04,
oj_dump_str, // RUBY_T_STRING = 0x05,
NULL, // RUBY_T_REGEXP = 0x06,
dump_array, // RUBY_T_ARRAY = 0x07,
dump_hash, // RUBY_T_HASH = 0x08,
NULL, // RUBY_T_STRUCT = 0x09,
oj_dump_bignum, // RUBY_T_BIGNUM = 0x0a,
NULL, // RUBY_T_FILE = 0x0b,
dump_data_null, // RUBY_T_DATA = 0x0c,
NULL, // RUBY_T_MATCH = 0x0d,
NULL, // RUBY_T_COMPLEX = 0x0e,
NULL, // RUBY_T_RATIONAL = 0x0f,
NULL, // 0x10
oj_dump_nil, // RUBY_T_NIL = 0x11,
oj_dump_true, // RUBY_T_TRUE = 0x12,
oj_dump_false, // RUBY_T_FALSE = 0x13,
oj_dump_sym, // RUBY_T_SYMBOL = 0x14,
oj_dump_fixnum, // RUBY_T_FIXNUM = 0x15,
};
void oj_dump_null_val(VALUE obj, int depth, Out out) {
int type = rb_type(obj);
TRACE(out->opts->trace, "dump", obj, depth, TraceOut);
if (MAX_DEPTH < depth) {
rb_raise(rb_eNoMemError, "Too deeply nested.\n");
}
if (0 < type && type <= RUBY_T_FIXNUM) {
DumpFunc f = null_funcs[type];
if (NULL != f) {
f(obj, depth, out, false);
TRACE(out->opts->trace, "dump", obj, depth, TraceOut);
return;
}
}
oj_dump_nil(Qnil, depth, out, false);
TRACE(out->opts->trace, "dump", Qnil, depth, TraceOut);
}
|