Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Change use of double to json_double_t to mirror json_int_t #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,24 @@ The `type` field of `json_value` is one of:
Compile-Time Options
--------------------

The following options should be specified both when building the library and when using it in your own project, and should be specified on the command line or in your project's settings, not in any source file.

-DJSON_TRACK_SOURCE

Stores the source location (line and column number) inside each `json_value`.

This is useful for application-level error reporting.

-Djson_int_t=long long

This is the type of `u.integer`, and defaults to a 64-bit signed integer.

-Djson_double_t=double
-DJSON_POW=my_pow
-DJSON_POW_HEADER="your_header.h"

This is the type of `u.dbl` and defaults to `double`, but you may change it to `float` or `long double`. If you are compiling as C++, `std::pow` is used automatically. If you are compiling as C99 or later, the generic `pow` macro from `<tgmath.h>` is used. If you are compiling as C89 or C94, you need to define `JSON_POW` to an equivalent of C99's `powf` or `powl` for `float` and `long double` respectively, otherwise `pow` will be used which will use `double` only. If you do use a custom `JSON_POW` implementation, you should also define `JSON_POW_HEADER` to the header file that defines or declares the function specified by `JOSN_POW`.


Runtime Options
---------------
Expand Down
26 changes: 22 additions & 4 deletions json.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@ const struct _json_value json_value_none;
#include <ctype.h>
#include <math.h>

#ifndef JSON_POW
#ifdef __cplusplus
#include <cmath>
#define JSON_POW std::pow
#else
#define JSON_POW pow
#ifdef __STDC_VERSION__
#if __STDC_VERSION__ >= 199901L
#include <tgmath.h>
#endif
#endif
#endif
#else
#ifdef JSON_POW_HEADER
#include JSON_POW_HEADER
#endif
#endif

typedef unsigned int json_uchar;

static unsigned char hex_value (json_char c)
Expand Down Expand Up @@ -784,7 +802,7 @@ json_value * json_parse_ex (json_settings * settings,
}

top->type = json_double;
top->u.dbl = (double) top->u.integer;
top->u.dbl = (json_double_t) top->u.integer;

num_digits = 0;
continue;
Expand All @@ -799,7 +817,7 @@ json_value * json_parse_ex (json_settings * settings,
goto e_failed;
}

top->u.dbl += ((double) num_fraction) / (pow (10.0, (double) num_digits));
top->u.dbl += ((json_double_t) num_fraction) / (json_double_t) (JSON_POW ((json_double_t) 10.0, (json_double_t) num_digits));
}

if (b == 'e' || b == 'E')
Expand All @@ -809,7 +827,7 @@ json_value * json_parse_ex (json_settings * settings,
if (top->type == json_integer)
{
top->type = json_double;
top->u.dbl = (double) top->u.integer;
top->u.dbl = (json_double_t) top->u.integer;
}

num_digits = 0;
Expand All @@ -825,7 +843,7 @@ json_value * json_parse_ex (json_settings * settings,
goto e_failed;
}

top->u.dbl *= pow (10.0, (double)
top->u.dbl *= (json_double_t) JSON_POW ((json_double_t) 10.0, (json_double_t)
(flags & flag_num_e_negative ? - num_e : num_e));
}

Expand Down
10 changes: 7 additions & 3 deletions json.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
#endif
#endif

#ifndef json_double_t
#define json_double_t double
#endif

#include <stdlib.h>

#ifdef __cplusplus
Expand Down Expand Up @@ -108,7 +112,7 @@ typedef struct _json_value
{
int boolean;
json_int_t integer;
double dbl;
json_double_t dbl;

struct
{
Expand Down Expand Up @@ -236,12 +240,12 @@ typedef struct _json_value
return u.boolean != 0;
}

inline operator double () const
inline operator json_double_t () const
{
switch (type)
{
case json_integer:
return (double) u.integer;
return (json_double_t) u.integer;

case json_double:
return u.dbl;
Expand Down