adding support for int64 parsing

This commit is contained in:
Eyal Gal 2020-06-17 09:58:53 +03:00
parent a110815230
commit bab60ee563
5 changed files with 19 additions and 7 deletions

View File

@ -24,7 +24,7 @@ INSTALL_LIBRARY_PATH = $(DESTDIR)$(PREFIX)/$(LIBRARY_PATH)
INSTALL ?= cp -a
CC = gcc -std=c89
CC = gcc -std=c11
# validate gcc version for use fstack-protector-strong
MIN_GCC_VERSION = "4.9"

10
cJSON.c
View File

@ -44,7 +44,7 @@
#include <limits.h>
#include <ctype.h>
#include <float.h>
#include <stdint.h>
#ifdef ENABLE_LOCALES
#include <locale.h>
#endif
@ -312,6 +312,7 @@ typedef struct
static cJSON_bool parse_number(cJSON * const item, parse_buffer * const input_buffer)
{
double number = 0;
uint64_t number64;
unsigned char *after_end = NULL;
unsigned char number_c_string[64];
unsigned char decimal_point = get_decimal_point();
@ -378,7 +379,12 @@ loop_end:
{
item->valueint = (int)number;
}
number64 = strtoull((const char*)number_c_string, (char**)&after_end, 10);
if(number_c_string == after_end)
{
return false;
}
item->valueint64 = number64;
item->type = cJSON_Number;
input_buffer->offset += (size_t)(after_end - number_c_string);

View File

@ -84,7 +84,7 @@ then using the CJSON_API_VISIBILITY flag to "export" the same symbols the way CJ
#define CJSON_VERSION_PATCH 13
#include <stddef.h>
#include <stdint.h>
/* cJSON Types: */
#define cJSON_Invalid (0)
#define cJSON_False (1 << 0)
@ -117,7 +117,7 @@ typedef struct cJSON
int valueint;
/* The item's number, if type==cJSON_Number */
double valuedouble;
uint64_t valueint64;
/* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
char *string;
size_t valuestring_len;
@ -180,6 +180,7 @@ CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void);
CJSON_PUBLIC(char *) cJSON_GetStringValue(cJSON *item);
CJSON_PUBLIC(size_t) cJSON_GetStringValueLength(cJSON *item);
CJSON_PUBLIC(double) cJSON_GetNumberValue(cJSON *item);
CJSON_PUBLIC(long int) cJSON_Get64NumberValue(cJSON *item);
/* These functions check the type of an item */
CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item);

View File

@ -836,7 +836,7 @@ static int apply_patch(cJSON *object, const cJSON *patch, const cJSON_bool case_
{
if (opcode == REMOVE)
{
static const cJSON invalid = { NULL, NULL, NULL, cJSON_Invalid, NULL, 0, 0, NULL};
static const cJSON invalid = { NULL, NULL, NULL, cJSON_Invalid, NULL, 0, 0, 0, NULL, 0};
overwrite_item(object, invalid);

View File

@ -2,7 +2,7 @@
#include <stdio.h>
#include <string.h>
char JSON_STR[] = "{ \"val\": \"\\u0000\\u0001\", \"vval\":true, \"test_plain\":\"\\r\\n\", \"plain_text\": \"i'm a text\"}";
char JSON_STR[] = "{ \"val\": \"\\u0000\\u0001\", \"vval\":true, \"test_plain\":\"\\r\\n\", \"plain_text\": \"i'm a text\", \"test_int64\": 18446744073709551610}";
int main(void) {
printf("%s\n", JSON_STR);
cJSON *json = cJSON_Parse(JSON_STR);
@ -24,6 +24,11 @@ int main(void) {
if(json3 == NULL) {
printf("could not get val\n");
}
cJSON *json4 = cJSON_GetObjectItem(json, "test_int64");
printf("got int 64 val: %016lx\n", json4->valueint64);
double asDouble = cJSON_GetNumberValue(json4);
int asInt = json4->valueint;
printf("got val: %e %lx %u\n",asDouble, *((uint64_t*)&asDouble), asInt);
printf("val is : *%s*\nlen is : %lu as opposed to: %lu\n", cJSON_GetStringValue(json3), json3->valuestring_len, strlen(json3->valuestring));
cJSON_Delete(json);
return 0;