mirror of
https://github.com/DaveGamble/cJSON.git
synced 2023-08-10 21:13:26 +03:00
Compare commits
19 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
de8eaaba89 | ||
|
|
b2da44d6cb | ||
|
|
f6998a6a34 | ||
|
|
e3e0b5150b | ||
|
|
1df987a170 | ||
|
|
ddadb44a67 | ||
|
|
9ef44fc0b6 | ||
|
|
8c58e62597 | ||
|
|
8893e39712 | ||
|
|
3d6ae11340 | ||
|
|
a1f2600883 | ||
|
|
fcc85bdfbc | ||
|
|
7d08a3518a | ||
|
|
cc486a0354 | ||
|
|
fcc89c4bb2 | ||
|
|
a0431e226f | ||
|
|
89edfb6741 | ||
|
|
e69db83de5 | ||
|
|
06008b0444 |
5
.gitignore
vendored
5
.gitignore
vendored
@@ -8,3 +8,8 @@ test
|
|||||||
tags
|
tags
|
||||||
*.dylib
|
*.dylib
|
||||||
build/
|
build/
|
||||||
|
cJSON_test
|
||||||
|
cJSON_test_utils
|
||||||
|
libcjson.so.*
|
||||||
|
libcjson_utils.so.*
|
||||||
|
*.orig
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ include(GNUInstallDirs)
|
|||||||
project(cJSON C)
|
project(cJSON C)
|
||||||
|
|
||||||
set(PROJECT_VERSION_MAJOR 1)
|
set(PROJECT_VERSION_MAJOR 1)
|
||||||
set(PROJECT_VERSION_MINOR 1)
|
set(PROJECT_VERSION_MINOR 2)
|
||||||
set(PROJECT_VERSION_PATCH 0)
|
set(PROJECT_VERSION_PATCH 0)
|
||||||
set(CJSON_VERSION_SO 1)
|
set(CJSON_VERSION_SO 1)
|
||||||
set(CJSON_UTILS_VERSION_SO 1)
|
set(CJSON_UTILS_VERSION_SO 1)
|
||||||
@@ -15,7 +15,7 @@ set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT
|
|||||||
option(ENABLE_CUSTOM_COMPILER_FLAGS "Enables custom compiler flags for Clang and GCC" ON)
|
option(ENABLE_CUSTOM_COMPILER_FLAGS "Enables custom compiler flags for Clang and GCC" ON)
|
||||||
if (ENABLE_CUSTOM_COMPILER_FLAGS)
|
if (ENABLE_CUSTOM_COMPILER_FLAGS)
|
||||||
if(("${CMAKE_C_COMPILER_ID}" MATCHES "GNU") OR ("${CMAKE_C_COMPILER_ID}" MATCHES "Clang"))
|
if(("${CMAKE_C_COMPILER_ID}" MATCHES "GNU") OR ("${CMAKE_C_COMPILER_ID}" MATCHES "Clang"))
|
||||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c89 -pedantic -Wall -Wextra -Werror -Wstrict-prototypes -Wwrite-strings -Wshadow -Winit-self -Wcast-align -Wformat=2 -Wmissing-prototypes -Wstrict-overflow=2")
|
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c89 -pedantic -Wall -Wextra -Werror -Wstrict-prototypes -Wwrite-strings -Wshadow -Winit-self -Wcast-align -Wformat=2 -Wmissing-prototypes -Wstrict-overflow=2 -Wcast-qual -Wc++-compat")
|
||||||
endif()
|
endif()
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ Contributors
|
|||||||
* Ian Mobley
|
* Ian Mobley
|
||||||
* Irwan Djadjadi
|
* Irwan Djadjadi
|
||||||
* [IvanVoid](https://github.com/npi3pak)
|
* [IvanVoid](https://github.com/npi3pak)
|
||||||
|
* [Jiri Zouhar](https://github.com/loigu)
|
||||||
* [Jonathan Fether](https://github.com/jfether)
|
* [Jonathan Fether](https://github.com/jfether)
|
||||||
* [Kevin Branigan](https://github.com/kbranigan)
|
* [Kevin Branigan](https://github.com/kbranigan)
|
||||||
* [Kyle Chisholm](https://github.com/ChisholmKyle)
|
* [Kyle Chisholm](https://github.com/ChisholmKyle)
|
||||||
@@ -24,6 +25,7 @@ Contributors
|
|||||||
* [Rafael Leal Dias](https://github.com/rafaeldias)
|
* [Rafael Leal Dias](https://github.com/rafaeldias)
|
||||||
* [Rod Vagg](https://github.com/rvagg)
|
* [Rod Vagg](https://github.com/rvagg)
|
||||||
* [Roland Meertens](https://github.com/rmeertens)
|
* [Roland Meertens](https://github.com/rmeertens)
|
||||||
|
* [Romain Porte](https://github.com/MicroJoe)
|
||||||
* [Stephan Gatzka](https://github.com/gatzka)
|
* [Stephan Gatzka](https://github.com/gatzka)
|
||||||
* [Weston Schmidt](https://github.com/schmidtw)
|
* [Weston Schmidt](https://github.com/schmidtw)
|
||||||
|
|
||||||
|
|||||||
4
Makefile
4
Makefile
@@ -10,7 +10,7 @@ UTILS_TEST_SRC = cJSON.c cJSON_Utils.c test_utils.c
|
|||||||
|
|
||||||
LDLIBS = -lm
|
LDLIBS = -lm
|
||||||
|
|
||||||
LIBVERSION = 1.1.0
|
LIBVERSION = 1.2.0
|
||||||
CJSON_SOVERSION = 1
|
CJSON_SOVERSION = 1
|
||||||
UTILS_SOVERSION = 1
|
UTILS_SOVERSION = 1
|
||||||
|
|
||||||
@@ -23,7 +23,7 @@ INSTALL_LIBRARY_PATH = $(DESTDIR)$(PREFIX)/$(LIBRARY_PATH)
|
|||||||
|
|
||||||
INSTALL ?= cp -a
|
INSTALL ?= cp -a
|
||||||
|
|
||||||
R_CFLAGS = -fPIC -std=c89 -pedantic -Wall -Werror -Wstrict-prototypes -Wwrite-strings -Wshadow -Winit-self -Wcast-align -Wformat=2 -Wmissing-prototypes -Wstrict-overflow=2 $(CFLAGS)
|
R_CFLAGS = -fPIC -std=c89 -pedantic -Wall -Werror -Wstrict-prototypes -Wwrite-strings -Wshadow -Winit-self -Wcast-align -Wformat=2 -Wmissing-prototypes -Wstrict-overflow=2 -Wcast-qual -Wc++-compat $(CFLAGS)
|
||||||
|
|
||||||
uname := $(shell sh -c 'uname -s 2>/dev/null || echo false')
|
uname := $(shell sh -c 'uname -s 2>/dev/null || echo false')
|
||||||
|
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ make
|
|||||||
And install it with `make install` if you want. By default it installs the headers `/usr/local/include/cjson` and the libraries to `/usr/local/lib`. It also installs files for pkg-config to make it easier to detect and use an existing installation of CMake. And it installs CMake config files, that can be used by other CMake based projects to discover the library.
|
And install it with `make install` if you want. By default it installs the headers `/usr/local/include/cjson` and the libraries to `/usr/local/lib`. It also installs files for pkg-config to make it easier to detect and use an existing installation of CMake. And it installs CMake config files, that can be used by other CMake based projects to discover the library.
|
||||||
|
|
||||||
You can change the build process with a list of different options that you can pass to CMake. Turn them on with `On` and off with `Off`:
|
You can change the build process with a list of different options that you can pass to CMake. Turn them on with `On` and off with `Off`:
|
||||||
* `-DENABLE_CJSON_TESTS=On`: Enable building the tests. (on by default)
|
* `-DENABLE_CJSON_TEST=On`: Enable building the tests. (on by default)
|
||||||
* `-DENABLE_CJSON_UTILS=On`: Enable building cJSON_Utils. (off by default)
|
* `-DENABLE_CJSON_UTILS=On`: Enable building cJSON_Utils. (off by default)
|
||||||
* `-DENABLE_TARGET_EXPORT=On`: Enable the export of CMake targets. Turn off if it makes problems. (on by default)
|
* `-DENABLE_TARGET_EXPORT=On`: Enable the export of CMake targets. Turn off if it makes problems. (on by default)
|
||||||
* `-DENABLE_CUSTOM_COMPILER_FLAGS=On`: Enable custom compiler flags (currently for Clang and GCC). Turn off if it makes problems. (on by default)
|
* `-DENABLE_CUSTOM_COMPILER_FLAGS=On`: Enable custom compiler flags (currently for Clang and GCC). Turn off if it makes problems. (on by default)
|
||||||
@@ -90,7 +90,7 @@ If you are packaging cJSON for a distribution of Linux, you would probably take
|
|||||||
```
|
```
|
||||||
mkdir build
|
mkdir build
|
||||||
cd build
|
cd build
|
||||||
cmake .. -DENABLE_CJSON_UTILS=On -DENABLE_CJSON_TESTS=Off -DCMAKE_INSTALL_PREFIX=/usr
|
cmake .. -DENABLE_CJSON_UTILS=On -DENABLE_CJSON_TEST=Off -DCMAKE_INSTALL_PREFIX=/usr
|
||||||
make
|
make
|
||||||
make DESTDIR=$pkgdir install
|
make DESTDIR=$pkgdir install
|
||||||
```
|
```
|
||||||
|
|||||||
52
cJSON.c
52
cJSON.c
@@ -88,6 +88,11 @@ static char* cJSON_strdup(const char* str)
|
|||||||
size_t len = 0;
|
size_t len = 0;
|
||||||
char *copy = NULL;
|
char *copy = NULL;
|
||||||
|
|
||||||
|
if (str == NULL)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
len = strlen(str) + 1;
|
len = strlen(str) + 1;
|
||||||
if (!(copy = (char*)cJSON_malloc(len)))
|
if (!(copy = (char*)cJSON_malloc(len)))
|
||||||
{
|
{
|
||||||
@@ -491,7 +496,7 @@ static const char *parse_string(cJSON *item, const char *str, const char **ep)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
while ((*end_ptr != '\"') && *end_ptr && ++len)
|
while ((*end_ptr != '\"') && *end_ptr)
|
||||||
{
|
{
|
||||||
if (*end_ptr++ == '\\')
|
if (*end_ptr++ == '\\')
|
||||||
{
|
{
|
||||||
@@ -503,6 +508,7 @@ static const char *parse_string(cJSON *item, const char *str, const char **ep)
|
|||||||
/* Skip escaped quotes. */
|
/* Skip escaped quotes. */
|
||||||
end_ptr++;
|
end_ptr++;
|
||||||
}
|
}
|
||||||
|
len++;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* This is at most how long we need for the string, roughly. */
|
/* This is at most how long we need for the string, roughly. */
|
||||||
@@ -988,6 +994,27 @@ static char *print_value(const cJSON *item, int depth, cjbool fmt, printbuffer *
|
|||||||
case cJSON_Number:
|
case cJSON_Number:
|
||||||
out = print_number(item, p);
|
out = print_number(item, p);
|
||||||
break;
|
break;
|
||||||
|
case cJSON_Raw:
|
||||||
|
{
|
||||||
|
size_t raw_length = 0;
|
||||||
|
if (item->valuestring == NULL)
|
||||||
|
{
|
||||||
|
if (!p->noalloc)
|
||||||
|
{
|
||||||
|
cJSON_free(p->buffer);
|
||||||
|
}
|
||||||
|
out = NULL;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
raw_length = strlen(item->valuestring) + sizeof('\0');
|
||||||
|
out = ensure(p, raw_length);
|
||||||
|
if (out)
|
||||||
|
{
|
||||||
|
memcpy(out, item->valuestring, raw_length);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
case cJSON_String:
|
case cJSON_String:
|
||||||
out = print_string(item, p);
|
out = print_string(item, p);
|
||||||
break;
|
break;
|
||||||
@@ -1015,6 +1042,9 @@ static char *print_value(const cJSON *item, int depth, cjbool fmt, printbuffer *
|
|||||||
case cJSON_Number:
|
case cJSON_Number:
|
||||||
out = print_number(item, 0);
|
out = print_number(item, 0);
|
||||||
break;
|
break;
|
||||||
|
case cJSON_Raw:
|
||||||
|
out = cJSON_strdup(item->valuestring);
|
||||||
|
break;
|
||||||
case cJSON_String:
|
case cJSON_String:
|
||||||
out = print_string(item, 0);
|
out = print_string(item, 0);
|
||||||
break;
|
break;
|
||||||
@@ -1756,7 +1786,10 @@ void cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item)
|
|||||||
{
|
{
|
||||||
cJSON_free(item->string);
|
cJSON_free(item->string);
|
||||||
}
|
}
|
||||||
|
#pragma GCC diagnostic push
|
||||||
|
#pragma GCC diagnostic ignored "-Wcast-qual"
|
||||||
item->string = (char*)string;
|
item->string = (char*)string;
|
||||||
|
#pragma GCC diagnostic pop
|
||||||
item->type |= cJSON_StringIsConst;
|
item->type |= cJSON_StringIsConst;
|
||||||
cJSON_AddItemToArray(object, item);
|
cJSON_AddItemToArray(object, item);
|
||||||
}
|
}
|
||||||
@@ -1984,6 +2017,23 @@ cJSON *cJSON_CreateString(const char *string)
|
|||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extern cJSON *cJSON_CreateRaw(const char *raw)
|
||||||
|
{
|
||||||
|
cJSON *item = cJSON_New_Item();
|
||||||
|
if(item)
|
||||||
|
{
|
||||||
|
item->type = cJSON_Raw;
|
||||||
|
item->valuestring = cJSON_strdup(raw);
|
||||||
|
if(!item->valuestring)
|
||||||
|
{
|
||||||
|
cJSON_Delete(item);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
cJSON *cJSON_CreateArray(void)
|
cJSON *cJSON_CreateArray(void)
|
||||||
{
|
{
|
||||||
cJSON *item = cJSON_New_Item();
|
cJSON *item = cJSON_New_Item();
|
||||||
|
|||||||
6
cJSON.h
6
cJSON.h
@@ -38,6 +38,7 @@ extern "C"
|
|||||||
#define cJSON_String (1 << 4)
|
#define cJSON_String (1 << 4)
|
||||||
#define cJSON_Array (1 << 5)
|
#define cJSON_Array (1 << 5)
|
||||||
#define cJSON_Object (1 << 6)
|
#define cJSON_Object (1 << 6)
|
||||||
|
#define cJSON_Raw (1 << 7) /* raw json */
|
||||||
|
|
||||||
#define cJSON_IsReference 256
|
#define cJSON_IsReference 256
|
||||||
#define cJSON_StringIsConst 512
|
#define cJSON_StringIsConst 512
|
||||||
@@ -54,7 +55,7 @@ typedef struct cJSON
|
|||||||
/* The type of the item, as above. */
|
/* The type of the item, as above. */
|
||||||
int type;
|
int type;
|
||||||
|
|
||||||
/* The item's string, if type==cJSON_String */
|
/* The item's string, if type==cJSON_String and type == cJSON_Raw */
|
||||||
char *valuestring;
|
char *valuestring;
|
||||||
/* The item's number, if type==cJSON_Number */
|
/* The item's number, if type==cJSON_Number */
|
||||||
int valueint;
|
int valueint;
|
||||||
@@ -105,6 +106,8 @@ extern cJSON *cJSON_CreateFalse(void);
|
|||||||
extern cJSON *cJSON_CreateBool(int b);
|
extern cJSON *cJSON_CreateBool(int b);
|
||||||
extern cJSON *cJSON_CreateNumber(double num);
|
extern cJSON *cJSON_CreateNumber(double num);
|
||||||
extern cJSON *cJSON_CreateString(const char *string);
|
extern cJSON *cJSON_CreateString(const char *string);
|
||||||
|
/* raw json */
|
||||||
|
extern cJSON *cJSON_CreateRaw(const char *raw);
|
||||||
extern cJSON *cJSON_CreateArray(void);
|
extern cJSON *cJSON_CreateArray(void);
|
||||||
extern cJSON *cJSON_CreateObject(void);
|
extern cJSON *cJSON_CreateObject(void);
|
||||||
|
|
||||||
@@ -155,6 +158,7 @@ extern void cJSON_Minify(char *json);
|
|||||||
#define cJSON_AddBoolToObject(object,name,b) cJSON_AddItemToObject(object, name, cJSON_CreateBool(b))
|
#define cJSON_AddBoolToObject(object,name,b) cJSON_AddItemToObject(object, name, cJSON_CreateBool(b))
|
||||||
#define cJSON_AddNumberToObject(object,name,n) cJSON_AddItemToObject(object, name, cJSON_CreateNumber(n))
|
#define cJSON_AddNumberToObject(object,name,n) cJSON_AddItemToObject(object, name, cJSON_CreateNumber(n))
|
||||||
#define cJSON_AddStringToObject(object,name,s) cJSON_AddItemToObject(object, name, cJSON_CreateString(s))
|
#define cJSON_AddStringToObject(object,name,s) cJSON_AddItemToObject(object, name, cJSON_CreateString(s))
|
||||||
|
#define cJSON_AddRawToObject(object,name,s) cJSON_AddItemToObject(object, name, cJSON_CreateRaw(s))
|
||||||
|
|
||||||
/* When assigning an integer value, it needs to be propagated to valuedouble too. */
|
/* When assigning an integer value, it needs to be propagated to valuedouble too. */
|
||||||
#define cJSON_SetIntValue(object,val) ((object) ? (object)->valueint = (object)->valuedouble = (val) : (val))
|
#define cJSON_SetIntValue(object,val) ((object) ? (object)->valueint = (object)->valuedouble = (val) : (val))
|
||||||
|
|||||||
4
test.c
4
test.c
@@ -106,7 +106,7 @@ static int print_preallocated(cJSON *root)
|
|||||||
/* create buffer to succeed */
|
/* create buffer to succeed */
|
||||||
/* the extra 64 bytes are in case a floating point value is printed */
|
/* the extra 64 bytes are in case a floating point value is printed */
|
||||||
len = strlen(out) + 64;
|
len = strlen(out) + 64;
|
||||||
buf = malloc(len);
|
buf = (char*)malloc(len);
|
||||||
if (buf == NULL)
|
if (buf == NULL)
|
||||||
{
|
{
|
||||||
printf("Failed to allocate memory.\n");
|
printf("Failed to allocate memory.\n");
|
||||||
@@ -115,7 +115,7 @@ static int print_preallocated(cJSON *root)
|
|||||||
|
|
||||||
/* create buffer to fail */
|
/* create buffer to fail */
|
||||||
len_fail = strlen(out);
|
len_fail = strlen(out);
|
||||||
buf_fail = malloc(len_fail);
|
buf_fail = (char*)malloc(len_fail);
|
||||||
if (buf_fail == NULL)
|
if (buf_fail == NULL)
|
||||||
{
|
{
|
||||||
printf("Failed to allocate memory.\n");
|
printf("Failed to allocate memory.\n");
|
||||||
|
|||||||
Reference in New Issue
Block a user