mirror of https://github.com/akheron/jansson
no-float no-errno no-file)
This commit is contained in:
parent
6dddf687d8
commit
5c6110b2da
|
@ -54,6 +54,9 @@ project (jansson C)
|
|||
option(JANSSON_BUILD_SHARED_LIBS "Build shared libraries." OFF)
|
||||
option(USE_URANDOM "Use /dev/urandom to seed the hash function." ON)
|
||||
option(USE_WINDOWS_CRYPTOAPI "Use CryptGenRandom to seed the hash function." ON)
|
||||
option(JSON_HAVE_FLOAT "Platform has support for floating point numbers." ON)
|
||||
option(JSON_HAVE_ERRNO "Platform has errno.h and supports errno()." ON)
|
||||
option(JSON_HAVE_FILE "Platform has FILE functions." ON)
|
||||
|
||||
if (MSVC)
|
||||
# This option must match the settings used in your program, in particular if you
|
||||
|
|
|
@ -65,4 +65,18 @@
|
|||
#define JSON_PARSER_MAX_DEPTH 2048
|
||||
|
||||
|
||||
/* If errno.h and errno() are available, define to 1,
|
||||
otherwise to 0. */
|
||||
#cmakedefine JSON_HAVE_ERRNO 1
|
||||
|
||||
|
||||
/* If FILE and its related APIs (fopen, fclose, etc.) are available, define to 1
|
||||
otherwise to 1. */
|
||||
#cmakedefine JSON_HAVE_FILE 1
|
||||
|
||||
|
||||
/* If floating point types are available, define to 1,
|
||||
otherwise to 0. */
|
||||
#cmakedefine JSON_HAVE_FLOAT 1
|
||||
|
||||
#endif
|
||||
|
|
|
@ -58,9 +58,11 @@ void print_json_aux(json_t *element, int indent) {
|
|||
case JSON_INTEGER:
|
||||
print_json_integer(element, indent);
|
||||
break;
|
||||
#if JSON_HAVE_FLOAT
|
||||
case JSON_REAL:
|
||||
print_json_real(element, indent);
|
||||
break;
|
||||
#endif
|
||||
case JSON_TRUE:
|
||||
print_json_true(element, indent);
|
||||
break;
|
||||
|
@ -122,10 +124,12 @@ void print_json_integer(json_t *element, int indent) {
|
|||
printf("JSON Integer: \"%" JSON_INTEGER_FORMAT "\"\n", json_integer_value(element));
|
||||
}
|
||||
|
||||
#if JSON_HAVE_FLOAT
|
||||
void print_json_real(json_t *element, int indent) {
|
||||
print_json_indent(indent);
|
||||
printf("JSON Real: %f\n", json_real_value(element));
|
||||
}
|
||||
#endif
|
||||
|
||||
void print_json_true(json_t *element, int indent) {
|
||||
(void)element;
|
||||
|
|
|
@ -51,6 +51,7 @@ static int dump_to_buffer(const char *buffer, size_t size, void *data)
|
|||
return 0;
|
||||
}
|
||||
|
||||
#if JSON_HAVE_FILE
|
||||
static int dump_to_file(const char *buffer, size_t size, void *data)
|
||||
{
|
||||
FILE *dest = (FILE *)data;
|
||||
|
@ -68,6 +69,7 @@ static int dump_to_fd(const char *buffer, size_t size, void *data)
|
|||
#endif
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* 32 spaces (the maximum indentation size) */
|
||||
static const char whitespace[] = " ";
|
||||
|
@ -239,6 +241,7 @@ static int do_dump(const json_t *json, size_t flags, int depth,
|
|||
return dump(buffer, size, data);
|
||||
}
|
||||
|
||||
#if JSON_HAVE_FLOAT
|
||||
case JSON_REAL:
|
||||
{
|
||||
char buffer[MAX_REAL_STR_LENGTH];
|
||||
|
@ -252,6 +255,7 @@ static int do_dump(const json_t *json, size_t flags, int depth,
|
|||
|
||||
return dump(buffer, size, data);
|
||||
}
|
||||
#endif
|
||||
|
||||
case JSON_STRING:
|
||||
return dump_string(json_string_value(json), json_string_length(json), dump, data, flags);
|
||||
|
@ -459,6 +463,7 @@ size_t json_dumpb(const json_t *json, char *buffer, size_t size, size_t flags)
|
|||
return buf.used;
|
||||
}
|
||||
|
||||
#if JSON_HAVE_FILE
|
||||
int json_dumpf(const json_t *json, FILE *output, size_t flags)
|
||||
{
|
||||
return json_dump_callback(json, dump_to_file, (void *)output, flags);
|
||||
|
@ -484,6 +489,7 @@ int json_dump_file(const json_t *json, const char *path, size_t flags)
|
|||
|
||||
return result;
|
||||
}
|
||||
#endif
|
||||
|
||||
int json_dump_callback(const json_t *json, json_dump_callback_t callback, void *data, size_t flags)
|
||||
{
|
||||
|
|
|
@ -46,7 +46,9 @@ typedef enum {
|
|||
JSON_ARRAY,
|
||||
JSON_STRING,
|
||||
JSON_INTEGER,
|
||||
#if JSON_HAVE_FLOAT
|
||||
JSON_REAL,
|
||||
#endif
|
||||
JSON_TRUE,
|
||||
JSON_FALSE,
|
||||
JSON_NULL
|
||||
|
@ -76,8 +78,12 @@ typedef long json_int_t;
|
|||
#define json_is_array(json) ((json) && json_typeof(json) == JSON_ARRAY)
|
||||
#define json_is_string(json) ((json) && json_typeof(json) == JSON_STRING)
|
||||
#define json_is_integer(json) ((json) && json_typeof(json) == JSON_INTEGER)
|
||||
#if JSON_HAVE_FLOAT
|
||||
#define json_is_real(json) ((json) && json_typeof(json) == JSON_REAL)
|
||||
#define json_is_number(json) (json_is_integer(json) || json_is_real(json))
|
||||
#else
|
||||
#define json_is_number(json) (json_is_integer(json))
|
||||
#endif
|
||||
#define json_is_true(json) ((json) && json_typeof(json) == JSON_TRUE)
|
||||
#define json_is_false(json) ((json) && json_typeof(json) == JSON_FALSE)
|
||||
#define json_boolean_value json_is_true
|
||||
|
@ -93,7 +99,9 @@ json_t *json_stringn(const char *value, size_t len);
|
|||
json_t *json_string_nocheck(const char *value);
|
||||
json_t *json_stringn_nocheck(const char *value, size_t len);
|
||||
json_t *json_integer(json_int_t value);
|
||||
#if JSON_HAVE_FLOAT
|
||||
json_t *json_real(double value);
|
||||
#endif
|
||||
json_t *json_true(void);
|
||||
json_t *json_false(void);
|
||||
#define json_boolean(val) ((val) ? json_true() : json_false())
|
||||
|
@ -174,7 +182,8 @@ enum json_error_code {
|
|||
json_error_duplicate_key,
|
||||
json_error_numeric_overflow,
|
||||
json_error_item_not_found,
|
||||
json_error_index_out_of_range
|
||||
json_error_index_out_of_range,
|
||||
json_error_real_numbers_not_supported
|
||||
};
|
||||
|
||||
static JSON_INLINE enum json_error_code json_error_code(const json_error_t *e) {
|
||||
|
@ -266,15 +275,21 @@ int json_array_insert(json_t *array, size_t ind, json_t *value)
|
|||
const char *json_string_value(const json_t *string);
|
||||
size_t json_string_length(const json_t *string);
|
||||
json_int_t json_integer_value(const json_t *integer);
|
||||
#if JSON_HAVE_FLOAT
|
||||
double json_real_value(const json_t *real);
|
||||
double json_number_value(const json_t *json);
|
||||
#else
|
||||
json_int_t json_number_value(const json_t *json);
|
||||
#endif
|
||||
|
||||
int json_string_set(json_t *string, const char *value);
|
||||
int json_string_setn(json_t *string, const char *value, size_t len);
|
||||
int json_string_set_nocheck(json_t *string, const char *value);
|
||||
int json_string_setn_nocheck(json_t *string, const char *value, size_t len);
|
||||
int json_integer_set(json_t *integer, json_int_t value);
|
||||
#if JSON_HAVE_FLOAT
|
||||
int json_real_set(json_t *real, double value);
|
||||
#endif
|
||||
|
||||
/* pack, unpack */
|
||||
|
||||
|
@ -311,15 +326,19 @@ json_t *json_deep_copy(const json_t *value);
|
|||
#define JSON_REJECT_DUPLICATES 0x1
|
||||
#define JSON_DISABLE_EOF_CHECK 0x2
|
||||
#define JSON_DECODE_ANY 0x4
|
||||
#if JSON_HAVE_FLOAT
|
||||
#define JSON_DECODE_INT_AS_REAL 0x8
|
||||
#endif
|
||||
#define JSON_ALLOW_NUL 0x10
|
||||
|
||||
typedef size_t (*json_load_callback_t)(void *buffer, size_t buflen, void *data);
|
||||
|
||||
json_t *json_loads(const char *input, size_t flags, json_error_t *error);
|
||||
json_t *json_loadb(const char *buffer, size_t buflen, size_t flags, json_error_t *error);
|
||||
#if JSON_HAVE_FILE
|
||||
json_t *json_loadf(FILE *input, size_t flags, json_error_t *error);
|
||||
json_t *json_loadfd(int input, size_t flags, json_error_t *error);
|
||||
#endif
|
||||
json_t *json_load_file(const char *path, size_t flags, json_error_t *error);
|
||||
json_t *json_load_callback(json_load_callback_t callback, void *data, size_t flags, json_error_t *error);
|
||||
|
||||
|
@ -334,15 +353,19 @@ json_t *json_load_callback(json_load_callback_t callback, void *data, size_t fla
|
|||
#define JSON_PRESERVE_ORDER 0x100
|
||||
#define JSON_ENCODE_ANY 0x200
|
||||
#define JSON_ESCAPE_SLASH 0x400
|
||||
#if JSON_HAVE_FLOAT
|
||||
#define JSON_REAL_PRECISION(n) (((n) & 0x1F) << 11)
|
||||
#endif
|
||||
#define JSON_EMBED 0x10000
|
||||
|
||||
typedef int (*json_dump_callback_t)(const char *buffer, size_t size, void *data);
|
||||
|
||||
char *json_dumps(const json_t *json, size_t flags);
|
||||
size_t json_dumpb(const json_t *json, char *buffer, size_t size, size_t flags);
|
||||
#if JSON_HAVE_FILE
|
||||
int json_dumpf(const json_t *json, FILE *output, size_t flags);
|
||||
int json_dumpfd(const json_t *json, int output, size_t flags);
|
||||
#endif
|
||||
int json_dump_file(const json_t *json, const char *path, size_t flags);
|
||||
int json_dump_callback(const json_t *json, json_dump_callback_t callback, void *data, size_t flags);
|
||||
|
||||
|
|
|
@ -48,4 +48,16 @@
|
|||
This limits the depth of e.g. array-within-array constructions. */
|
||||
#define JSON_PARSER_MAX_DEPTH 2048
|
||||
|
||||
/* If errno.h and errno() are available, define to 1,
|
||||
otherwise to 0. */
|
||||
#define JSON_HAVE_ERRNO @json_have_errno@
|
||||
|
||||
/* If FILE and its related APIs (fopen, fclose, etc.) are available, define to 1
|
||||
otherwise to 1. */
|
||||
#define JSON_HAVE_FILE @json_have_file@
|
||||
|
||||
/* If floating point types are available, define to 1,
|
||||
otherwise to 0. */
|
||||
#define JSON_HAVE_FLOAT @json_have_float@
|
||||
|
||||
#endif
|
||||
|
|
|
@ -50,10 +50,12 @@ typedef struct {
|
|||
size_t length;
|
||||
} json_string_t;
|
||||
|
||||
#if JSON_HAVE_FLOAT
|
||||
typedef struct {
|
||||
json_t json;
|
||||
double value;
|
||||
} json_real_t;
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
json_t json;
|
||||
|
@ -63,7 +65,9 @@ typedef struct {
|
|||
#define json_to_object(json_) container_of(json_, json_object_t, json)
|
||||
#define json_to_array(json_) container_of(json_, json_array_t, json)
|
||||
#define json_to_string(json_) container_of(json_, json_string_t, json)
|
||||
#if JSON_HAVE_FLOAT
|
||||
#define json_to_real(json_) container_of(json_, json_real_t, json)
|
||||
#endif
|
||||
#define json_to_integer(json_) container_of(json_, json_integer_t, json)
|
||||
|
||||
/* Create a string by taking ownership of an existing buffer */
|
||||
|
@ -94,10 +98,10 @@ char *jsonp_strndup(const char *str, size_t len);
|
|||
/* Windows compatibility */
|
||||
#if defined(_WIN32) || defined(WIN32)
|
||||
# if defined(_MSC_VER) /* MS compiller */
|
||||
# if (_MSC_VER < 1900) && !defined(snprintf) /* snprintf not defined yet & not introduced */
|
||||
# if (_MSC_VER <= 1915) && !defined(snprintf) /* snprintf not defined yet & not introduced */
|
||||
# define snprintf _snprintf
|
||||
# endif
|
||||
# if (_MSC_VER < 1500) && !defined(vsnprintf) /* vsnprintf not defined yet & not introduced */
|
||||
# if (_MSC_VER <= 1915) && !defined(vsnprintf) /* vsnprintf not defined yet & not introduced */
|
||||
# define vsnprintf(b,c,f,a) _vsnprintf(b,c,f,a)
|
||||
# endif
|
||||
# else /* Other Windows compiller, old definition */
|
||||
|
|
32
src/load.c
32
src/load.c
|
@ -11,10 +11,14 @@
|
|||
|
||||
#include "jansson_private.h"
|
||||
|
||||
#if JSON_HAVE_ERRNO
|
||||
#include <errno.h>
|
||||
#endif
|
||||
#include <limits.h>
|
||||
#if JSON_HAVE_FILE
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#endif
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#ifdef HAVE_UNISTD_H
|
||||
|
@ -74,7 +78,9 @@ typedef struct {
|
|||
size_t len;
|
||||
} string;
|
||||
json_int_t integer;
|
||||
#if JSON_HAVE_FLOAT
|
||||
double real;
|
||||
#endif
|
||||
} value;
|
||||
} lex_t;
|
||||
|
||||
|
@ -495,7 +501,9 @@ static int lex_scan_number(lex_t *lex, int c, json_error_t *error)
|
|||
{
|
||||
const char *saved_text;
|
||||
char *end;
|
||||
#if JSON_HAVE_FLOAT
|
||||
double doubleval;
|
||||
#endif
|
||||
|
||||
lex->token = TOKEN_INVALID;
|
||||
|
||||
|
@ -514,13 +522,21 @@ static int lex_scan_number(lex_t *lex, int c, json_error_t *error)
|
|||
c = lex_get_save(lex, error);
|
||||
while(l_isdigit(c));
|
||||
}
|
||||
#if JSON_HAVE_FLOAT
|
||||
else if(c == '.' || c == 'E' || c == 'e') {
|
||||
error_set(error, lex, json_error_real_numbers_not_supported, "real numbers are not supported");
|
||||
goto out;
|
||||
}
|
||||
#endif
|
||||
else {
|
||||
lex_unget_unsave(lex, c);
|
||||
goto out;
|
||||
}
|
||||
|
||||
#if JSON_HAVE_FLOAT
|
||||
if(!(lex->flags & JSON_DECODE_INT_AS_REAL) &&
|
||||
c != '.' && c != 'E' && c != 'e')
|
||||
#endif
|
||||
{
|
||||
json_int_t intval;
|
||||
|
||||
|
@ -528,6 +544,7 @@ static int lex_scan_number(lex_t *lex, int c, json_error_t *error)
|
|||
|
||||
saved_text = strbuffer_value(&lex->saved_text);
|
||||
|
||||
#if JSON_HAVE_ERRNO
|
||||
errno = 0;
|
||||
intval = json_strtoint(saved_text, &end, 10);
|
||||
if(errno == ERANGE) {
|
||||
|
@ -537,6 +554,9 @@ static int lex_scan_number(lex_t *lex, int c, json_error_t *error)
|
|||
error_set(error, lex, json_error_numeric_overflow, "too big integer");
|
||||
goto out;
|
||||
}
|
||||
#else
|
||||
intval = json_strtoint(saved_text, &end, 10);
|
||||
#endif
|
||||
|
||||
assert(end == saved_text + lex->saved_text.length);
|
||||
|
||||
|
@ -545,6 +565,7 @@ static int lex_scan_number(lex_t *lex, int c, json_error_t *error)
|
|||
return 0;
|
||||
}
|
||||
|
||||
#if JSON_HAVE_FLOAT
|
||||
if(c == '.') {
|
||||
c = lex_get(lex, error);
|
||||
if(!l_isdigit(c)) {
|
||||
|
@ -583,6 +604,7 @@ static int lex_scan_number(lex_t *lex, int c, json_error_t *error)
|
|||
lex->token = TOKEN_REAL;
|
||||
lex->value.real = doubleval;
|
||||
return 0;
|
||||
#endif
|
||||
|
||||
out:
|
||||
return -1;
|
||||
|
@ -841,10 +863,12 @@ static json_t *parse_value(lex_t *lex, size_t flags, json_error_t *error)
|
|||
break;
|
||||
}
|
||||
|
||||
#if JSON_HAVE_FLOAT
|
||||
case TOKEN_REAL: {
|
||||
json = json_real(lex->value.real);
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
|
||||
case TOKEN_TRUE:
|
||||
json = json_true();
|
||||
|
@ -1007,6 +1031,8 @@ json_t *json_loadb(const char *buffer, size_t buflen, size_t flags, json_error_t
|
|||
return result;
|
||||
}
|
||||
|
||||
#if JSON_HAVE_FILE
|
||||
|
||||
json_t *json_loadf(FILE *input, size_t flags, json_error_t *error)
|
||||
{
|
||||
lex_t lex;
|
||||
|
@ -1088,8 +1114,12 @@ json_t *json_load_file(const char *path, size_t flags, json_error_t *error)
|
|||
fp = fopen(path, "rb");
|
||||
if(!fp)
|
||||
{
|
||||
#if JSON_HAVE_ERRNO
|
||||
error_set(error, NULL, json_error_cannot_open_file, "unable to open %s: %s",
|
||||
path, strerror(errno));
|
||||
#else
|
||||
error_set(error, NULL, json_error_cannot_open_file, "unable to open %s", path);
|
||||
#endif
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
@ -1099,6 +1129,8 @@ json_t *json_load_file(const char *path, size_t flags, json_error_t *error)
|
|||
return result;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#define MAX_BUF_LEN 1024
|
||||
|
||||
typedef struct
|
||||
|
|
|
@ -369,8 +369,10 @@ static json_t *pack(scanner_t *s, va_list *ap)
|
|||
case 'I': /* integer from json_int_t */
|
||||
return json_integer(va_arg(*ap, json_int_t));
|
||||
|
||||
#if JSON_HAVE_FLOAT
|
||||
case 'f': /* real */
|
||||
return json_real(va_arg(*ap, double));
|
||||
#endif
|
||||
|
||||
case 'O': /* a json_t object; increments refcount */
|
||||
{
|
||||
|
@ -724,6 +726,7 @@ static int unpack(scanner_t *s, json_t *root, va_list *ap)
|
|||
|
||||
return 0;
|
||||
|
||||
#if JSON_HAVE_FLOAT
|
||||
case 'f':
|
||||
if(root && !json_is_real(root)) {
|
||||
set_error(s, "<validation>", json_error_wrong_type, "Expected real, got %s",
|
||||
|
@ -738,11 +741,17 @@ static int unpack(scanner_t *s, json_t *root, va_list *ap)
|
|||
}
|
||||
|
||||
return 0;
|
||||
#endif
|
||||
|
||||
case 'F':
|
||||
if(root && !json_is_number(root)) {
|
||||
#if JSON_HAVE_FLOAT
|
||||
set_error(s, "<validation>", json_error_wrong_type, "Expected real or integer, got %s",
|
||||
type_name(root));
|
||||
#else
|
||||
set_error(s, "<validation>", json_error_wrong_type, "Expected integer, got %s",
|
||||
type_name(root));
|
||||
#endif
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,12 +1,17 @@
|
|||
#include "jansson_private.h"
|
||||
|
||||
#if JSON_HAVE_FLOAT
|
||||
|
||||
#include <assert.h>
|
||||
#if JSON_HAVE_ERRNO
|
||||
#include <errno.h>
|
||||
#endif
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#ifdef __MINGW32__
|
||||
#undef __NO_ISOCEXT /* ensure stdlib.h will declare prototypes for mingw own 'strtod' replacement, called '__strtod' */
|
||||
#endif
|
||||
#include "jansson_private.h"
|
||||
#include "strbuffer.h"
|
||||
|
||||
/* need jansson_private_config.h to get the correct snprintf */
|
||||
|
@ -73,14 +78,19 @@ int jsonp_strtod(strbuffer_t *strbuffer, double *out)
|
|||
to_locale(strbuffer);
|
||||
#endif
|
||||
|
||||
#if JSON_HAVE_ERRNO
|
||||
errno = 0;
|
||||
value = strtod(strbuffer->value, &end);
|
||||
assert(end == strbuffer->value + strbuffer->length);
|
||||
|
||||
if((value == HUGE_VAL || value == -HUGE_VAL) && errno == ERANGE) {
|
||||
/* Overflow */
|
||||
return -1;
|
||||
}
|
||||
#else
|
||||
value = strtod(strbuffer->value, &end);
|
||||
#endif
|
||||
|
||||
assert(end == strbuffer->value + strbuffer->length);
|
||||
|
||||
*out = value;
|
||||
return 0;
|
||||
|
@ -143,3 +153,4 @@ int jsonp_dtostr(char *buffer, size_t size, double value, int precision)
|
|||
|
||||
return (int)length;
|
||||
}
|
||||
#endif
|
||||
|
|
27
src/value.c
27
src/value.c
|
@ -12,11 +12,14 @@
|
|||
#ifdef HAVE_CONFIG_H
|
||||
#include <jansson_private_config.h>
|
||||
#endif
|
||||
#include "jansson_private.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#if JSON_HAVE_FLOAT
|
||||
#include <math.h>
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_STDINT_H
|
||||
#include <stdint.h>
|
||||
|
@ -24,10 +27,10 @@
|
|||
|
||||
#include "jansson.h"
|
||||
#include "hashtable.h"
|
||||
#include "jansson_private.h"
|
||||
#include "utf.h"
|
||||
|
||||
/* Work around nonstandard isnan() and isinf() implementations */
|
||||
#if JSON_HAVE_FLOAT
|
||||
#ifndef isnan
|
||||
#ifndef __sun
|
||||
static JSON_INLINE int isnan(double x) { return x != x; }
|
||||
|
@ -36,6 +39,7 @@ static JSON_INLINE int isnan(double x) { return x != x; }
|
|||
#ifndef isinf
|
||||
static JSON_INLINE int isinf(double x) { return !isnan(x) && isnan(x - x); }
|
||||
#endif
|
||||
#endif
|
||||
|
||||
static JSON_INLINE void json_init(json_t *json, json_type type)
|
||||
{
|
||||
|
@ -871,6 +875,7 @@ static json_t *json_integer_copy(const json_t *integer)
|
|||
|
||||
/*** real ***/
|
||||
|
||||
#if JSON_HAVE_FLOAT
|
||||
json_t *json_real(double value)
|
||||
{
|
||||
json_real_t *real;
|
||||
|
@ -919,10 +924,11 @@ static json_t *json_real_copy(const json_t *real)
|
|||
{
|
||||
return json_real(json_real_value(real));
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/*** number ***/
|
||||
|
||||
#if JSON_HAVE_FLOAT
|
||||
double json_number_value(const json_t *json)
|
||||
{
|
||||
if(json_is_integer(json))
|
||||
|
@ -932,6 +938,15 @@ double json_number_value(const json_t *json)
|
|||
else
|
||||
return 0.0;
|
||||
}
|
||||
#else
|
||||
json_int_t json_number_value(const json_t *json)
|
||||
{
|
||||
if (json_is_integer(json))
|
||||
return json_integer_value(json);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
/*** simple values ***/
|
||||
|
@ -977,9 +992,11 @@ void json_delete(json_t *json)
|
|||
case JSON_INTEGER:
|
||||
json_delete_integer(json_to_integer(json));
|
||||
break;
|
||||
#if JSON_HAVE_FLOAT
|
||||
case JSON_REAL:
|
||||
json_delete_real(json_to_real(json));
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
@ -1011,8 +1028,10 @@ int json_equal(const json_t *json1, const json_t *json2)
|
|||
return json_string_equal(json1, json2);
|
||||
case JSON_INTEGER:
|
||||
return json_integer_equal(json1, json2);
|
||||
#if JSON_HAVE_FLOAT
|
||||
case JSON_REAL:
|
||||
return json_real_equal(json1, json2);
|
||||
#endif
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
|
@ -1035,8 +1054,10 @@ json_t *json_copy(json_t *json)
|
|||
return json_string_copy(json);
|
||||
case JSON_INTEGER:
|
||||
return json_integer_copy(json);
|
||||
#if JSON_HAVE_FLOAT
|
||||
case JSON_REAL:
|
||||
return json_real_copy(json);
|
||||
#endif
|
||||
case JSON_TRUE:
|
||||
case JSON_FALSE:
|
||||
case JSON_NULL:
|
||||
|
@ -1064,8 +1085,10 @@ json_t *json_deep_copy(const json_t *json)
|
|||
return json_string_copy(json);
|
||||
case JSON_INTEGER:
|
||||
return json_integer_copy(json);
|
||||
#if JSON_HAVE_FLOAT
|
||||
case JSON_REAL:
|
||||
return json_real_copy(json);
|
||||
#endif
|
||||
case JSON_TRUE:
|
||||
case JSON_FALSE:
|
||||
case JSON_NULL:
|
||||
|
|
|
@ -158,7 +158,9 @@ static int cmpfile(const char *str, const char *path, const char *fname)
|
|||
|
||||
int use_conf(char *test_path)
|
||||
{
|
||||
int ret;
|
||||
int ret = 0;
|
||||
|
||||
#if JSON_HAVE_FILE
|
||||
size_t flags = 0;
|
||||
char filename[1024], errstr[1024];
|
||||
char *buffer;
|
||||
|
@ -199,6 +201,7 @@ int use_conf(char *test_path)
|
|||
if (conf.sort_keys)
|
||||
flags |= JSON_SORT_KEYS;
|
||||
|
||||
#if JSON_HAVE_FLOAT
|
||||
if (conf.precision < 0 || conf.precision > 31) {
|
||||
fprintf(stderr, "invalid value for JSON_REAL_PRECISION: %d\n",
|
||||
conf.precision);
|
||||
|
@ -207,6 +210,7 @@ int use_conf(char *test_path)
|
|||
}
|
||||
if (conf.precision)
|
||||
flags |= JSON_REAL_PRECISION(conf.precision);
|
||||
#endif
|
||||
|
||||
if (conf.have_hashseed)
|
||||
json_object_seed(conf.hashseed);
|
||||
|
@ -235,10 +239,12 @@ int use_conf(char *test_path)
|
|||
ret = cmpfile(buffer, test_path, "output");
|
||||
free(buffer);
|
||||
json_decref(json);
|
||||
#endif
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
static int getenv_int(const char *name)
|
||||
{
|
||||
char *value, *end;
|
||||
|
@ -257,6 +263,7 @@ static int getenv_int(const char *name)
|
|||
|
||||
int use_env()
|
||||
{
|
||||
#if JSON_HAVE_FILE
|
||||
int indent, precision;
|
||||
size_t flags = 0;
|
||||
json_t *json;
|
||||
|
@ -289,18 +296,22 @@ int use_env()
|
|||
if(getenv_int("JSON_SORT_KEYS"))
|
||||
flags |= JSON_SORT_KEYS;
|
||||
|
||||
#if JSON_HAVE_FLOAT
|
||||
precision = getenv_int("JSON_REAL_PRECISION");
|
||||
if(precision < 0 || precision > 31) {
|
||||
fprintf(stderr, "invalid value for JSON_REAL_PRECISION: %d\n",
|
||||
precision);
|
||||
return 2;
|
||||
}
|
||||
#endif
|
||||
|
||||
if(getenv("HASHSEED"))
|
||||
json_object_seed(getenv_int("HASHSEED"));
|
||||
|
||||
#if JSON_HAVE_FLOAT
|
||||
if(precision > 0)
|
||||
flags |= JSON_REAL_PRECISION(precision);
|
||||
#endif
|
||||
|
||||
if(getenv_int("STRIP")) {
|
||||
/* Load to memory, strip leading and trailing whitespace */
|
||||
|
@ -342,6 +353,7 @@ int use_env()
|
|||
|
||||
json_dumpf(json, stdout, flags);
|
||||
json_decref(json);
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -72,6 +72,7 @@ static void test_copy_simple(void)
|
|||
json_decref(value);
|
||||
json_decref(copy);
|
||||
|
||||
#if JSON_HAVE_FLOAT
|
||||
/* real */
|
||||
value = json_real(123e9);
|
||||
if(!value)
|
||||
|
@ -87,6 +88,7 @@ static void test_copy_simple(void)
|
|||
fail("invalid refcounts");
|
||||
json_decref(value);
|
||||
json_decref(copy);
|
||||
#endif
|
||||
}
|
||||
|
||||
static void test_deep_copy_simple(void)
|
||||
|
@ -152,6 +154,7 @@ static void test_deep_copy_simple(void)
|
|||
json_decref(value);
|
||||
json_decref(copy);
|
||||
|
||||
#if JSON_HAVE_FLOAT
|
||||
/* real */
|
||||
value = json_real(123e9);
|
||||
if(!value)
|
||||
|
@ -167,6 +170,7 @@ static void test_deep_copy_simple(void)
|
|||
fail("invalid refcounts");
|
||||
json_decref(value);
|
||||
json_decref(copy);
|
||||
#endif
|
||||
}
|
||||
|
||||
static void test_copy_array(void)
|
||||
|
|
|
@ -30,12 +30,14 @@ static void encode_null()
|
|||
if(json_dumpb(NULL, NULL, 0, JSON_ENCODE_ANY) != 0)
|
||||
fail("json_dumps didn't fail for NULL");
|
||||
|
||||
#if JSON_HAVE_FILE
|
||||
if(json_dumpf(NULL, stderr, JSON_ENCODE_ANY) != -1)
|
||||
fail("json_dumpf didn't fail for NULL");
|
||||
|
||||
#ifdef HAVE_UNISTD_H
|
||||
if(json_dumpfd(NULL, STDERR_FILENO, JSON_ENCODE_ANY) != -1)
|
||||
fail("json_dumpfd didn't fail for NULL");
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* Don't test json_dump_file to avoid creating a file */
|
||||
|
@ -142,10 +144,12 @@ static void encode_other_than_array_or_object()
|
|||
json = json_string("foo");
|
||||
if(json_dumps(json, 0) != NULL)
|
||||
fail("json_dumps encoded a string!");
|
||||
#if JSON_HAVE_FILE
|
||||
if(json_dumpf(json, NULL, 0) == 0)
|
||||
fail("json_dumpf encoded a string!");
|
||||
if(json_dumpfd(json, -1, 0) == 0)
|
||||
fail("json_dumpfd encoded a string!");
|
||||
#endif
|
||||
|
||||
result = json_dumps(json, JSON_ENCODE_ANY);
|
||||
if(!result || strcmp(result, "\"foo\"") != 0)
|
||||
|
@ -157,10 +161,12 @@ static void encode_other_than_array_or_object()
|
|||
json = json_integer(42);
|
||||
if(json_dumps(json, 0) != NULL)
|
||||
fail("json_dumps encoded an integer!");
|
||||
#if JSON_HAVE_FILE
|
||||
if(json_dumpf(json, NULL, 0) == 0)
|
||||
fail("json_dumpf encoded an integer!");
|
||||
if(json_dumpfd(json, -1, 0) == 0)
|
||||
fail("json_dumpfd encoded an integer!");
|
||||
#endif
|
||||
|
||||
result = json_dumps(json, JSON_ENCODE_ANY);
|
||||
if(!result || strcmp(result, "42") != 0)
|
||||
|
@ -212,6 +218,7 @@ static void encode_nul_byte()
|
|||
|
||||
static void dump_file()
|
||||
{
|
||||
#if JSON_HAVE_FILE
|
||||
json_t *json;
|
||||
int result;
|
||||
|
||||
|
@ -226,6 +233,7 @@ static void dump_file()
|
|||
|
||||
json_decref(json);
|
||||
remove("json_dump_file.json");
|
||||
#endif
|
||||
}
|
||||
|
||||
static void dumpb()
|
||||
|
@ -252,6 +260,7 @@ static void dumpb()
|
|||
|
||||
static void dumpfd()
|
||||
{
|
||||
#if JSON_HAVE_FILE
|
||||
#ifdef HAVE_UNISTD_H
|
||||
int fds[2] = {-1, -1};
|
||||
json_t *a, *b;
|
||||
|
@ -276,6 +285,7 @@ static void dumpfd()
|
|||
json_decref(a);
|
||||
json_decref(b);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
static void embed()
|
||||
|
|
|
@ -42,6 +42,7 @@ static void test_equal_simple()
|
|||
json_decref(value1);
|
||||
json_decref(value2);
|
||||
|
||||
#if JSON_HAVE_FLOAT
|
||||
/* real */
|
||||
value1 = json_real(1.2);
|
||||
value2 = json_real(1.2);
|
||||
|
@ -59,6 +60,7 @@ static void test_equal_simple()
|
|||
|
||||
json_decref(value1);
|
||||
json_decref(value2);
|
||||
#endif
|
||||
|
||||
/* string */
|
||||
value1 = json_string("foo");
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
|
||||
static void file_not_found()
|
||||
{
|
||||
#if JSON_HAVE_FILE
|
||||
json_t *json;
|
||||
json_error_t error;
|
||||
char *pos;
|
||||
|
@ -34,9 +35,11 @@ static void file_not_found()
|
|||
fail("json_load_file returned an invalid error message");
|
||||
if(json_error_code(&error) != json_error_cannot_open_file)
|
||||
fail("json_load_file returned an invalid error code");
|
||||
#endif
|
||||
}
|
||||
|
||||
static void very_long_file_name() {
|
||||
#if JSON_HAVE_FILE
|
||||
json_t *json;
|
||||
json_error_t error;
|
||||
|
||||
|
@ -50,6 +53,7 @@ static void very_long_file_name() {
|
|||
fail("error source was set incorrectly");
|
||||
if(json_error_code(&error) != json_error_cannot_open_file)
|
||||
fail("error code was set incorrectly");
|
||||
#endif
|
||||
}
|
||||
|
||||
static void reject_duplicates()
|
||||
|
@ -105,6 +109,7 @@ static void decode_any()
|
|||
json_decref(json);
|
||||
}
|
||||
|
||||
#if JSON_HAVE_FLOAT
|
||||
static void decode_int_as_real()
|
||||
{
|
||||
json_t *json;
|
||||
|
@ -147,6 +152,7 @@ static void decode_int_as_real()
|
|||
json_decref(json);
|
||||
|
||||
}
|
||||
#endif
|
||||
|
||||
static void allow_nul()
|
||||
{
|
||||
|
@ -181,6 +187,7 @@ static void load_wrong_args()
|
|||
if (json)
|
||||
fail("json_loadb should return NULL if the first argument is NULL");
|
||||
|
||||
#if JSON_HAVE_FILE
|
||||
json = json_loadf(NULL, 0, &error);
|
||||
if (json)
|
||||
fail("json_loadf should return NULL if the first argument is NULL");
|
||||
|
@ -192,6 +199,7 @@ static void load_wrong_args()
|
|||
json = json_load_file(NULL, 0, &error);
|
||||
if (json)
|
||||
fail("json_load_file should return NULL if the first argument is NULL");
|
||||
#endif
|
||||
}
|
||||
|
||||
static void position()
|
||||
|
@ -238,7 +246,9 @@ static void run_tests()
|
|||
reject_duplicates();
|
||||
disable_eof_check();
|
||||
decode_any();
|
||||
#if JSON_HAVE_FLOAT
|
||||
decode_int_as_real();
|
||||
#endif
|
||||
allow_nul();
|
||||
load_wrong_args();
|
||||
position();
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
#endif
|
||||
static void test_inifity()
|
||||
{
|
||||
#if JSON_HAVE_FLOAT
|
||||
json_t *real = json_real(INFINITY);
|
||||
if (real != NULL)
|
||||
fail("could construct a real from Inf");
|
||||
|
@ -31,6 +32,7 @@ static void test_inifity()
|
|||
fail("real value changed unexpectedly");
|
||||
|
||||
json_decref(real);
|
||||
#endif
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(pop)
|
||||
#endif
|
||||
|
@ -39,22 +41,30 @@ static void test_inifity()
|
|||
|
||||
static void run_tests()
|
||||
{
|
||||
json_t *integer, *real;
|
||||
json_int_t i;
|
||||
json_t *integer;
|
||||
#if JSON_HAVE_FLOAT
|
||||
json_t *real;
|
||||
double d;
|
||||
#endif
|
||||
json_int_t i;
|
||||
|
||||
integer = json_integer(5);
|
||||
#if JSON_HAVE_FLOAT
|
||||
real = json_real(100.1);
|
||||
#endif
|
||||
|
||||
if(!integer)
|
||||
fail("unable to create integer");
|
||||
#if JSON_HAVE_FLOAT
|
||||
if(!real)
|
||||
fail("unable to create real");
|
||||
#endif
|
||||
|
||||
i = json_integer_value(integer);
|
||||
if(i != 5)
|
||||
fail("wrong integer value");
|
||||
|
||||
#if JSON_HAVE_FLOAT
|
||||
d = json_real_value(real);
|
||||
if(d != 100.1)
|
||||
fail("wrong real value");
|
||||
|
@ -66,9 +76,12 @@ static void run_tests()
|
|||
if(d != 100.1)
|
||||
fail("wrong number value");
|
||||
|
||||
json_decref(integer);
|
||||
json_decref(real);
|
||||
#endif
|
||||
|
||||
json_decref(integer);
|
||||
|
||||
#if JSON_HAVE_FLOAT
|
||||
#ifdef NAN
|
||||
real = json_real(NAN);
|
||||
if(real != NULL)
|
||||
|
@ -83,6 +96,7 @@ static void run_tests()
|
|||
|
||||
json_decref(real);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef INFINITY
|
||||
test_inifity();
|
||||
|
|
|
@ -67,6 +67,7 @@ static void run_tests()
|
|||
fail("json_pack integer refcount failed");
|
||||
json_decref(value);
|
||||
|
||||
#if JSON_HAVE_FLOAT
|
||||
/* real */
|
||||
value = json_pack("f", 1.0);
|
||||
if(!json_is_real(value) || json_real_value(value) != 1.0)
|
||||
|
@ -74,6 +75,7 @@ static void run_tests()
|
|||
if(value->refcount != (size_t)1)
|
||||
fail("json_pack real refcount failed");
|
||||
json_decref(value);
|
||||
#endif
|
||||
|
||||
/* string */
|
||||
value = json_pack("s", "test");
|
||||
|
|
|
@ -48,8 +48,10 @@ static void run_tests()
|
|||
if(!json_is_integer(value))
|
||||
fail("json_is_integer failed");
|
||||
|
||||
#if JSON_HAVE_FLOAT
|
||||
if(json_is_real(value))
|
||||
fail("json_is_real failed");
|
||||
#endif
|
||||
|
||||
if(!json_is_number(value))
|
||||
fail("json_is_number failed");
|
||||
|
@ -162,6 +164,7 @@ static void run_tests()
|
|||
|
||||
json_decref(value);
|
||||
|
||||
#if JSON_HAVE_FLOAT
|
||||
value = json_real(123.123);
|
||||
if(!value)
|
||||
fail("json_real failed");
|
||||
|
@ -178,6 +181,7 @@ static void run_tests()
|
|||
fail("invalid number value");
|
||||
|
||||
json_decref(value);
|
||||
#endif
|
||||
|
||||
value = json_true();
|
||||
if(!value)
|
||||
|
|
|
@ -18,7 +18,9 @@ static void run_tests()
|
|||
json_int_t I1;
|
||||
int rv;
|
||||
size_t z;
|
||||
#if JSON_HAVE_FLOAT
|
||||
double f;
|
||||
#endif
|
||||
char *s;
|
||||
|
||||
json_error_t error;
|
||||
|
@ -55,6 +57,7 @@ static void run_tests()
|
|||
fail("json_unpack json_int_t failed");
|
||||
json_decref(j);
|
||||
|
||||
#if JSON_HAVE_FLOAT
|
||||
/* real */
|
||||
j = json_real(1.7);
|
||||
rv = json_unpack(j, "f", &f);
|
||||
|
@ -74,6 +77,7 @@ static void run_tests()
|
|||
if(rv || f != 1.7)
|
||||
fail("json_unpack real (or integer) failed");
|
||||
json_decref(j);
|
||||
#endif
|
||||
|
||||
/* string */
|
||||
j = json_string("foo");
|
||||
|
|
Loading…
Reference in New Issue