Compare commits
35 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 | ||
|
|
d0a9f46888 | ||
|
|
23b269d5f9 |
@@ -6,16 +6,16 @@ include(GNUInstallDirs)
|
||||
project(cJSON C)
|
||||
|
||||
set(PROJECT_VERSION_MAJOR 1)
|
||||
set(PROJECT_VERSION_MINOR 0)
|
||||
set(PROJECT_VERSION_PATCH 1)
|
||||
set(PROJECT_VERSION_MINOR 1)
|
||||
set(PROJECT_VERSION_PATCH 0)
|
||||
set(CJSON_VERSION_SO 1)
|
||||
set(CJSON_UTILS_VERSION_SO 1)
|
||||
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(("${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()
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ Contributors
|
||||
============
|
||||
|
||||
* [Ajay Bhargav](https://github.com/ajaybhargav)
|
||||
* [Alper Akcan](https://github.com/alperakcan)
|
||||
* [Anton Sergeev](https://github.com/anton-sergeev)
|
||||
* [Christian Schulze](https://github.com/ChristianSch)
|
||||
* [Dave Gamble](https://github.com/DaveGamble)
|
||||
@@ -15,6 +16,7 @@ Contributors
|
||||
* [IvanVoid](https://github.com/npi3pak)
|
||||
* [Jonathan Fether](https://github.com/jfether)
|
||||
* [Kevin Branigan](https://github.com/kbranigan)
|
||||
* [Kyle Chisholm](https://github.com/ChisholmKyle)
|
||||
* [Linus Wallgren](https://github.com/ecksun)
|
||||
* [Max Bruckner](https://github.com/FSMaxB)
|
||||
* Mike Pontillo
|
||||
@@ -22,6 +24,7 @@ Contributors
|
||||
* [Rafael Leal Dias](https://github.com/rafaeldias)
|
||||
* [Rod Vagg](https://github.com/rvagg)
|
||||
* [Roland Meertens](https://github.com/rmeertens)
|
||||
* [Stephan Gatzka](https://github.com/gatzka)
|
||||
* [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)
|
||||
|
||||
17
Makefile
17
Makefile
@@ -5,9 +5,12 @@ UTILS_LIBNAME = libcjson_utils
|
||||
CJSON_TEST = cJSON_test
|
||||
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
|
||||
|
||||
LIBVERSION = 1.0.1
|
||||
LIBVERSION = 1.1.0
|
||||
CJSON_SOVERSION = 1
|
||||
UTILS_SOVERSION = 1
|
||||
|
||||
@@ -20,7 +23,7 @@ INSTALL_LIBRARY_PATH = $(DESTDIR)$(PREFIX)/$(LIBRARY_PATH)
|
||||
|
||||
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')
|
||||
|
||||
@@ -62,15 +65,15 @@ test: tests
|
||||
./$(UTILS_TEST)
|
||||
|
||||
.c.o:
|
||||
$(CC) -ansi -pedantic -c $(R_CFLAGS) $<
|
||||
$(CC) -c $(R_CFLAGS) $<
|
||||
|
||||
#tests
|
||||
#cJSON
|
||||
$(CJSON_TEST): cJSON.c cJSON.h test.c
|
||||
$(CC) $^ -o $@ $(LDLIBS) -I.
|
||||
$(CJSON_TEST): $(CJSON_TEST_SRC) cJSON.h
|
||||
$(CC) $(R_CFLAGS) $(CJSON_TEST_SRC) -o $@ $(LDLIBS) -I.
|
||||
#cJSON_Utils
|
||||
$(UTILS_TEST): cJSON.c cJSON.h test.c
|
||||
$(CC) $^ -o $@ $(LDLIBS) -I.
|
||||
$(UTILS_TEST): $(UTILS_TEST_SRC) cJSON.h cJSON_Utils.h
|
||||
$(CC) $(R_CFLAGS) $(UTILS_TEST_SRC) -o $@ $(LDLIBS) -I.
|
||||
|
||||
#static libraries
|
||||
#cJSON
|
||||
|
||||
69
cJSON.c
69
cJSON.c
@@ -47,9 +47,9 @@
|
||||
#endif
|
||||
|
||||
/* define our own boolean type */
|
||||
typedef int bool;
|
||||
#define true ((bool)1)
|
||||
#define false ((bool)0)
|
||||
typedef int cjbool;
|
||||
#define true ((cjbool)1)
|
||||
#define false ((cjbool)0)
|
||||
|
||||
static const char *global_ep = NULL;
|
||||
|
||||
@@ -244,6 +244,7 @@ typedef struct
|
||||
char *buffer;
|
||||
int length;
|
||||
int offset;
|
||||
cjbool noalloc;
|
||||
} printbuffer;
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
||||
if (p->noalloc) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
newsize = pow2gt(needed);
|
||||
newbuffer = (char*)cJSON_malloc(newsize);
|
||||
if (!newbuffer)
|
||||
@@ -652,7 +657,7 @@ static char *print_string_ptr(const char *str, printbuffer *p)
|
||||
char *ptr2 = NULL;
|
||||
char *out = NULL;
|
||||
int len = 0;
|
||||
bool flag = false;
|
||||
cjbool flag = false;
|
||||
unsigned char token = '\0';
|
||||
|
||||
/* empty string */
|
||||
@@ -712,8 +717,9 @@ static char *print_string_ptr(const char *str, printbuffer *p)
|
||||
|
||||
ptr = str;
|
||||
/* calculate additional space that is needed for escaping */
|
||||
while ((token = *ptr) && ++len)
|
||||
while ((token = *ptr))
|
||||
{
|
||||
++len;
|
||||
if (strchr("\"\\\b\f\n\r\t", token))
|
||||
{
|
||||
len++; /* +1 for the backslash */
|
||||
@@ -798,11 +804,11 @@ static char *print_string(const cJSON *item, printbuffer *p)
|
||||
|
||||
/* Predeclare these prototypes. */
|
||||
static const char *parse_value(cJSON *item, const char *value, const char **ep);
|
||||
static char *print_value(const cJSON *item, int depth, bool fmt, printbuffer *p);
|
||||
static char *print_value(const cJSON *item, int depth, cjbool fmt, printbuffer *p);
|
||||
static const char *parse_array(cJSON *item, const char *value, const char **ep);
|
||||
static char *print_array(const cJSON *item, int depth, bool fmt, printbuffer *p);
|
||||
static char *print_array(const cJSON *item, int depth, cjbool fmt, printbuffer *p);
|
||||
static const char *parse_object(cJSON *item, const char *value, const char **ep);
|
||||
static char *print_object(const cJSON *item, int depth, bool fmt, printbuffer *p);
|
||||
static char *print_object(const cJSON *item, int depth, cjbool fmt, printbuffer *p);
|
||||
|
||||
/* Utility to jump whitespace and cr/lf */
|
||||
static const char *skip(const char *in)
|
||||
@@ -816,7 +822,7 @@ static const char *skip(const char *in)
|
||||
}
|
||||
|
||||
/* Parse an object - create a new root, and populate. */
|
||||
cJSON *cJSON_ParseWithOpts(const char *value, const char **return_parse_end, bool require_null_terminated)
|
||||
cJSON *cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cjbool require_null_terminated)
|
||||
{
|
||||
const char *end = NULL;
|
||||
/* use global error pointer if no specific one was given */
|
||||
@@ -872,7 +878,7 @@ char *cJSON_PrintUnformatted(const cJSON *item)
|
||||
return print_value(item, 0, 0, 0);
|
||||
}
|
||||
|
||||
char *cJSON_PrintBuffered(const cJSON *item, int prebuffer, bool fmt)
|
||||
char *cJSON_PrintBuffered(const cJSON *item, int prebuffer, cjbool fmt)
|
||||
{
|
||||
printbuffer p;
|
||||
p.buffer = (char*)cJSON_malloc(prebuffer);
|
||||
@@ -882,10 +888,20 @@ char *cJSON_PrintBuffered(const cJSON *item, int prebuffer, bool fmt)
|
||||
}
|
||||
p.length = prebuffer;
|
||||
p.offset = 0;
|
||||
p.noalloc = false;
|
||||
|
||||
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. */
|
||||
static const char *parse_value(cJSON *item, const char *value, const char **ep)
|
||||
@@ -936,7 +952,7 @@ static const char *parse_value(cJSON *item, const char *value, const char **ep)
|
||||
}
|
||||
|
||||
/* Render a value to text. */
|
||||
static char *print_value(const cJSON *item, int depth, bool fmt, printbuffer *p)
|
||||
static char *print_value(const cJSON *item, int depth, cjbool fmt, printbuffer *p)
|
||||
{
|
||||
char *out = NULL;
|
||||
|
||||
@@ -1082,7 +1098,7 @@ static const char *parse_array(cJSON *item,const char *value,const char **ep)
|
||||
}
|
||||
|
||||
/* Render an array to text */
|
||||
static char *print_array(const cJSON *item, int depth, bool fmt, printbuffer *p)
|
||||
static char *print_array(const cJSON *item, int depth, cjbool fmt, printbuffer *p)
|
||||
{
|
||||
char **entries;
|
||||
char *out = NULL;
|
||||
@@ -1092,7 +1108,7 @@ static char *print_array(const cJSON *item, int depth, bool fmt, printbuffer *p)
|
||||
cJSON *child = item->child;
|
||||
int numentries = 0;
|
||||
int i = 0;
|
||||
bool fail = false;
|
||||
cjbool fail = false;
|
||||
size_t tmplen = 0;
|
||||
|
||||
/* How many entries in the array? */
|
||||
@@ -1137,7 +1153,10 @@ static char *print_array(const cJSON *item, int depth, bool fmt, printbuffer *p)
|
||||
child = item->child;
|
||||
while (child && !fail)
|
||||
{
|
||||
print_value(child, depth + 1, fmt, p);
|
||||
if (!print_value(child, depth + 1, fmt, p))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
p->offset = update(p);
|
||||
if (child->next)
|
||||
{
|
||||
@@ -1343,7 +1362,7 @@ static const char *parse_object(cJSON *item, const char *value, const char **ep)
|
||||
}
|
||||
|
||||
/* Render an object to text. */
|
||||
static char *print_object(const cJSON *item, int depth, bool fmt, printbuffer *p)
|
||||
static char *print_object(const cJSON *item, int depth, cjbool fmt, printbuffer *p)
|
||||
{
|
||||
char **entries = NULL;
|
||||
char **names = NULL;
|
||||
@@ -1356,7 +1375,7 @@ static char *print_object(const cJSON *item, int depth, bool fmt, printbuffer *p
|
||||
int j = 0;
|
||||
cJSON *child = item->child;
|
||||
int numentries = 0;
|
||||
bool fail = false;
|
||||
cjbool fail = false;
|
||||
size_t tmplen = 0;
|
||||
|
||||
/* Count the number of entries. */
|
||||
@@ -1434,7 +1453,10 @@ static char *print_object(const cJSON *item, int depth, bool fmt, printbuffer *p
|
||||
}
|
||||
|
||||
/* print key */
|
||||
print_string_ptr(child->string, p);
|
||||
if (!print_string_ptr(child->string, p))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
p->offset = update(p);
|
||||
|
||||
len = fmt ? 2 : 1;
|
||||
@@ -1451,7 +1473,10 @@ static char *print_object(const cJSON *item, int depth, bool fmt, printbuffer *p
|
||||
p->offset+=len;
|
||||
|
||||
/* print value */
|
||||
print_value(child, depth, fmt, p);
|
||||
if (!print_value(child, depth, fmt, p))
|
||||
{
|
||||
return NULL;
|
||||
};
|
||||
p->offset = update(p);
|
||||
|
||||
/* print comma if not last */
|
||||
@@ -1652,7 +1677,7 @@ cJSON *cJSON_GetObjectItem(const cJSON *object, const char *string)
|
||||
return c;
|
||||
}
|
||||
|
||||
bool cJSON_HasObjectItem(const cJSON *object,const char *string)
|
||||
cjbool cJSON_HasObjectItem(const cJSON *object,const char *string)
|
||||
{
|
||||
return cJSON_GetObjectItem(object, string) ? 1 : 0;
|
||||
}
|
||||
@@ -1918,7 +1943,7 @@ cJSON *cJSON_CreateFalse(void)
|
||||
return item;
|
||||
}
|
||||
|
||||
cJSON *cJSON_CreateBool(bool b)
|
||||
cJSON *cJSON_CreateBool(cjbool b)
|
||||
{
|
||||
cJSON *item = cJSON_New_Item();
|
||||
if(item)
|
||||
@@ -2095,7 +2120,7 @@ cJSON *cJSON_CreateStringArray(const char **strings, int count)
|
||||
}
|
||||
|
||||
/* Duplication */
|
||||
cJSON *cJSON_Duplicate(const cJSON *item, bool recurse)
|
||||
cJSON *cJSON_Duplicate(const cJSON *item, cjbool recurse)
|
||||
{
|
||||
cJSON *newitem = NULL;
|
||||
cJSON *cptr = NULL;
|
||||
@@ -2128,7 +2153,7 @@ cJSON *cJSON_Duplicate(const cJSON *item, bool recurse)
|
||||
}
|
||||
if (item->string)
|
||||
{
|
||||
newitem->string = cJSON_strdup(item->string);
|
||||
newitem->string = (item->type&cJSON_StringIsConst) ? item->string : cJSON_strdup(item->string);
|
||||
if (!newitem->string)
|
||||
{
|
||||
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);
|
||||
/* 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);
|
||||
/* 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. */
|
||||
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. */
|
||||
extern void cJSON_AddItemToArray(cJSON *array, 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. */
|
||||
extern void cJSON_AddItemReferenceToArray(cJSON *array, 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 <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "cJSON.h"
|
||||
|
||||
/* Parse text to JSON, then render back to text, and print! */
|
||||
void doit(char *text)
|
||||
static void doit(char *text)
|
||||
{
|
||||
char *out = NULL;
|
||||
cJSON *json = NULL;
|
||||
@@ -44,8 +45,9 @@ void doit(char *text)
|
||||
}
|
||||
}
|
||||
|
||||
#if 0
|
||||
/* Read a file, parse, render back, etc. */
|
||||
void dofile(char *filename)
|
||||
static void dofile(char *filename)
|
||||
{
|
||||
FILE *f = NULL;
|
||||
long len = 0;
|
||||
@@ -59,6 +61,11 @@ void dofile(char *filename)
|
||||
fseek(f, 0, SEEK_SET);
|
||||
|
||||
data = (char*)malloc(len + 1);
|
||||
if (data == NULL)
|
||||
{
|
||||
printf("Failed to allocate memory.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
fread(data, 1, len, f);
|
||||
data[len] = '\0';
|
||||
@@ -67,6 +74,7 @@ void dofile(char *filename)
|
||||
doit(data);
|
||||
free(data);
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Used by some code below as an example datatype. */
|
||||
struct record
|
||||
@@ -81,8 +89,75 @@ struct record
|
||||
const char *country;
|
||||
};
|
||||
|
||||
|
||||
/* 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. */
|
||||
cJSON *root = NULL;
|
||||
@@ -90,7 +165,6 @@ void create_objects(void)
|
||||
cJSON *img = NULL;
|
||||
cJSON *thm = NULL;
|
||||
cJSON *fld = NULL;
|
||||
char *out = NULL;
|
||||
int i = 0;
|
||||
|
||||
/* Our "days of the week" array: */
|
||||
@@ -152,21 +226,20 @@ void create_objects(void)
|
||||
cJSON_AddNumberToObject(fmt, "frame rate", 24);
|
||||
|
||||
/* Print to text */
|
||||
out = cJSON_Print(root);
|
||||
/* Delete the cJSON */
|
||||
if (print_preallocated(root) != 0) {
|
||||
cJSON_Delete(root);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
cJSON_Delete(root);
|
||||
/* print it */
|
||||
printf("%s\n",out);
|
||||
/* release the string */
|
||||
free(out);
|
||||
|
||||
/* Our "days of the week" array: */
|
||||
root = cJSON_CreateStringArray(strings, 7);
|
||||
|
||||
out = cJSON_Print(root);
|
||||
if (print_preallocated(root) != 0) {
|
||||
cJSON_Delete(root);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
cJSON_Delete(root);
|
||||
printf("%s\n", out);
|
||||
free(out);
|
||||
|
||||
/* Our matrix: */
|
||||
root = cJSON_CreateArray();
|
||||
@@ -177,11 +250,11 @@ void create_objects(void)
|
||||
|
||||
/* 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);
|
||||
printf("%s\n", out);
|
||||
free(out);
|
||||
|
||||
|
||||
/* Our "gallery" item: */
|
||||
root = cJSON_CreateObject();
|
||||
@@ -195,13 +268,13 @@ void create_objects(void)
|
||||
cJSON_AddStringToObject(thm, "Width", "100");
|
||||
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);
|
||||
printf("%s\n", out);
|
||||
free(out);
|
||||
|
||||
/* Our array of "records": */
|
||||
|
||||
root = cJSON_CreateArray();
|
||||
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)); */
|
||||
|
||||
out = cJSON_Print(root);
|
||||
if (print_preallocated(root) != 0) {
|
||||
cJSON_Delete(root);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
cJSON_Delete(root);
|
||||
printf("%s\n", out);
|
||||
free(out);
|
||||
|
||||
root = cJSON_CreateObject();
|
||||
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);
|
||||
printf("%s\n", out);
|
||||
free(out);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
|
||||
22
test_utils.c
22
test_utils.c
@@ -96,14 +96,14 @@ int main(void)
|
||||
printf("JSON Apply Patch Tests\n");
|
||||
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]);
|
||||
int err = cJSONUtils_ApplyPatches(object, patch);
|
||||
char *output = cJSON_Print(object);
|
||||
int err = cJSONUtils_ApplyPatches(object_to_be_patched, patch);
|
||||
char *output = cJSON_Print(object_to_be_patched);
|
||||
printf("Test %d (err %d):\n%s\n\n", i + 1, err, output);
|
||||
|
||||
free(output);
|
||||
cJSON_Delete(object);
|
||||
cJSON_Delete(object_to_be_patched);
|
||||
cJSON_Delete(patch);
|
||||
}
|
||||
|
||||
@@ -168,19 +168,19 @@ int main(void)
|
||||
printf("JSON Merge Patch tests\n");
|
||||
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]);
|
||||
char *before = cJSON_PrintUnformatted(object);
|
||||
char *before_merge = cJSON_PrintUnformatted(object_to_be_merged);
|
||||
patchtext = cJSON_PrintUnformatted(patch);
|
||||
printf("Before: [%s] -> [%s] = ", before, patchtext);
|
||||
object = cJSONUtils_MergePatch(object, patch);
|
||||
after = cJSON_PrintUnformatted(object);
|
||||
printf("Before: [%s] -> [%s] = ", before_merge, patchtext);
|
||||
object_to_be_merged = cJSONUtils_MergePatch(object_to_be_merged, patch);
|
||||
after = cJSON_PrintUnformatted(object_to_be_merged);
|
||||
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(after);
|
||||
cJSON_Delete(object);
|
||||
cJSON_Delete(object_to_be_merged);
|
||||
cJSON_Delete(patch);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user