mirror of
https://github.com/DaveGamble/cJSON.git
synced 2023-08-10 21:13:26 +03:00
Compare commits
33 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5310da351d | ||
|
|
68196f5b14 | ||
|
|
d992cd46e6 | ||
|
|
84b48121bb | ||
|
|
f10e2f884a | ||
|
|
eeb6d237b6 | ||
|
|
5b4420298f | ||
|
|
e1eb06fae0 | ||
|
|
6aefa45056 | ||
|
|
345d57e9e5 | ||
|
|
3da18a2df9 | ||
|
|
bf17703012 | ||
|
|
6622c54f18 | ||
|
|
8df4cd46eb | ||
|
|
3917fb5255 | ||
|
|
99896d879c | ||
|
|
4d1dcaa160 | ||
|
|
de93d76d0b | ||
|
|
602c241a0b | ||
|
|
23dafa4739 | ||
|
|
59cf4112d2 | ||
|
|
5cfda2292f | ||
|
|
038b04d80a | ||
|
|
624bc85143 | ||
|
|
1f0ad823c9 | ||
|
|
29b6643bab | ||
|
|
a8e1368697 | ||
|
|
bea0be48b6 | ||
|
|
91d11cd050 | ||
|
|
9591ecddbe | ||
|
|
1568015de6 | ||
|
|
7ed067e680 | ||
|
|
4703f01cf4 |
@@ -6,16 +6,16 @@ include(GNUInstallDirs)
|
|||||||
project(cJSON C)
|
project(cJSON C)
|
||||||
|
|
||||||
set(PROJECT_VERSION_MAJOR 1)
|
set(PROJECT_VERSION_MAJOR 1)
|
||||||
set(PROJECT_VERSION_MINOR 0)
|
set(PROJECT_VERSION_MINOR 1)
|
||||||
set(PROJECT_VERSION_PATCH 2)
|
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)
|
||||||
set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}")
|
set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}")
|
||||||
|
|
||||||
option(ENABLE_CUSTOM_COMPILER_FLAGS 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")
|
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")
|
||||||
endif()
|
endif()
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ Contributors
|
|||||||
============
|
============
|
||||||
|
|
||||||
* [Ajay Bhargav](https://github.com/ajaybhargav)
|
* [Ajay Bhargav](https://github.com/ajaybhargav)
|
||||||
|
* [Alper Akcan](https://github.com/alperakcan)
|
||||||
* [Anton Sergeev](https://github.com/anton-sergeev)
|
* [Anton Sergeev](https://github.com/anton-sergeev)
|
||||||
* [Christian Schulze](https://github.com/ChristianSch)
|
* [Christian Schulze](https://github.com/ChristianSch)
|
||||||
* [Dave Gamble](https://github.com/DaveGamble)
|
* [Dave Gamble](https://github.com/DaveGamble)
|
||||||
@@ -15,6 +16,7 @@ Contributors
|
|||||||
* [IvanVoid](https://github.com/npi3pak)
|
* [IvanVoid](https://github.com/npi3pak)
|
||||||
* [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)
|
||||||
* [Linus Wallgren](https://github.com/ecksun)
|
* [Linus Wallgren](https://github.com/ecksun)
|
||||||
* [Max Bruckner](https://github.com/FSMaxB)
|
* [Max Bruckner](https://github.com/FSMaxB)
|
||||||
* Mike Pontillo
|
* Mike Pontillo
|
||||||
@@ -22,6 +24,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)
|
||||||
|
* [Stephan Gatzka](https://github.com/gatzka)
|
||||||
* [Weston Schmidt](https://github.com/schmidtw)
|
* [Weston Schmidt](https://github.com/schmidtw)
|
||||||
|
|
||||||
And probably more people on [SourceForge](https://sourceforge.net/p/cjson/bugs/search/?q=status%3Aclosed-rejected+or+status%3Aclosed-out-of-date+or+status%3Awont-fix+or+status%3Aclosed-fixed+or+status%3Aclosed&page=0)
|
And probably more people on [SourceForge](https://sourceforge.net/p/cjson/bugs/search/?q=status%3Aclosed-rejected+or+status%3Aclosed-out-of-date+or+status%3Awont-fix+or+status%3Aclosed-fixed+or+status%3Aclosed&page=0)
|
||||||
|
|||||||
17
Makefile
17
Makefile
@@ -5,9 +5,12 @@ UTILS_LIBNAME = libcjson_utils
|
|||||||
CJSON_TEST = cJSON_test
|
CJSON_TEST = cJSON_test
|
||||||
UTILS_TEST = cJSON_test_utils
|
UTILS_TEST = cJSON_test_utils
|
||||||
|
|
||||||
|
CJSON_TEST_SRC = cJSON.c test.c
|
||||||
|
UTILS_TEST_SRC = cJSON.c cJSON_Utils.c test_utils.c
|
||||||
|
|
||||||
LDLIBS = -lm
|
LDLIBS = -lm
|
||||||
|
|
||||||
LIBVERSION = 1.0.2
|
LIBVERSION = 1.1.0
|
||||||
CJSON_SOVERSION = 1
|
CJSON_SOVERSION = 1
|
||||||
UTILS_SOVERSION = 1
|
UTILS_SOVERSION = 1
|
||||||
|
|
||||||
@@ -20,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 $(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 $(CFLAGS)
|
||||||
|
|
||||||
uname := $(shell sh -c 'uname -s 2>/dev/null || echo false')
|
uname := $(shell sh -c 'uname -s 2>/dev/null || echo false')
|
||||||
|
|
||||||
@@ -62,15 +65,15 @@ test: tests
|
|||||||
./$(UTILS_TEST)
|
./$(UTILS_TEST)
|
||||||
|
|
||||||
.c.o:
|
.c.o:
|
||||||
$(CC) -ansi -pedantic -c $(R_CFLAGS) $<
|
$(CC) -c $(R_CFLAGS) $<
|
||||||
|
|
||||||
#tests
|
#tests
|
||||||
#cJSON
|
#cJSON
|
||||||
$(CJSON_TEST): cJSON.c cJSON.h test.c
|
$(CJSON_TEST): $(CJSON_TEST_SRC) cJSON.h
|
||||||
$(CC) $^ -o $@ $(LDLIBS) -I.
|
$(CC) $(R_CFLAGS) $(CJSON_TEST_SRC) -o $@ $(LDLIBS) -I.
|
||||||
#cJSON_Utils
|
#cJSON_Utils
|
||||||
$(UTILS_TEST): cJSON.c cJSON.h test.c
|
$(UTILS_TEST): $(UTILS_TEST_SRC) cJSON.h cJSON_Utils.h
|
||||||
$(CC) $^ -o $@ $(LDLIBS) -I.
|
$(CC) $(R_CFLAGS) $(UTILS_TEST_SRC) -o $@ $(LDLIBS) -I.
|
||||||
|
|
||||||
#static libraries
|
#static libraries
|
||||||
#cJSON
|
#cJSON
|
||||||
|
|||||||
35
cJSON.c
35
cJSON.c
@@ -244,6 +244,7 @@ typedef struct
|
|||||||
char *buffer;
|
char *buffer;
|
||||||
int length;
|
int length;
|
||||||
int offset;
|
int offset;
|
||||||
|
cjbool noalloc;
|
||||||
} printbuffer;
|
} printbuffer;
|
||||||
|
|
||||||
/* realloc printbuffer if necessary to have at least "needed" bytes more */
|
/* realloc printbuffer if necessary to have at least "needed" bytes more */
|
||||||
@@ -261,6 +262,10 @@ static char* ensure(printbuffer *p, int needed)
|
|||||||
return p->buffer + p->offset;
|
return p->buffer + p->offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (p->noalloc) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
newsize = pow2gt(needed);
|
newsize = pow2gt(needed);
|
||||||
newbuffer = (char*)cJSON_malloc(newsize);
|
newbuffer = (char*)cJSON_malloc(newsize);
|
||||||
if (!newbuffer)
|
if (!newbuffer)
|
||||||
@@ -712,8 +717,9 @@ static char *print_string_ptr(const char *str, printbuffer *p)
|
|||||||
|
|
||||||
ptr = str;
|
ptr = str;
|
||||||
/* calculate additional space that is needed for escaping */
|
/* calculate additional space that is needed for escaping */
|
||||||
while ((token = *ptr) && ++len)
|
while ((token = *ptr))
|
||||||
{
|
{
|
||||||
|
++len;
|
||||||
if (strchr("\"\\\b\f\n\r\t", token))
|
if (strchr("\"\\\b\f\n\r\t", token))
|
||||||
{
|
{
|
||||||
len++; /* +1 for the backslash */
|
len++; /* +1 for the backslash */
|
||||||
@@ -882,10 +888,20 @@ char *cJSON_PrintBuffered(const cJSON *item, int prebuffer, cjbool fmt)
|
|||||||
}
|
}
|
||||||
p.length = prebuffer;
|
p.length = prebuffer;
|
||||||
p.offset = 0;
|
p.offset = 0;
|
||||||
|
p.noalloc = false;
|
||||||
|
|
||||||
return print_value(item, 0, fmt, &p);
|
return print_value(item, 0, fmt, &p);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int cJSON_PrintPreallocated(cJSON *item,char *buf, const int len, const cjbool fmt)
|
||||||
|
{
|
||||||
|
printbuffer p;
|
||||||
|
p.buffer = buf;
|
||||||
|
p.length = len;
|
||||||
|
p.offset = 0;
|
||||||
|
p.noalloc = true;
|
||||||
|
return print_value(item,0,fmt,&p) != NULL;
|
||||||
|
}
|
||||||
|
|
||||||
/* Parser core - when encountering text, process appropriately. */
|
/* Parser core - when encountering text, process appropriately. */
|
||||||
static const char *parse_value(cJSON *item, const char *value, const char **ep)
|
static const char *parse_value(cJSON *item, const char *value, const char **ep)
|
||||||
@@ -1137,7 +1153,10 @@ static char *print_array(const cJSON *item, int depth, cjbool fmt, printbuffer *
|
|||||||
child = item->child;
|
child = item->child;
|
||||||
while (child && !fail)
|
while (child && !fail)
|
||||||
{
|
{
|
||||||
print_value(child, depth + 1, fmt, p);
|
if (!print_value(child, depth + 1, fmt, p))
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
p->offset = update(p);
|
p->offset = update(p);
|
||||||
if (child->next)
|
if (child->next)
|
||||||
{
|
{
|
||||||
@@ -1434,7 +1453,10 @@ static char *print_object(const cJSON *item, int depth, cjbool fmt, printbuffer
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* print key */
|
/* print key */
|
||||||
print_string_ptr(child->string, p);
|
if (!print_string_ptr(child->string, p))
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
p->offset = update(p);
|
p->offset = update(p);
|
||||||
|
|
||||||
len = fmt ? 2 : 1;
|
len = fmt ? 2 : 1;
|
||||||
@@ -1451,7 +1473,10 @@ static char *print_object(const cJSON *item, int depth, cjbool fmt, printbuffer
|
|||||||
p->offset+=len;
|
p->offset+=len;
|
||||||
|
|
||||||
/* print value */
|
/* print value */
|
||||||
print_value(child, depth, fmt, p);
|
if (!print_value(child, depth, fmt, p))
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
};
|
||||||
p->offset = update(p);
|
p->offset = update(p);
|
||||||
|
|
||||||
/* print comma if not last */
|
/* print comma if not last */
|
||||||
@@ -2128,7 +2153,7 @@ cJSON *cJSON_Duplicate(const cJSON *item, cjbool recurse)
|
|||||||
}
|
}
|
||||||
if (item->string)
|
if (item->string)
|
||||||
{
|
{
|
||||||
newitem->string = cJSON_strdup(item->string);
|
newitem->string = (item->type&cJSON_StringIsConst) ? item->string : cJSON_strdup(item->string);
|
||||||
if (!newitem->string)
|
if (!newitem->string)
|
||||||
{
|
{
|
||||||
cJSON_Delete(newitem);
|
cJSON_Delete(newitem);
|
||||||
|
|||||||
7
cJSON.h
7
cJSON.h
@@ -83,6 +83,8 @@ extern char *cJSON_Print(const cJSON *item);
|
|||||||
extern char *cJSON_PrintUnformatted(const cJSON *item);
|
extern char *cJSON_PrintUnformatted(const cJSON *item);
|
||||||
/* Render a cJSON entity to text using a buffered strategy. prebuffer is a guess at the final size. guessing well reduces reallocation. fmt=0 gives unformatted, =1 gives formatted */
|
/* Render a cJSON entity to text using a buffered strategy. prebuffer is a guess at the final size. guessing well reduces reallocation. fmt=0 gives unformatted, =1 gives formatted */
|
||||||
extern char *cJSON_PrintBuffered(const cJSON *item, int prebuffer, int fmt);
|
extern char *cJSON_PrintBuffered(const cJSON *item, int prebuffer, int fmt);
|
||||||
|
/* Render a cJSON entity to text using a buffer already allocated in memory with length buf_len. Returns 1 on success and 0 on failure. */
|
||||||
|
extern int cJSON_PrintPreallocated(cJSON *item, char *buf, const int len, const int fmt);
|
||||||
/* Delete a cJSON entity and all subentities. */
|
/* Delete a cJSON entity and all subentities. */
|
||||||
extern void cJSON_Delete(cJSON *c);
|
extern void cJSON_Delete(cJSON *c);
|
||||||
|
|
||||||
@@ -115,7 +117,10 @@ extern cJSON *cJSON_CreateStringArray(const char **strings, int count);
|
|||||||
/* Append item to the specified array/object. */
|
/* Append item to the specified array/object. */
|
||||||
extern void cJSON_AddItemToArray(cJSON *array, cJSON *item);
|
extern void cJSON_AddItemToArray(cJSON *array, cJSON *item);
|
||||||
extern void cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item);
|
extern void cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item);
|
||||||
extern void cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item); /* Use this when string is definitely const (i.e. a literal, or as good as), and will definitely survive the cJSON object */
|
/* Use this when string is definitely const (i.e. a literal, or as good as), and will definitely survive the cJSON object.
|
||||||
|
* WARNING: When this function was used, make sure to always check that (item->type & cJSON_StringIsConst) is zero before
|
||||||
|
* writing to `item->string` */
|
||||||
|
extern void cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item);
|
||||||
/* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */
|
/* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */
|
||||||
extern void cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item);
|
extern void cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item);
|
||||||
extern void cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item);
|
extern void cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item);
|
||||||
|
|||||||
130
test.c
130
test.c
@@ -22,10 +22,11 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
#include "cJSON.h"
|
#include "cJSON.h"
|
||||||
|
|
||||||
/* Parse text to JSON, then render back to text, and print! */
|
/* Parse text to JSON, then render back to text, and print! */
|
||||||
void doit(char *text)
|
static void doit(char *text)
|
||||||
{
|
{
|
||||||
char *out = NULL;
|
char *out = NULL;
|
||||||
cJSON *json = NULL;
|
cJSON *json = NULL;
|
||||||
@@ -44,8 +45,9 @@ void doit(char *text)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if 0
|
||||||
/* Read a file, parse, render back, etc. */
|
/* Read a file, parse, render back, etc. */
|
||||||
void dofile(char *filename)
|
static void dofile(char *filename)
|
||||||
{
|
{
|
||||||
FILE *f = NULL;
|
FILE *f = NULL;
|
||||||
long len = 0;
|
long len = 0;
|
||||||
@@ -59,6 +61,11 @@ void dofile(char *filename)
|
|||||||
fseek(f, 0, SEEK_SET);
|
fseek(f, 0, SEEK_SET);
|
||||||
|
|
||||||
data = (char*)malloc(len + 1);
|
data = (char*)malloc(len + 1);
|
||||||
|
if (data == NULL)
|
||||||
|
{
|
||||||
|
printf("Failed to allocate memory.\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
fread(data, 1, len, f);
|
fread(data, 1, len, f);
|
||||||
data[len] = '\0';
|
data[len] = '\0';
|
||||||
@@ -67,6 +74,7 @@ void dofile(char *filename)
|
|||||||
doit(data);
|
doit(data);
|
||||||
free(data);
|
free(data);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
/* Used by some code below as an example datatype. */
|
/* Used by some code below as an example datatype. */
|
||||||
struct record
|
struct record
|
||||||
@@ -81,8 +89,75 @@ struct record
|
|||||||
const char *country;
|
const char *country;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/* Create a bunch of objects as demonstration. */
|
/* Create a bunch of objects as demonstration. */
|
||||||
void create_objects(void)
|
static int print_preallocated(cJSON *root)
|
||||||
|
{
|
||||||
|
/* declarations */
|
||||||
|
char *out = NULL;
|
||||||
|
char *buf = NULL;
|
||||||
|
char *buf_fail = NULL;
|
||||||
|
int len = 0;
|
||||||
|
int len_fail = 0;
|
||||||
|
|
||||||
|
/* formatted print */
|
||||||
|
out = cJSON_Print(root);
|
||||||
|
|
||||||
|
/* create buffer to succeed */
|
||||||
|
/* the extra 64 bytes are in case a floating point value is printed */
|
||||||
|
len = strlen(out) + 64;
|
||||||
|
buf = malloc(len);
|
||||||
|
if (buf == NULL)
|
||||||
|
{
|
||||||
|
printf("Failed to allocate memory.\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* create buffer to fail */
|
||||||
|
len_fail = strlen(out);
|
||||||
|
buf_fail = malloc(len_fail);
|
||||||
|
if (buf_fail == NULL)
|
||||||
|
{
|
||||||
|
printf("Failed to allocate memory.\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Print to buffer */
|
||||||
|
if (!cJSON_PrintPreallocated(root, buf, len, 1)) {
|
||||||
|
printf("cJSON_PrintPreallocated failed!\n");
|
||||||
|
if (strcmp(out, buf) != 0) {
|
||||||
|
printf("cJSON_PrintPreallocated not the same as cJSON_Print!\n");
|
||||||
|
printf("cJSON_Print result:\n%s\n", out);
|
||||||
|
printf("cJSON_PrintPreallocated result:\n%s\n", buf);
|
||||||
|
}
|
||||||
|
free(out);
|
||||||
|
free(buf_fail);
|
||||||
|
free(buf);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* success */
|
||||||
|
printf("%s\n", buf);
|
||||||
|
|
||||||
|
/* force it to fail */
|
||||||
|
if (cJSON_PrintPreallocated(root, buf_fail, len_fail, 1)) {
|
||||||
|
printf("cJSON_PrintPreallocated failed to show error with insufficient memory!\n");
|
||||||
|
printf("cJSON_Print result:\n%s\n", out);
|
||||||
|
printf("cJSON_PrintPreallocated result:\n%s\n", buf_fail);
|
||||||
|
free(out);
|
||||||
|
free(buf_fail);
|
||||||
|
free(buf);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(out);
|
||||||
|
free(buf_fail);
|
||||||
|
free(buf);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Create a bunch of objects as demonstration. */
|
||||||
|
static void create_objects(void)
|
||||||
{
|
{
|
||||||
/* declare a few. */
|
/* declare a few. */
|
||||||
cJSON *root = NULL;
|
cJSON *root = NULL;
|
||||||
@@ -90,7 +165,6 @@ void create_objects(void)
|
|||||||
cJSON *img = NULL;
|
cJSON *img = NULL;
|
||||||
cJSON *thm = NULL;
|
cJSON *thm = NULL;
|
||||||
cJSON *fld = NULL;
|
cJSON *fld = NULL;
|
||||||
char *out = NULL;
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
||||||
/* Our "days of the week" array: */
|
/* Our "days of the week" array: */
|
||||||
@@ -152,21 +226,20 @@ void create_objects(void)
|
|||||||
cJSON_AddNumberToObject(fmt, "frame rate", 24);
|
cJSON_AddNumberToObject(fmt, "frame rate", 24);
|
||||||
|
|
||||||
/* Print to text */
|
/* Print to text */
|
||||||
out = cJSON_Print(root);
|
if (print_preallocated(root) != 0) {
|
||||||
/* Delete the cJSON */
|
cJSON_Delete(root);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
cJSON_Delete(root);
|
cJSON_Delete(root);
|
||||||
/* print it */
|
|
||||||
printf("%s\n",out);
|
|
||||||
/* release the string */
|
|
||||||
free(out);
|
|
||||||
|
|
||||||
/* Our "days of the week" array: */
|
/* Our "days of the week" array: */
|
||||||
root = cJSON_CreateStringArray(strings, 7);
|
root = cJSON_CreateStringArray(strings, 7);
|
||||||
|
|
||||||
out = cJSON_Print(root);
|
if (print_preallocated(root) != 0) {
|
||||||
|
cJSON_Delete(root);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
cJSON_Delete(root);
|
cJSON_Delete(root);
|
||||||
printf("%s\n", out);
|
|
||||||
free(out);
|
|
||||||
|
|
||||||
/* Our matrix: */
|
/* Our matrix: */
|
||||||
root = cJSON_CreateArray();
|
root = cJSON_CreateArray();
|
||||||
@@ -177,11 +250,11 @@ void create_objects(void)
|
|||||||
|
|
||||||
/* cJSON_ReplaceItemInArray(root, 1, cJSON_CreateString("Replacement")); */
|
/* cJSON_ReplaceItemInArray(root, 1, cJSON_CreateString("Replacement")); */
|
||||||
|
|
||||||
out = cJSON_Print(root);
|
if (print_preallocated(root) != 0) {
|
||||||
|
cJSON_Delete(root);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
cJSON_Delete(root);
|
cJSON_Delete(root);
|
||||||
printf("%s\n", out);
|
|
||||||
free(out);
|
|
||||||
|
|
||||||
|
|
||||||
/* Our "gallery" item: */
|
/* Our "gallery" item: */
|
||||||
root = cJSON_CreateObject();
|
root = cJSON_CreateObject();
|
||||||
@@ -195,13 +268,13 @@ void create_objects(void)
|
|||||||
cJSON_AddStringToObject(thm, "Width", "100");
|
cJSON_AddStringToObject(thm, "Width", "100");
|
||||||
cJSON_AddItemToObject(img, "IDs", cJSON_CreateIntArray(ids, 4));
|
cJSON_AddItemToObject(img, "IDs", cJSON_CreateIntArray(ids, 4));
|
||||||
|
|
||||||
out = cJSON_Print(root);
|
if (print_preallocated(root) != 0) {
|
||||||
|
cJSON_Delete(root);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
cJSON_Delete(root);
|
cJSON_Delete(root);
|
||||||
printf("%s\n", out);
|
|
||||||
free(out);
|
|
||||||
|
|
||||||
/* Our array of "records": */
|
/* Our array of "records": */
|
||||||
|
|
||||||
root = cJSON_CreateArray();
|
root = cJSON_CreateArray();
|
||||||
for (i = 0; i < 2; i++)
|
for (i = 0; i < 2; i++)
|
||||||
{
|
{
|
||||||
@@ -218,17 +291,20 @@ void create_objects(void)
|
|||||||
|
|
||||||
/* cJSON_ReplaceItemInObject(cJSON_GetArrayItem(root, 1), "City", cJSON_CreateIntArray(ids, 4)); */
|
/* cJSON_ReplaceItemInObject(cJSON_GetArrayItem(root, 1), "City", cJSON_CreateIntArray(ids, 4)); */
|
||||||
|
|
||||||
out = cJSON_Print(root);
|
if (print_preallocated(root) != 0) {
|
||||||
|
cJSON_Delete(root);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
cJSON_Delete(root);
|
cJSON_Delete(root);
|
||||||
printf("%s\n", out);
|
|
||||||
free(out);
|
|
||||||
|
|
||||||
root = cJSON_CreateObject();
|
root = cJSON_CreateObject();
|
||||||
cJSON_AddNumberToObject(root, "number", 1.0 / zero);
|
cJSON_AddNumberToObject(root, "number", 1.0 / zero);
|
||||||
out = cJSON_Print(root);
|
|
||||||
|
if (print_preallocated(root) != 0) {
|
||||||
|
cJSON_Delete(root);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
cJSON_Delete(root);
|
cJSON_Delete(root);
|
||||||
printf("%s\n", out);
|
|
||||||
free(out);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
|
|||||||
22
test_utils.c
22
test_utils.c
@@ -96,14 +96,14 @@ int main(void)
|
|||||||
printf("JSON Apply Patch Tests\n");
|
printf("JSON Apply Patch Tests\n");
|
||||||
for (i = 0; i < 15; i++)
|
for (i = 0; i < 15; i++)
|
||||||
{
|
{
|
||||||
cJSON *object = cJSON_Parse(patches[i][0]);
|
cJSON *object_to_be_patched = cJSON_Parse(patches[i][0]);
|
||||||
cJSON *patch = cJSON_Parse(patches[i][1]);
|
cJSON *patch = cJSON_Parse(patches[i][1]);
|
||||||
int err = cJSONUtils_ApplyPatches(object, patch);
|
int err = cJSONUtils_ApplyPatches(object_to_be_patched, patch);
|
||||||
char *output = cJSON_Print(object);
|
char *output = cJSON_Print(object_to_be_patched);
|
||||||
printf("Test %d (err %d):\n%s\n\n", i + 1, err, output);
|
printf("Test %d (err %d):\n%s\n\n", i + 1, err, output);
|
||||||
|
|
||||||
free(output);
|
free(output);
|
||||||
cJSON_Delete(object);
|
cJSON_Delete(object_to_be_patched);
|
||||||
cJSON_Delete(patch);
|
cJSON_Delete(patch);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -168,19 +168,19 @@ int main(void)
|
|||||||
printf("JSON Merge Patch tests\n");
|
printf("JSON Merge Patch tests\n");
|
||||||
for (i = 0; i < 15; i++)
|
for (i = 0; i < 15; i++)
|
||||||
{
|
{
|
||||||
cJSON *object = cJSON_Parse(merges[i][0]);
|
cJSON *object_to_be_merged = cJSON_Parse(merges[i][0]);
|
||||||
cJSON *patch = cJSON_Parse(merges[i][1]);
|
cJSON *patch = cJSON_Parse(merges[i][1]);
|
||||||
char *before = cJSON_PrintUnformatted(object);
|
char *before_merge = cJSON_PrintUnformatted(object_to_be_merged);
|
||||||
patchtext = cJSON_PrintUnformatted(patch);
|
patchtext = cJSON_PrintUnformatted(patch);
|
||||||
printf("Before: [%s] -> [%s] = ", before, patchtext);
|
printf("Before: [%s] -> [%s] = ", before_merge, patchtext);
|
||||||
object = cJSONUtils_MergePatch(object, patch);
|
object_to_be_merged = cJSONUtils_MergePatch(object_to_be_merged, patch);
|
||||||
after = cJSON_PrintUnformatted(object);
|
after = cJSON_PrintUnformatted(object_to_be_merged);
|
||||||
printf("[%s] vs [%s] (%s)\n", after, merges[i][2], strcmp(after, merges[i][2]) ? "FAIL" : "OK");
|
printf("[%s] vs [%s] (%s)\n", after, merges[i][2], strcmp(after, merges[i][2]) ? "FAIL" : "OK");
|
||||||
|
|
||||||
free(before);
|
free(before_merge);
|
||||||
free(patchtext);
|
free(patchtext);
|
||||||
free(after);
|
free(after);
|
||||||
cJSON_Delete(object);
|
cJSON_Delete(object_to_be_merged);
|
||||||
cJSON_Delete(patch);
|
cJSON_Delete(patch);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user