- Main
- Tutorials
- Library
- Samples

- File List
- Globals

Data Structures | Macros | Typedefs | Functions
cJSON.c File Reference
`#include <string.h>`
`#include <stdio.h>`
`#include <math.h>`
`#include <stdlib.h>`
`#include <limits.h>`
`#include <ctype.h>`
`#include "cJSON.h"`

Go to the source code of this file.

## Data Structures
struct | error
struct | internal_hooks
struct | parse_buffer
struct | printbuffer

## Macros
#define | true ((cJSON_bool)1)
#define | false ((cJSON_bool)0)
#define | internal_malloc malloc
#define | internal_free free
#define | internal_realloc realloc
#define | static_strlen(string_literal) (sizeof(string_literal) - sizeof(""))
#define | can_read(buffer, size) ((buffer != NULL) && (((buffer)->offset + size) <= (buffer)->length))
#define | can_access_at_index(buffer, index) ((buffer != NULL) && (((buffer)->offset + index) < (buffer)->length))
#define | cannot_access_at_index(buffer, index) (!can_access_at_index(buffer, index))
#define | buffer_at_offset(buffer) ((buffer)->content + (buffer)->offset)
#define | cjson_min(a, b) ((a < b) ? a : b)

## Typedefs
typedef struct internal_hooks | internal_hooks

## Functions
| CJSON_PUBLIC (const char *)
| CJSON_PUBLIC (char *)
| CJSON_PUBLIC (void)
| CJSON_PUBLIC (double)
| CJSON_PUBLIC (cJSON *)
| CJSON_PUBLIC (cJSON_bool)
| CJSON_PUBLIC (void *)

## Macro Definition Documentation

#define true ((cJSON_bool)1)

Definition at line 64 of file cJSON.c.

#define false ((cJSON_bool)0)

Definition at line 69 of file cJSON.c.

#define internal_malloc malloc

Definition at line 149 of file cJSON.c.

#define internal_free free

Definition at line 150 of file cJSON.c.

#define internal_realloc realloc

Definition at line 151 of file cJSON.c.

#define static_strlen | ( |  | string_literal | ) | (sizeof(string_literal) - sizeof(""))

Definition at line 155 of file cJSON.c.

#define can_read | ( |  | buffer,
|  |  | size
| ) |  | ((buffer != NULL) && (((buffer)->offset + size) <= (buffer)->length))

Definition at line 268 of file cJSON.c.

#define can_access_at_index | ( |  | buffer,
|  |  | index
| ) |  | ((buffer != NULL) && (((buffer)->offset + index) < (buffer)->length))

Definition at line 270 of file cJSON.c.

#define cannot_access_at_index | ( |  | buffer,
|  |  | index
| ) |  | (!can_access_at_index(buffer, index))

Definition at line 271 of file cJSON.c.

#define buffer_at_offset | ( |  | buffer | ) | ((buffer)->content + (buffer)->offset)

Definition at line 273 of file cJSON.c.

#define cjson_min | ( |  | a,
|  |  | b
| ) |  | ((a < b) ? a : b)

Definition at line 1095 of file cJSON.c.

## Typedef Documentation

typedef struct internal_hooks internal_hooks

## Function Documentation

CJSON_PUBLIC | ( | const char * |  | ) |

Definition at line 77 of file cJSON.c.

78 {
79 return (const char*) (global_error.json + global_error.position);
80 }
error::json
const unsigned char * json
Definition: cJSON.c:72
error::position
size_t position
Definition: cJSON.c:73

CJSON_PUBLIC | ( | char * |  | ) |

Definition at line 82 of file cJSON.c.

82 {
83 if (!cJSON_IsString(item)) {
84 return NULL;
85 }
86
87 return item->valuestring;
88 }
item
cJSON * item
Definition: cJSON.h:218
cJSON::valuestring
char * valuestring
Definition: cJSON.h:115

CJSON_PUBLIC | ( | void |  | ) |

Definition at line 180 of file cJSON.c.

181 {
182 if (hooks == NULL)
183 {
184 /* Reset hooks */
185 global_hooks.allocate = malloc;
186 global_hooks.deallocate = free;
187 global_hooks.reallocate = realloc;
188 return;
189 }
190
191 global_hooks.allocate = malloc;
192 if (hooks->malloc_fn != NULL)
193 {
194 global_hooks.allocate = hooks->malloc_fn;
195 }
196
197 global_hooks.deallocate = free;
198 if (hooks->free_fn != NULL)
199 {
200 global_hooks.deallocate = hooks->free_fn;
201 }
202
203 /* use realloc only if both free and malloc are used */
204 global_hooks.reallocate = NULL;
205 if ((global_hooks.allocate == malloc) && (global_hooks.deallocate == free))
206 {
207 global_hooks.reallocate = realloc;
208 }
209 }
internal_hooks::reallocate
void *CJSON_CDECL * reallocate(void *pointer, size_t size)
internal_hooks::allocate
void *CJSON_CDECL * allocate(size_t size)

CJSON_PUBLIC | ( | double |  | ) |

Definition at line 353 of file cJSON.c.

354 {
355 if (number >= INT_MAX)
356 {
357 object->valueint = INT_MAX;
358 }
359 else if (number <= (double)INT_MIN)
360 {
361 object->valueint = INT_MIN;
362 }
363 else
364 {
365 object->valueint = (int)number;
366 }
367
368 return object->valuedouble = number;
369 }
number
const char *const const double number
Definition: cJSON.h:262

CJSON_PUBLIC | ( | cJSON * |  | ) |

Definition at line 1010 of file cJSON.c.

1011 {
1012 parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
1013 cJSON *item = NULL;
1014
1015 /* reset error position */
1016 global_error.json = NULL;
1017 global_error.position = 0;
1018
1019 if (value == NULL)
1020 {
1021 goto fail;
1022 }
1023
1024 buffer.content = (const unsigned char*)value;
1025 buffer.length = strlen((const char*)value) + sizeof("");
1026 buffer.offset = 0;
1027 buffer.hooks = global_hooks;
1028
1029 item = cJSON_New_Item(&global_hooks);
1030 if (item == NULL) /* memory fail */
1031 {
1032 goto fail;
1033 }
1034
1035 if (!parse_value(item, buffer_skip_whitespace(skip_utf8_bom(&buffer))))
1036 {
1037 /* parse failure. ep is set. */
1038 goto fail;
1039 }
1040
1041 /* if we require null-terminated JSON without appended garbage, skip and then check for a null terminator */
1042 if (require_null_terminated)
1043 {
1044 buffer_skip_whitespace(&buffer);
1045 if ((buffer.offset >= buffer.length) || buffer_at_offset(&buffer)[0] != '\0')
1046 {
1047 goto fail;
1048 }
1049 }
1050 if (return_parse_end)
1051 {
1052 *return_parse_end = (const char*)buffer_at_offset(&buffer);
1053 }
1054
1055 return item;
1056
1057 fail:
1058 if (item != NULL)
1059 {
1060 cJSON_Delete(item);
1061 }
1062
1063 if (value != NULL)
1064 {
1065 error local_error;
1066 local_error.json = (const unsigned char*)value;
1067 local_error.position = 0;
1068
1069 if (buffer.offset < buffer.length)
1070 {
1071 local_error.position = buffer.offset;
1072 }
1073 else if (buffer.length > 0)
1074 {
1075 local_error.position = buffer.length - 1;
1076 }
1077
1078 if (return_parse_end != NULL)
1079 {
1080 *return_parse_end = (const char*)local_error.json + local_error.position;
1081 }
1082
1083 global_error = local_error;
1084 }
1085
1086 return NULL;
1087 }
parse_buffer::hooks
internal_hooks hooks
Definition: cJSON.c:264
parse_buffer::content
const unsigned char * content
Definition: cJSON.c:260
require_null_terminated
const char cJSON_bool require_null_terminated
Definition: cJSON.h:151
error
Definition: cJSON.c:71
parse_buffer::offset
size_t offset
Definition: cJSON.c:262
parse_buffer
Definition: cJSON.c:258
buffer
char * buffer
Definition: cJSON.h:161
item
cJSON * item
Definition: cJSON.h:218
error::json
const unsigned char * json
Definition: cJSON.c:72
return_parse_end
const char ** return_parse_end
Definition: cJSON.h:151
error::position
size_t position
Definition: cJSON.c:73
buffer_at_offset
#define buffer_at_offset(buffer)
Definition: cJSON.c:273
parse_buffer::length
size_t length
Definition: cJSON.c:261
cJSON
Definition: cJSON.h:103

CJSON_PUBLIC | ( | cJSON_bool |  | ) |

Definition at line 1202 of file cJSON.c.

1203 {
1204 printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
1205
1206 if ((len < 0) || (buf == NULL))
1207 {
1208 return false;
1209 }
1210
1211 p.buffer = (unsigned char*)buf;
1212 p.length = (size_t)len;
1213 p.offset = 0;
1214 p.noalloc = true;
1215 p.format = fmt;
1216 p.hooks = global_hooks;
1217
1218 return print_value(item, &p);
1219 }
printbuffer
Definition: cJSON.c:371
printbuffer::length
size_t length
Definition: cJSON.c:374
printbuffer::hooks
internal_hooks hooks
Definition: cJSON.c:379
fmt
int cJSON_bool fmt
Definition: cJSON.h:158
printbuffer::noalloc
cJSON_bool noalloc
Definition: cJSON.c:377
item
cJSON * item
Definition: cJSON.h:218
printbuffer::format
cJSON_bool format
Definition: cJSON.c:378
printbuffer::offset
size_t offset
Definition: cJSON.c:375
printbuffer::buffer
unsigned char * buffer
Definition: cJSON.c:373

CJSON_PUBLIC | ( | void * |  | ) |

Definition at line 2963 of file cJSON.c.

2964 {
2965 return global_hooks.allocate(size);
2966 }
internal_hooks::allocate
void *CJSON_CDECL * allocate(size_t size)