2012-10-16 19:56:54 +02:00
|
|
|
/*
|
2012-11-03 19:52:49 +01:00
|
|
|
Parson ( http://kgabis.github.com/parson/ )
|
2014-04-10 17:00:40 +02:00
|
|
|
Copyright (c) 2012 - 2014 Krzysztof Gabis
|
2013-12-06 14:31:18 -05:00
|
|
|
|
2012-10-16 19:56:54 +02:00
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
2013-12-06 14:31:18 -05:00
|
|
|
|
2012-10-16 19:56:54 +02:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2013-12-06 14:31:18 -05:00
|
|
|
|
2012-10-16 19:56:54 +02:00
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
*/
|
2012-11-03 08:19:28 -04:00
|
|
|
|
|
|
|
#include "parson.h"
|
|
|
|
|
2012-10-16 19:56:54 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
2012-11-07 22:51:03 +01:00
|
|
|
#define ERROR 0
|
|
|
|
#define SUCCESS 1
|
|
|
|
#define STARTING_CAPACITY 15
|
|
|
|
#define ARRAY_MAX_CAPACITY 122880 /* 15*(2^13) */
|
|
|
|
#define OBJECT_MAX_CAPACITY 960 /* 15*(2^6) */
|
|
|
|
#define MAX_NESTING 19
|
|
|
|
#define sizeof_token(a) (sizeof(a) - 1)
|
|
|
|
#define skip_char(str) ((*str)++)
|
2013-07-02 18:01:20 +02:00
|
|
|
#define skip_whitespaces(str) while (isspace(**str)) { skip_char(str); }
|
2012-12-11 11:47:18 +01:00
|
|
|
#define MAX(a, b) ((a) > (b) ? (a) : (b))
|
2012-10-16 19:56:54 +02:00
|
|
|
|
2012-11-03 08:19:28 -04:00
|
|
|
#define parson_malloc(a) malloc(a)
|
|
|
|
#define parson_free(a) free((void*)a)
|
2012-10-28 17:58:24 +01:00
|
|
|
#define parson_realloc(a, b) realloc(a, b)
|
2012-10-22 19:20:02 +02:00
|
|
|
|
2012-10-16 19:56:54 +02:00
|
|
|
/* Type definitions */
|
2012-11-03 08:19:28 -04:00
|
|
|
typedef union json_value_value {
|
|
|
|
const char *string;
|
|
|
|
double number;
|
2012-10-16 19:56:54 +02:00
|
|
|
JSON_Object *object;
|
2012-11-03 08:19:28 -04:00
|
|
|
JSON_Array *array;
|
|
|
|
int boolean;
|
|
|
|
int null;
|
2012-11-03 19:52:49 +01:00
|
|
|
} JSON_Value_Value;
|
2012-10-16 19:56:54 +02:00
|
|
|
|
2012-11-03 19:52:49 +01:00
|
|
|
struct json_value_t {
|
|
|
|
JSON_Value_Type type;
|
|
|
|
JSON_Value_Value value;
|
|
|
|
};
|
2012-10-16 19:56:54 +02:00
|
|
|
|
2012-11-03 19:52:49 +01:00
|
|
|
struct json_object_t {
|
2012-10-16 19:56:54 +02:00
|
|
|
const char **names;
|
|
|
|
JSON_Value **values;
|
2012-11-03 08:19:28 -04:00
|
|
|
size_t count;
|
|
|
|
size_t capacity;
|
2012-11-03 19:52:49 +01:00
|
|
|
};
|
2012-10-16 19:56:54 +02:00
|
|
|
|
2012-11-03 19:52:49 +01:00
|
|
|
struct json_array_t {
|
2012-10-16 19:56:54 +02:00
|
|
|
JSON_Value **items;
|
2012-11-03 08:19:28 -04:00
|
|
|
size_t count;
|
|
|
|
size_t capacity;
|
2012-11-03 19:52:49 +01:00
|
|
|
};
|
2012-10-16 19:56:54 +02:00
|
|
|
|
2012-11-07 22:51:03 +01:00
|
|
|
/* Various */
|
2013-11-30 20:22:16 +01:00
|
|
|
static char * read_file(const char *filename);
|
|
|
|
static void remove_comments(char *string, const char *start_token, const char *end_token);
|
2012-11-07 22:51:03 +01:00
|
|
|
static int try_realloc(void **ptr, size_t new_size);
|
|
|
|
static char * parson_strndup(const char *string, size_t n);
|
2012-12-02 11:15:48 +01:00
|
|
|
static int is_utf(const unsigned char *string);
|
2012-11-07 22:51:03 +01:00
|
|
|
static int is_decimal(const char *string, size_t length);
|
|
|
|
|
2012-10-16 19:56:54 +02:00
|
|
|
/* JSON Object */
|
2012-10-19 16:24:24 +02:00
|
|
|
static JSON_Object * json_object_init(void);
|
2012-11-03 08:19:28 -04:00
|
|
|
static int json_object_add(JSON_Object *object, const char *name, JSON_Value *value);
|
2012-11-07 22:51:03 +01:00
|
|
|
static int json_object_resize(JSON_Object *object, size_t capacity);
|
Code refractoring.
Details:
- Increased STARTING_CAPACITY from 10 to 15.
- Added 2 macros: skip_char and skip_whitespaces.
- Added json_object_nget_value function, which removes neccessity to allocate new string when using dotget functions.
- Removed parson_strdup function, it was called only once and could be easilly replaced with appropriate call to parson_strndup.
- Renamed skip_string to skip_quotes, which is a more appropriate name, and made it work on a passed pointer to a string, which is much like skip_char and skip_whitespaces.
- Removed copy_and_remove_whitespaces, it was unncessary, and could be easily replaced with skip_whitepsaces macro.
- Merged parse_escaped_characters and get_string to get_processed_string, which makes more sense.
- Changed is_decimal implementation, to avoid unncessary string duplicating.
- Removed string copying in parse_number value and json_parse_string, since it was unncessary.
2012-11-04 17:45:52 +01:00
|
|
|
static JSON_Value * json_object_nget_value(const JSON_Object *object, const char *name, size_t n);
|
2012-11-03 08:19:28 -04:00
|
|
|
static void json_object_free(JSON_Object *object);
|
2012-10-16 19:56:54 +02:00
|
|
|
|
|
|
|
/* JSON Array */
|
2012-10-19 16:24:24 +02:00
|
|
|
static JSON_Array * json_array_init(void);
|
2012-11-03 08:19:28 -04:00
|
|
|
static int json_array_add(JSON_Array *array, JSON_Value *value);
|
2012-11-07 22:51:03 +01:00
|
|
|
static int json_array_resize(JSON_Array *array, size_t capacity);
|
2012-11-03 08:19:28 -04:00
|
|
|
static void json_array_free(JSON_Array *array);
|
2012-10-16 19:56:54 +02:00
|
|
|
|
|
|
|
/* JSON Value */
|
2012-10-19 16:24:24 +02:00
|
|
|
static JSON_Value * json_value_init_object(void);
|
|
|
|
static JSON_Value * json_value_init_array(void);
|
2012-10-16 19:56:54 +02:00
|
|
|
static JSON_Value * json_value_init_string(const char *string);
|
|
|
|
static JSON_Value * json_value_init_number(double number);
|
2012-10-18 15:25:11 +02:00
|
|
|
static JSON_Value * json_value_init_boolean(int boolean);
|
2012-10-19 16:24:24 +02:00
|
|
|
static JSON_Value * json_value_init_null(void);
|
2012-10-16 19:56:54 +02:00
|
|
|
|
|
|
|
/* Parser */
|
2012-11-07 22:51:03 +01:00
|
|
|
static void skip_quotes(const char **string);
|
2014-04-10 17:00:40 +02:00
|
|
|
static int parse_utf_16(char **processed, char **unprocessed);
|
Code refractoring.
Details:
- Increased STARTING_CAPACITY from 10 to 15.
- Added 2 macros: skip_char and skip_whitespaces.
- Added json_object_nget_value function, which removes neccessity to allocate new string when using dotget functions.
- Removed parson_strdup function, it was called only once and could be easilly replaced with appropriate call to parson_strndup.
- Renamed skip_string to skip_quotes, which is a more appropriate name, and made it work on a passed pointer to a string, which is much like skip_char and skip_whitespaces.
- Removed copy_and_remove_whitespaces, it was unncessary, and could be easily replaced with skip_whitepsaces macro.
- Merged parse_escaped_characters and get_string to get_processed_string, which makes more sense.
- Changed is_decimal implementation, to avoid unncessary string duplicating.
- Removed string copying in parse_number value and json_parse_string, since it was unncessary.
2012-11-04 17:45:52 +01:00
|
|
|
static const char * get_processed_string(const char **string);
|
2012-10-24 10:41:05 +02:00
|
|
|
static JSON_Value * parse_object_value(const char **string, size_t nesting);
|
|
|
|
static JSON_Value * parse_array_value(const char **string, size_t nesting);
|
2012-10-16 19:56:54 +02:00
|
|
|
static JSON_Value * parse_string_value(const char **string);
|
2012-10-18 15:25:11 +02:00
|
|
|
static JSON_Value * parse_boolean_value(const char **string);
|
2012-10-16 19:56:54 +02:00
|
|
|
static JSON_Value * parse_number_value(const char **string);
|
|
|
|
static JSON_Value * parse_null_value(const char **string);
|
2012-10-24 10:41:05 +02:00
|
|
|
static JSON_Value * parse_value(const char **string, size_t nesting);
|
2012-10-16 19:56:54 +02:00
|
|
|
|
2012-11-07 22:51:03 +01:00
|
|
|
/* Various */
|
|
|
|
static int try_realloc(void **ptr, size_t new_size) {
|
|
|
|
void *reallocated_ptr = parson_realloc(*ptr, new_size);
|
2013-11-30 20:28:55 +01:00
|
|
|
if (!reallocated_ptr)
|
|
|
|
return ERROR;
|
2012-11-07 22:51:03 +01:00
|
|
|
*ptr = reallocated_ptr;
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char * parson_strndup(const char *string, size_t n) {
|
|
|
|
char *output_string = (char*)parson_malloc(n + 1);
|
2013-11-30 20:28:55 +01:00
|
|
|
if (!output_string)
|
|
|
|
return NULL;
|
2012-11-07 22:51:03 +01:00
|
|
|
output_string[n] = '\0';
|
|
|
|
strncpy(output_string, string, n);
|
|
|
|
return output_string;
|
|
|
|
}
|
|
|
|
|
2012-12-02 11:15:48 +01:00
|
|
|
static int is_utf(const unsigned char *s) {
|
2012-11-07 22:51:03 +01:00
|
|
|
return isxdigit(s[0]) && isxdigit(s[1]) && isxdigit(s[2]) && isxdigit(s[3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int is_decimal(const char *string, size_t length) {
|
2013-11-30 20:28:55 +01:00
|
|
|
if (length > 1 && string[0] == '0' && string[1] != '.')
|
|
|
|
return 0;
|
|
|
|
if (length > 2 && !strncmp(string, "-0", 2) && string[2] != '.')
|
|
|
|
return 0;
|
|
|
|
while (length--)
|
|
|
|
if (strchr("xX", string[length]))
|
|
|
|
return 0;
|
2012-11-07 22:51:03 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-11-30 20:22:16 +01:00
|
|
|
static char * read_file(const char * filename) {
|
|
|
|
FILE *fp = fopen(filename, "r");
|
|
|
|
size_t file_size;
|
|
|
|
char *file_contents;
|
2013-11-30 20:28:55 +01:00
|
|
|
if (!fp)
|
|
|
|
return NULL;
|
2013-11-30 20:22:16 +01:00
|
|
|
fseek(fp, 0L, SEEK_END);
|
|
|
|
file_size = ftell(fp);
|
|
|
|
rewind(fp);
|
|
|
|
file_contents = (char*)parson_malloc(sizeof(char) * (file_size + 1));
|
2013-11-30 20:28:55 +01:00
|
|
|
if (!file_contents) {
|
|
|
|
fclose(fp);
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-11-30 20:22:16 +01:00
|
|
|
if (fread(file_contents, file_size, 1, fp) < 1) {
|
2013-11-30 20:28:55 +01:00
|
|
|
if (ferror(fp)) {
|
|
|
|
fclose(fp);
|
2013-12-06 14:31:18 -05:00
|
|
|
parson_free(file_contents);
|
2013-11-30 20:28:55 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2013-11-30 20:22:16 +01:00
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
file_contents[file_size] = '\0';
|
|
|
|
return file_contents;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void remove_comments(char *string, const char *start_token, const char *end_token) {
|
2013-11-30 20:35:55 +01:00
|
|
|
int in_string = 0, escaped = 0;
|
|
|
|
size_t i;
|
2013-11-30 20:22:16 +01:00
|
|
|
char *ptr = NULL, current_char;
|
|
|
|
size_t start_token_len = strlen(start_token);
|
|
|
|
size_t end_token_len = strlen(end_token);
|
|
|
|
if (start_token_len == 0 || end_token_len == 0)
|
|
|
|
return;
|
|
|
|
while ((current_char = *string) != '\0') {
|
|
|
|
if (current_char == '\\' && !escaped) {
|
|
|
|
escaped = 1;
|
|
|
|
string++;
|
|
|
|
continue;
|
|
|
|
} else if (current_char == '\"' && !escaped) {
|
|
|
|
in_string = !in_string;
|
|
|
|
} else if (!in_string && strncmp(string, start_token, start_token_len) == 0) {
|
2013-11-30 20:28:55 +01:00
|
|
|
for(i = 0; i < start_token_len; i++)
|
|
|
|
string[i] = ' ';
|
2013-11-30 20:22:16 +01:00
|
|
|
string = string + start_token_len;
|
|
|
|
ptr = strstr(string, end_token);
|
2013-11-30 20:28:55 +01:00
|
|
|
if (!ptr)
|
|
|
|
return;
|
|
|
|
for (i = 0; i < (ptr - string) + end_token_len; i++)
|
|
|
|
string[i] = ' ';
|
2013-11-30 20:22:16 +01:00
|
|
|
string = ptr + end_token_len - 1;
|
|
|
|
}
|
|
|
|
escaped = 0;
|
|
|
|
string++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-16 19:56:54 +02:00
|
|
|
/* JSON Object */
|
2012-10-19 16:24:24 +02:00
|
|
|
static JSON_Object * json_object_init(void) {
|
2012-10-22 19:20:02 +02:00
|
|
|
JSON_Object *new_obj = (JSON_Object*)parson_malloc(sizeof(JSON_Object));
|
2013-11-30 20:28:55 +01:00
|
|
|
if (!new_obj)
|
|
|
|
return NULL;
|
2012-12-11 11:47:18 +01:00
|
|
|
new_obj->names = (const char**)NULL;
|
|
|
|
new_obj->values = (JSON_Value**)NULL;
|
|
|
|
new_obj->capacity = 0;
|
2012-10-19 16:24:24 +02:00
|
|
|
new_obj->count = 0;
|
|
|
|
return new_obj;
|
2012-10-16 19:56:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int json_object_add(JSON_Object *object, const char *name, JSON_Value *value) {
|
|
|
|
size_t index;
|
|
|
|
if (object->count >= object->capacity) {
|
2012-12-11 11:47:18 +01:00
|
|
|
size_t new_capacity = MAX(object->capacity * 2, STARTING_CAPACITY);
|
2013-11-30 20:28:55 +01:00
|
|
|
if (new_capacity > OBJECT_MAX_CAPACITY)
|
|
|
|
return ERROR;
|
|
|
|
if (json_object_resize(object, new_capacity) == ERROR)
|
|
|
|
return ERROR;
|
2012-11-03 08:19:28 -04:00
|
|
|
}
|
2013-11-30 20:28:55 +01:00
|
|
|
if (json_object_get_value(object, name) != NULL)
|
|
|
|
return ERROR;
|
2012-10-16 19:56:54 +02:00
|
|
|
index = object->count;
|
Code refractoring.
Details:
- Increased STARTING_CAPACITY from 10 to 15.
- Added 2 macros: skip_char and skip_whitespaces.
- Added json_object_nget_value function, which removes neccessity to allocate new string when using dotget functions.
- Removed parson_strdup function, it was called only once and could be easilly replaced with appropriate call to parson_strndup.
- Renamed skip_string to skip_quotes, which is a more appropriate name, and made it work on a passed pointer to a string, which is much like skip_char and skip_whitespaces.
- Removed copy_and_remove_whitespaces, it was unncessary, and could be easily replaced with skip_whitepsaces macro.
- Merged parse_escaped_characters and get_string to get_processed_string, which makes more sense.
- Changed is_decimal implementation, to avoid unncessary string duplicating.
- Removed string copying in parse_number value and json_parse_string, since it was unncessary.
2012-11-04 17:45:52 +01:00
|
|
|
object->names[index] = parson_strndup(name, strlen(name));
|
2013-11-30 20:28:55 +01:00
|
|
|
if (!object->names[index])
|
|
|
|
return ERROR;
|
2012-10-16 19:56:54 +02:00
|
|
|
object->values[index] = value;
|
|
|
|
object->count++;
|
2012-11-07 22:51:03 +01:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int json_object_resize(JSON_Object *object, size_t capacity) {
|
2013-11-30 20:28:55 +01:00
|
|
|
if (try_realloc((void**)&object->names, capacity * sizeof(char*)) == ERROR)
|
|
|
|
return ERROR;
|
|
|
|
if (try_realloc((void**)&object->values, capacity * sizeof(JSON_Value*)) == ERROR)
|
|
|
|
return ERROR;
|
2012-11-07 22:51:03 +01:00
|
|
|
object->capacity = capacity;
|
|
|
|
return SUCCESS;
|
2012-10-16 19:56:54 +02:00
|
|
|
}
|
|
|
|
|
Code refractoring.
Details:
- Increased STARTING_CAPACITY from 10 to 15.
- Added 2 macros: skip_char and skip_whitespaces.
- Added json_object_nget_value function, which removes neccessity to allocate new string when using dotget functions.
- Removed parson_strdup function, it was called only once and could be easilly replaced with appropriate call to parson_strndup.
- Renamed skip_string to skip_quotes, which is a more appropriate name, and made it work on a passed pointer to a string, which is much like skip_char and skip_whitespaces.
- Removed copy_and_remove_whitespaces, it was unncessary, and could be easily replaced with skip_whitepsaces macro.
- Merged parse_escaped_characters and get_string to get_processed_string, which makes more sense.
- Changed is_decimal implementation, to avoid unncessary string duplicating.
- Removed string copying in parse_number value and json_parse_string, since it was unncessary.
2012-11-04 17:45:52 +01:00
|
|
|
static JSON_Value * json_object_nget_value(const JSON_Object *object, const char *name, size_t n) {
|
2012-11-07 22:51:03 +01:00
|
|
|
size_t i, name_length;
|
|
|
|
for (i = 0; i < json_object_get_count(object); i++) {
|
|
|
|
name_length = strlen(object->names[i]);
|
2013-11-30 20:28:55 +01:00
|
|
|
if (name_length != n)
|
|
|
|
continue;
|
|
|
|
if (strncmp(object->names[i], name, n) == 0)
|
|
|
|
return object->values[i];
|
Code refractoring.
Details:
- Increased STARTING_CAPACITY from 10 to 15.
- Added 2 macros: skip_char and skip_whitespaces.
- Added json_object_nget_value function, which removes neccessity to allocate new string when using dotget functions.
- Removed parson_strdup function, it was called only once and could be easilly replaced with appropriate call to parson_strndup.
- Renamed skip_string to skip_quotes, which is a more appropriate name, and made it work on a passed pointer to a string, which is much like skip_char and skip_whitespaces.
- Removed copy_and_remove_whitespaces, it was unncessary, and could be easily replaced with skip_whitepsaces macro.
- Merged parse_escaped_characters and get_string to get_processed_string, which makes more sense.
- Changed is_decimal implementation, to avoid unncessary string duplicating.
- Removed string copying in parse_number value and json_parse_string, since it was unncessary.
2012-11-04 17:45:52 +01:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-10-16 19:56:54 +02:00
|
|
|
static void json_object_free(JSON_Object *object) {
|
2012-11-07 22:51:03 +01:00
|
|
|
while(object->count--) {
|
|
|
|
parson_free(object->names[object->count]);
|
|
|
|
json_value_free(object->values[object->count]);
|
2012-10-16 19:56:54 +02:00
|
|
|
}
|
2012-10-28 20:18:34 +01:00
|
|
|
parson_free(object->names);
|
2012-10-22 19:20:02 +02:00
|
|
|
parson_free(object->values);
|
|
|
|
parson_free(object);
|
2012-10-16 19:56:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* JSON Array */
|
2012-10-19 16:24:24 +02:00
|
|
|
static JSON_Array * json_array_init(void) {
|
2012-10-22 19:20:02 +02:00
|
|
|
JSON_Array *new_array = (JSON_Array*)parson_malloc(sizeof(JSON_Array));
|
2013-11-30 20:28:55 +01:00
|
|
|
if (!new_array)
|
|
|
|
return NULL;
|
2012-12-11 11:47:18 +01:00
|
|
|
new_array->items = (JSON_Value**)NULL;
|
|
|
|
new_array->capacity = 0;
|
2012-10-16 19:56:54 +02:00
|
|
|
new_array->count = 0;
|
|
|
|
return new_array;
|
|
|
|
}
|
|
|
|
|
2012-10-19 16:24:24 +02:00
|
|
|
static int json_array_add(JSON_Array *array, JSON_Value *value) {
|
2012-10-16 19:56:54 +02:00
|
|
|
if (array->count >= array->capacity) {
|
2012-12-11 11:47:18 +01:00
|
|
|
size_t new_capacity = MAX(array->capacity * 2, STARTING_CAPACITY);
|
2013-11-30 20:28:55 +01:00
|
|
|
if (new_capacity > ARRAY_MAX_CAPACITY)
|
|
|
|
return ERROR;
|
|
|
|
if (!json_array_resize(array, new_capacity))
|
|
|
|
return ERROR;
|
2012-10-16 19:56:54 +02:00
|
|
|
}
|
|
|
|
array->items[array->count] = value;
|
|
|
|
array->count++;
|
2012-11-07 22:51:03 +01:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int json_array_resize(JSON_Array *array, size_t capacity) {
|
2013-11-30 20:28:55 +01:00
|
|
|
if (try_realloc((void**)&array->items, capacity * sizeof(JSON_Value*)) == ERROR)
|
|
|
|
return ERROR;
|
2012-11-07 22:51:03 +01:00
|
|
|
array->capacity = capacity;
|
|
|
|
return SUCCESS;
|
2012-10-16 19:56:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void json_array_free(JSON_Array *array) {
|
2013-11-30 20:28:55 +01:00
|
|
|
while (array->count--)
|
|
|
|
json_value_free(array->items[array->count]);
|
2012-10-22 19:20:02 +02:00
|
|
|
parson_free(array->items);
|
|
|
|
parson_free(array);
|
2012-10-16 19:56:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* JSON Value */
|
2012-10-19 16:24:24 +02:00
|
|
|
static JSON_Value * json_value_init_object(void) {
|
2012-10-22 19:20:02 +02:00
|
|
|
JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
|
2013-11-30 20:28:55 +01:00
|
|
|
if (!new_value)
|
|
|
|
return NULL;
|
2012-10-16 19:56:54 +02:00
|
|
|
new_value->type = JSONObject;
|
|
|
|
new_value->value.object = json_object_init();
|
2013-11-30 20:28:55 +01:00
|
|
|
if (!new_value->value.object) {
|
|
|
|
parson_free(new_value);
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-10-16 19:56:54 +02:00
|
|
|
return new_value;
|
|
|
|
}
|
|
|
|
|
2012-10-19 16:24:24 +02:00
|
|
|
static JSON_Value * json_value_init_array(void) {
|
2012-10-22 19:20:02 +02:00
|
|
|
JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
|
2013-11-30 20:28:55 +01:00
|
|
|
if (!new_value)
|
|
|
|
return NULL;
|
2012-10-16 19:56:54 +02:00
|
|
|
new_value->type = JSONArray;
|
|
|
|
new_value->value.array = json_array_init();
|
2013-11-30 20:28:55 +01:00
|
|
|
if (!new_value->value.array) {
|
|
|
|
parson_free(new_value);
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-10-16 19:56:54 +02:00
|
|
|
return new_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSON_Value * json_value_init_string(const char *string) {
|
2012-10-22 19:20:02 +02:00
|
|
|
JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
|
2013-11-30 20:28:55 +01:00
|
|
|
if (!new_value)
|
|
|
|
return NULL;
|
2012-10-16 19:56:54 +02:00
|
|
|
new_value->type = JSONString;
|
|
|
|
new_value->value.string = string;
|
|
|
|
return new_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSON_Value * json_value_init_number(double number) {
|
2012-10-22 19:20:02 +02:00
|
|
|
JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
|
2013-11-30 20:28:55 +01:00
|
|
|
if (!new_value)
|
|
|
|
return NULL;
|
2012-10-16 19:56:54 +02:00
|
|
|
new_value->type = JSONNumber;
|
|
|
|
new_value->value.number = number;
|
|
|
|
return new_value;
|
|
|
|
}
|
|
|
|
|
2012-10-18 15:25:11 +02:00
|
|
|
static JSON_Value * json_value_init_boolean(int boolean) {
|
2012-10-22 19:20:02 +02:00
|
|
|
JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
|
2013-11-30 20:28:55 +01:00
|
|
|
if (!new_value)
|
|
|
|
return NULL;
|
2012-10-18 15:25:11 +02:00
|
|
|
new_value->type = JSONBoolean;
|
|
|
|
new_value->value.boolean = boolean;
|
2012-10-16 19:56:54 +02:00
|
|
|
return new_value;
|
|
|
|
}
|
|
|
|
|
2012-10-19 16:24:24 +02:00
|
|
|
static JSON_Value * json_value_init_null(void) {
|
2012-10-22 19:20:02 +02:00
|
|
|
JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
|
2013-11-30 20:28:55 +01:00
|
|
|
if (!new_value)
|
|
|
|
return NULL;
|
2012-10-16 19:56:54 +02:00
|
|
|
new_value->type = JSONNull;
|
|
|
|
return new_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parser */
|
Code refractoring.
Details:
- Increased STARTING_CAPACITY from 10 to 15.
- Added 2 macros: skip_char and skip_whitespaces.
- Added json_object_nget_value function, which removes neccessity to allocate new string when using dotget functions.
- Removed parson_strdup function, it was called only once and could be easilly replaced with appropriate call to parson_strndup.
- Renamed skip_string to skip_quotes, which is a more appropriate name, and made it work on a passed pointer to a string, which is much like skip_char and skip_whitespaces.
- Removed copy_and_remove_whitespaces, it was unncessary, and could be easily replaced with skip_whitepsaces macro.
- Merged parse_escaped_characters and get_string to get_processed_string, which makes more sense.
- Changed is_decimal implementation, to avoid unncessary string duplicating.
- Removed string copying in parse_number value and json_parse_string, since it was unncessary.
2012-11-04 17:45:52 +01:00
|
|
|
static void skip_quotes(const char **string) {
|
|
|
|
skip_char(string);
|
|
|
|
while (**string != '\"') {
|
2013-11-30 20:28:55 +01:00
|
|
|
if (**string == '\0')
|
|
|
|
return;
|
|
|
|
if (**string == '\\') {
|
|
|
|
skip_char(string);
|
|
|
|
if (**string == '\0')
|
|
|
|
return;
|
|
|
|
}
|
2012-11-07 22:51:03 +01:00
|
|
|
skip_char(string);
|
2012-10-16 19:56:54 +02:00
|
|
|
}
|
Code refractoring.
Details:
- Increased STARTING_CAPACITY from 10 to 15.
- Added 2 macros: skip_char and skip_whitespaces.
- Added json_object_nget_value function, which removes neccessity to allocate new string when using dotget functions.
- Removed parson_strdup function, it was called only once and could be easilly replaced with appropriate call to parson_strndup.
- Renamed skip_string to skip_quotes, which is a more appropriate name, and made it work on a passed pointer to a string, which is much like skip_char and skip_whitespaces.
- Removed copy_and_remove_whitespaces, it was unncessary, and could be easily replaced with skip_whitepsaces macro.
- Merged parse_escaped_characters and get_string to get_processed_string, which makes more sense.
- Changed is_decimal implementation, to avoid unncessary string duplicating.
- Removed string copying in parse_number value and json_parse_string, since it was unncessary.
2012-11-04 17:45:52 +01:00
|
|
|
skip_char(string);
|
2012-10-16 19:56:54 +02:00
|
|
|
}
|
|
|
|
|
2014-04-10 17:00:40 +02:00
|
|
|
static int parse_utf_16(char **processed, char **unprocessed) {
|
|
|
|
unsigned int cp, lead, trail;
|
|
|
|
char *processed_ptr = *processed;
|
|
|
|
char *unprocessed_ptr = *unprocessed;
|
|
|
|
unprocessed_ptr++; /* skips u */
|
|
|
|
if (!is_utf((const unsigned char*)unprocessed_ptr) || sscanf(unprocessed_ptr, "%4x", &cp) == EOF)
|
|
|
|
return ERROR;
|
|
|
|
if (cp < 0x80) {
|
|
|
|
*processed_ptr = cp; /* 0xxxxxxx */
|
|
|
|
} else if (cp < 0x800) {
|
|
|
|
*processed_ptr++ = ((cp >> 6) & 0x1F) | 0xC0; /* 110xxxxx */
|
|
|
|
*processed_ptr = ((cp ) & 0x3F) | 0x80; /* 10xxxxxx */
|
|
|
|
} else if (cp < 0xD800 || cp > 0xDFFF) {
|
|
|
|
*processed_ptr++ = ((cp >> 12) & 0x0F) | 0xE0; /* 1110xxxx */
|
|
|
|
*processed_ptr++ = ((cp >> 6) & 0x3F) | 0x80; /* 10xxxxxx */
|
|
|
|
*processed_ptr = ((cp ) & 0x3F) | 0x80; /* 10xxxxxx */
|
|
|
|
} else if (cp >= 0xD800 && cp <= 0xDBFF) { /* lead surrogate (0xD800..0xDBFF) */
|
|
|
|
lead = cp;
|
|
|
|
unprocessed_ptr += 4; /* should always be within the buffer, otherwise previous sscanf would fail */
|
|
|
|
if (*unprocessed_ptr++ != '\\' || *unprocessed_ptr++ != 'u' || /* starts with \u? */
|
|
|
|
!is_utf((const unsigned char*)unprocessed_ptr) ||
|
|
|
|
sscanf(unprocessed_ptr, "%4x", &trail) == EOF ||
|
|
|
|
trail < 0xDC00 || trail > 0xDFFF) { /* valid trail surrogate? (0xDC00..0xDFFF) */
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
cp = ((((lead-0xD800)&0x3FF)<<10)|((trail-0xDC00)&0x3FF))+0x010000;
|
|
|
|
*processed_ptr++ = (((cp >> 18) & 0x07) | 0xF0); /* 11110xxx */
|
|
|
|
*processed_ptr++ = (((cp >> 12) & 0x3F) | 0x80); /* 10xxxxxx */
|
|
|
|
*processed_ptr++ = (((cp >> 6) & 0x3F) | 0x80); /* 10xxxxxx */
|
|
|
|
*processed_ptr = (((cp ) & 0x3F) | 0x80); /* 10xxxxxx */
|
|
|
|
} else { /* trail surrogate before lead surrogate */
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
unprocessed_ptr += 3;
|
|
|
|
*processed = processed_ptr;
|
|
|
|
*unprocessed = unprocessed_ptr;
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
Code refractoring.
Details:
- Increased STARTING_CAPACITY from 10 to 15.
- Added 2 macros: skip_char and skip_whitespaces.
- Added json_object_nget_value function, which removes neccessity to allocate new string when using dotget functions.
- Removed parson_strdup function, it was called only once and could be easilly replaced with appropriate call to parson_strndup.
- Renamed skip_string to skip_quotes, which is a more appropriate name, and made it work on a passed pointer to a string, which is much like skip_char and skip_whitespaces.
- Removed copy_and_remove_whitespaces, it was unncessary, and could be easily replaced with skip_whitepsaces macro.
- Merged parse_escaped_characters and get_string to get_processed_string, which makes more sense.
- Changed is_decimal implementation, to avoid unncessary string duplicating.
- Removed string copying in parse_number value and json_parse_string, since it was unncessary.
2012-11-04 17:45:52 +01:00
|
|
|
/* Returns contents of a string inside double quotes and parses escaped
|
|
|
|
characters inside.
|
|
|
|
Example: "\u006Corem ipsum" -> lorem ipsum */
|
|
|
|
static const char * get_processed_string(const char **string) {
|
|
|
|
const char *string_start = *string;
|
2014-04-10 17:00:40 +02:00
|
|
|
char *output = NULL, *processed_ptr = NULL, *unprocessed_ptr = NULL;
|
Code refractoring.
Details:
- Increased STARTING_CAPACITY from 10 to 15.
- Added 2 macros: skip_char and skip_whitespaces.
- Added json_object_nget_value function, which removes neccessity to allocate new string when using dotget functions.
- Removed parson_strdup function, it was called only once and could be easilly replaced with appropriate call to parson_strndup.
- Renamed skip_string to skip_quotes, which is a more appropriate name, and made it work on a passed pointer to a string, which is much like skip_char and skip_whitespaces.
- Removed copy_and_remove_whitespaces, it was unncessary, and could be easily replaced with skip_whitepsaces macro.
- Merged parse_escaped_characters and get_string to get_processed_string, which makes more sense.
- Changed is_decimal implementation, to avoid unncessary string duplicating.
- Removed string copying in parse_number value and json_parse_string, since it was unncessary.
2012-11-04 17:45:52 +01:00
|
|
|
skip_quotes(string);
|
2013-11-30 20:28:55 +01:00
|
|
|
if (**string == '\0')
|
|
|
|
return NULL;
|
2014-04-10 17:00:40 +02:00
|
|
|
output = parson_strndup(string_start + 1, *string - string_start - 2);
|
2013-11-30 20:28:55 +01:00
|
|
|
if (!output)
|
|
|
|
return NULL;
|
2012-11-07 22:51:03 +01:00
|
|
|
processed_ptr = unprocessed_ptr = output;
|
2014-04-10 17:00:40 +02:00
|
|
|
while (*unprocessed_ptr != '\0') {
|
|
|
|
if (*unprocessed_ptr == '\\') {
|
Code refractoring.
Details:
- Increased STARTING_CAPACITY from 10 to 15.
- Added 2 macros: skip_char and skip_whitespaces.
- Added json_object_nget_value function, which removes neccessity to allocate new string when using dotget functions.
- Removed parson_strdup function, it was called only once and could be easilly replaced with appropriate call to parson_strndup.
- Renamed skip_string to skip_quotes, which is a more appropriate name, and made it work on a passed pointer to a string, which is much like skip_char and skip_whitespaces.
- Removed copy_and_remove_whitespaces, it was unncessary, and could be easily replaced with skip_whitepsaces macro.
- Merged parse_escaped_characters and get_string to get_processed_string, which makes more sense.
- Changed is_decimal implementation, to avoid unncessary string duplicating.
- Removed string copying in parse_number value and json_parse_string, since it was unncessary.
2012-11-04 17:45:52 +01:00
|
|
|
unprocessed_ptr++;
|
2014-04-10 17:00:40 +02:00
|
|
|
switch (*unprocessed_ptr) {
|
2014-06-10 20:59:35 +02:00
|
|
|
case '\"': *processed_ptr = '\"'; break;
|
|
|
|
case '\\': *processed_ptr = '\\'; break;
|
|
|
|
case '/': *processed_ptr = '/'; break;
|
|
|
|
case 'b': *processed_ptr = '\b'; break;
|
|
|
|
case 'f': *processed_ptr = '\f'; break;
|
|
|
|
case 'n': *processed_ptr = '\n'; break;
|
|
|
|
case 'r': *processed_ptr = '\r'; break;
|
|
|
|
case 't': *processed_ptr = '\t'; break;
|
2012-10-16 19:56:54 +02:00
|
|
|
case 'u':
|
2014-04-10 17:00:40 +02:00
|
|
|
if (parse_utf_16(&processed_ptr, &unprocessed_ptr) == ERROR) {
|
|
|
|
parson_free(output);
|
|
|
|
return NULL;
|
2012-10-16 19:56:54 +02:00
|
|
|
}
|
2012-11-03 08:19:28 -04:00
|
|
|
break;
|
2012-10-16 19:56:54 +02:00
|
|
|
default:
|
Code refractoring.
Details:
- Increased STARTING_CAPACITY from 10 to 15.
- Added 2 macros: skip_char and skip_whitespaces.
- Added json_object_nget_value function, which removes neccessity to allocate new string when using dotget functions.
- Removed parson_strdup function, it was called only once and could be easilly replaced with appropriate call to parson_strndup.
- Renamed skip_string to skip_quotes, which is a more appropriate name, and made it work on a passed pointer to a string, which is much like skip_char and skip_whitespaces.
- Removed copy_and_remove_whitespaces, it was unncessary, and could be easily replaced with skip_whitepsaces macro.
- Merged parse_escaped_characters and get_string to get_processed_string, which makes more sense.
- Changed is_decimal implementation, to avoid unncessary string duplicating.
- Removed string copying in parse_number value and json_parse_string, since it was unncessary.
2012-11-04 17:45:52 +01:00
|
|
|
parson_free(output);
|
2012-10-16 19:56:54 +02:00
|
|
|
return NULL;
|
|
|
|
break;
|
|
|
|
}
|
2014-04-10 17:00:40 +02:00
|
|
|
} else if ((unsigned char)*unprocessed_ptr < 0x20) {
|
|
|
|
parson_free(output); /* 0x00-0x19 are invalid characters for json string (http://www.ietf.org/rfc/rfc4627.txt) */
|
2012-10-16 19:56:54 +02:00
|
|
|
return NULL;
|
2014-04-10 17:00:40 +02:00
|
|
|
} else {
|
|
|
|
*processed_ptr = *unprocessed_ptr;
|
2012-11-03 08:19:28 -04:00
|
|
|
}
|
2014-06-10 20:59:35 +02:00
|
|
|
processed_ptr++;
|
|
|
|
unprocessed_ptr++;
|
2012-10-16 19:56:54 +02:00
|
|
|
}
|
Code refractoring.
Details:
- Increased STARTING_CAPACITY from 10 to 15.
- Added 2 macros: skip_char and skip_whitespaces.
- Added json_object_nget_value function, which removes neccessity to allocate new string when using dotget functions.
- Removed parson_strdup function, it was called only once and could be easilly replaced with appropriate call to parson_strndup.
- Renamed skip_string to skip_quotes, which is a more appropriate name, and made it work on a passed pointer to a string, which is much like skip_char and skip_whitespaces.
- Removed copy_and_remove_whitespaces, it was unncessary, and could be easily replaced with skip_whitepsaces macro.
- Merged parse_escaped_characters and get_string to get_processed_string, which makes more sense.
- Changed is_decimal implementation, to avoid unncessary string duplicating.
- Removed string copying in parse_number value and json_parse_string, since it was unncessary.
2012-11-04 17:45:52 +01:00
|
|
|
*processed_ptr = '\0';
|
2013-11-30 20:28:55 +01:00
|
|
|
if (try_realloc((void**)&output, strlen(output) + 1) == ERROR)
|
|
|
|
return NULL;
|
Code refractoring.
Details:
- Increased STARTING_CAPACITY from 10 to 15.
- Added 2 macros: skip_char and skip_whitespaces.
- Added json_object_nget_value function, which removes neccessity to allocate new string when using dotget functions.
- Removed parson_strdup function, it was called only once and could be easilly replaced with appropriate call to parson_strndup.
- Renamed skip_string to skip_quotes, which is a more appropriate name, and made it work on a passed pointer to a string, which is much like skip_char and skip_whitespaces.
- Removed copy_and_remove_whitespaces, it was unncessary, and could be easily replaced with skip_whitepsaces macro.
- Merged parse_escaped_characters and get_string to get_processed_string, which makes more sense.
- Changed is_decimal implementation, to avoid unncessary string duplicating.
- Removed string copying in parse_number value and json_parse_string, since it was unncessary.
2012-11-04 17:45:52 +01:00
|
|
|
return output;
|
2012-10-16 19:56:54 +02:00
|
|
|
}
|
|
|
|
|
2012-10-24 10:41:05 +02:00
|
|
|
static JSON_Value * parse_value(const char **string, size_t nesting) {
|
2013-11-30 20:28:55 +01:00
|
|
|
if (nesting > MAX_NESTING)
|
|
|
|
return NULL;
|
Code refractoring.
Details:
- Increased STARTING_CAPACITY from 10 to 15.
- Added 2 macros: skip_char and skip_whitespaces.
- Added json_object_nget_value function, which removes neccessity to allocate new string when using dotget functions.
- Removed parson_strdup function, it was called only once and could be easilly replaced with appropriate call to parson_strndup.
- Renamed skip_string to skip_quotes, which is a more appropriate name, and made it work on a passed pointer to a string, which is much like skip_char and skip_whitespaces.
- Removed copy_and_remove_whitespaces, it was unncessary, and could be easily replaced with skip_whitepsaces macro.
- Merged parse_escaped_characters and get_string to get_processed_string, which makes more sense.
- Changed is_decimal implementation, to avoid unncessary string duplicating.
- Removed string copying in parse_number value and json_parse_string, since it was unncessary.
2012-11-04 17:45:52 +01:00
|
|
|
skip_whitespaces(string);
|
|
|
|
switch (**string) {
|
2012-10-16 19:56:54 +02:00
|
|
|
case '{':
|
2012-11-07 22:51:03 +01:00
|
|
|
return parse_object_value(string, nesting + 1);
|
2012-10-16 19:56:54 +02:00
|
|
|
case '[':
|
2012-11-07 22:51:03 +01:00
|
|
|
return parse_array_value(string, nesting + 1);
|
2012-10-16 19:56:54 +02:00
|
|
|
case '\"':
|
2012-11-07 22:51:03 +01:00
|
|
|
return parse_string_value(string);
|
2012-10-24 10:41:05 +02:00
|
|
|
case 'f': case 't':
|
2012-11-07 22:51:03 +01:00
|
|
|
return parse_boolean_value(string);
|
2012-10-16 19:56:54 +02:00
|
|
|
case '-':
|
|
|
|
case '0': case '1': case '2': case '3': case '4':
|
|
|
|
case '5': case '6': case '7': case '8': case '9':
|
2012-11-07 22:51:03 +01:00
|
|
|
return parse_number_value(string);
|
2012-10-16 19:56:54 +02:00
|
|
|
case 'n':
|
2012-11-07 22:51:03 +01:00
|
|
|
return parse_null_value(string);
|
2012-10-16 19:56:54 +02:00
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-24 10:41:05 +02:00
|
|
|
static JSON_Value * parse_object_value(const char **string, size_t nesting) {
|
2012-11-07 22:51:03 +01:00
|
|
|
JSON_Value *output_value = json_value_init_object(), *new_value = NULL;
|
|
|
|
JSON_Object *output_object = json_value_get_object(output_value);
|
2012-10-16 19:56:54 +02:00
|
|
|
const char *new_key = NULL;
|
2013-11-30 20:28:55 +01:00
|
|
|
if (!output_value)
|
|
|
|
return NULL;
|
Code refractoring.
Details:
- Increased STARTING_CAPACITY from 10 to 15.
- Added 2 macros: skip_char and skip_whitespaces.
- Added json_object_nget_value function, which removes neccessity to allocate new string when using dotget functions.
- Removed parson_strdup function, it was called only once and could be easilly replaced with appropriate call to parson_strndup.
- Renamed skip_string to skip_quotes, which is a more appropriate name, and made it work on a passed pointer to a string, which is much like skip_char and skip_whitespaces.
- Removed copy_and_remove_whitespaces, it was unncessary, and could be easily replaced with skip_whitepsaces macro.
- Merged parse_escaped_characters and get_string to get_processed_string, which makes more sense.
- Changed is_decimal implementation, to avoid unncessary string duplicating.
- Removed string copying in parse_number value and json_parse_string, since it was unncessary.
2012-11-04 17:45:52 +01:00
|
|
|
skip_char(string);
|
|
|
|
skip_whitespaces(string);
|
2013-11-30 20:28:55 +01:00
|
|
|
if (**string == '}') { /* empty object */
|
|
|
|
skip_char(string);
|
|
|
|
return output_value;
|
|
|
|
}
|
2012-10-16 19:56:54 +02:00
|
|
|
while (**string != '\0') {
|
Code refractoring.
Details:
- Increased STARTING_CAPACITY from 10 to 15.
- Added 2 macros: skip_char and skip_whitespaces.
- Added json_object_nget_value function, which removes neccessity to allocate new string when using dotget functions.
- Removed parson_strdup function, it was called only once and could be easilly replaced with appropriate call to parson_strndup.
- Renamed skip_string to skip_quotes, which is a more appropriate name, and made it work on a passed pointer to a string, which is much like skip_char and skip_whitespaces.
- Removed copy_and_remove_whitespaces, it was unncessary, and could be easily replaced with skip_whitepsaces macro.
- Merged parse_escaped_characters and get_string to get_processed_string, which makes more sense.
- Changed is_decimal implementation, to avoid unncessary string duplicating.
- Removed string copying in parse_number value and json_parse_string, since it was unncessary.
2012-11-04 17:45:52 +01:00
|
|
|
new_key = get_processed_string(string);
|
|
|
|
skip_whitespaces(string);
|
2012-10-19 16:24:24 +02:00
|
|
|
if (!new_key || **string != ':') {
|
2012-10-16 19:56:54 +02:00
|
|
|
json_value_free(output_value);
|
|
|
|
return NULL;
|
|
|
|
}
|
Code refractoring.
Details:
- Increased STARTING_CAPACITY from 10 to 15.
- Added 2 macros: skip_char and skip_whitespaces.
- Added json_object_nget_value function, which removes neccessity to allocate new string when using dotget functions.
- Removed parson_strdup function, it was called only once and could be easilly replaced with appropriate call to parson_strndup.
- Renamed skip_string to skip_quotes, which is a more appropriate name, and made it work on a passed pointer to a string, which is much like skip_char and skip_whitespaces.
- Removed copy_and_remove_whitespaces, it was unncessary, and could be easily replaced with skip_whitepsaces macro.
- Merged parse_escaped_characters and get_string to get_processed_string, which makes more sense.
- Changed is_decimal implementation, to avoid unncessary string duplicating.
- Removed string copying in parse_number value and json_parse_string, since it was unncessary.
2012-11-04 17:45:52 +01:00
|
|
|
skip_char(string);
|
2012-10-24 10:41:05 +02:00
|
|
|
new_value = parse_value(string, nesting);
|
2012-10-19 16:24:24 +02:00
|
|
|
if (!new_value) {
|
2012-10-28 20:18:34 +01:00
|
|
|
parson_free(new_key);
|
2012-10-16 19:56:54 +02:00
|
|
|
json_value_free(output_value);
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-11-07 22:51:03 +01:00
|
|
|
if(!json_object_add(output_object, new_key, new_value)) {
|
2012-10-28 20:18:34 +01:00
|
|
|
parson_free(new_key);
|
2012-10-22 19:20:02 +02:00
|
|
|
parson_free(new_value);
|
2012-10-16 19:56:54 +02:00
|
|
|
json_value_free(output_value);
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-11-03 08:19:28 -04:00
|
|
|
parson_free(new_key);
|
Code refractoring.
Details:
- Increased STARTING_CAPACITY from 10 to 15.
- Added 2 macros: skip_char and skip_whitespaces.
- Added json_object_nget_value function, which removes neccessity to allocate new string when using dotget functions.
- Removed parson_strdup function, it was called only once and could be easilly replaced with appropriate call to parson_strndup.
- Renamed skip_string to skip_quotes, which is a more appropriate name, and made it work on a passed pointer to a string, which is much like skip_char and skip_whitespaces.
- Removed copy_and_remove_whitespaces, it was unncessary, and could be easily replaced with skip_whitepsaces macro.
- Merged parse_escaped_characters and get_string to get_processed_string, which makes more sense.
- Changed is_decimal implementation, to avoid unncessary string duplicating.
- Removed string copying in parse_number value and json_parse_string, since it was unncessary.
2012-11-04 17:45:52 +01:00
|
|
|
skip_whitespaces(string);
|
2013-11-30 20:28:55 +01:00
|
|
|
if (**string != ',')
|
|
|
|
break;
|
Code refractoring.
Details:
- Increased STARTING_CAPACITY from 10 to 15.
- Added 2 macros: skip_char and skip_whitespaces.
- Added json_object_nget_value function, which removes neccessity to allocate new string when using dotget functions.
- Removed parson_strdup function, it was called only once and could be easilly replaced with appropriate call to parson_strndup.
- Renamed skip_string to skip_quotes, which is a more appropriate name, and made it work on a passed pointer to a string, which is much like skip_char and skip_whitespaces.
- Removed copy_and_remove_whitespaces, it was unncessary, and could be easily replaced with skip_whitepsaces macro.
- Merged parse_escaped_characters and get_string to get_processed_string, which makes more sense.
- Changed is_decimal implementation, to avoid unncessary string duplicating.
- Removed string copying in parse_number value and json_parse_string, since it was unncessary.
2012-11-04 17:45:52 +01:00
|
|
|
skip_char(string);
|
|
|
|
skip_whitespaces(string);
|
2012-10-16 19:56:54 +02:00
|
|
|
}
|
Code refractoring.
Details:
- Increased STARTING_CAPACITY from 10 to 15.
- Added 2 macros: skip_char and skip_whitespaces.
- Added json_object_nget_value function, which removes neccessity to allocate new string when using dotget functions.
- Removed parson_strdup function, it was called only once and could be easilly replaced with appropriate call to parson_strndup.
- Renamed skip_string to skip_quotes, which is a more appropriate name, and made it work on a passed pointer to a string, which is much like skip_char and skip_whitespaces.
- Removed copy_and_remove_whitespaces, it was unncessary, and could be easily replaced with skip_whitepsaces macro.
- Merged parse_escaped_characters and get_string to get_processed_string, which makes more sense.
- Changed is_decimal implementation, to avoid unncessary string duplicating.
- Removed string copying in parse_number value and json_parse_string, since it was unncessary.
2012-11-04 17:45:52 +01:00
|
|
|
skip_whitespaces(string);
|
2012-11-07 22:51:03 +01:00
|
|
|
if (**string != '}' || /* Trim object after parsing is over */
|
|
|
|
json_object_resize(output_object, json_object_get_count(output_object)) == ERROR) {
|
|
|
|
json_value_free(output_value);
|
|
|
|
return NULL;
|
|
|
|
}
|
Code refractoring.
Details:
- Increased STARTING_CAPACITY from 10 to 15.
- Added 2 macros: skip_char and skip_whitespaces.
- Added json_object_nget_value function, which removes neccessity to allocate new string when using dotget functions.
- Removed parson_strdup function, it was called only once and could be easilly replaced with appropriate call to parson_strndup.
- Renamed skip_string to skip_quotes, which is a more appropriate name, and made it work on a passed pointer to a string, which is much like skip_char and skip_whitespaces.
- Removed copy_and_remove_whitespaces, it was unncessary, and could be easily replaced with skip_whitepsaces macro.
- Merged parse_escaped_characters and get_string to get_processed_string, which makes more sense.
- Changed is_decimal implementation, to avoid unncessary string duplicating.
- Removed string copying in parse_number value and json_parse_string, since it was unncessary.
2012-11-04 17:45:52 +01:00
|
|
|
skip_char(string);
|
2012-10-16 19:56:54 +02:00
|
|
|
return output_value;
|
|
|
|
}
|
|
|
|
|
2012-10-24 10:41:05 +02:00
|
|
|
static JSON_Value * parse_array_value(const char **string, size_t nesting) {
|
2012-11-07 22:51:03 +01:00
|
|
|
JSON_Value *output_value = json_value_init_array(), *new_array_value = NULL;
|
|
|
|
JSON_Array *output_array = json_value_get_array(output_value);
|
2013-11-30 20:28:55 +01:00
|
|
|
if (!output_value)
|
|
|
|
return NULL;
|
Code refractoring.
Details:
- Increased STARTING_CAPACITY from 10 to 15.
- Added 2 macros: skip_char and skip_whitespaces.
- Added json_object_nget_value function, which removes neccessity to allocate new string when using dotget functions.
- Removed parson_strdup function, it was called only once and could be easilly replaced with appropriate call to parson_strndup.
- Renamed skip_string to skip_quotes, which is a more appropriate name, and made it work on a passed pointer to a string, which is much like skip_char and skip_whitespaces.
- Removed copy_and_remove_whitespaces, it was unncessary, and could be easily replaced with skip_whitepsaces macro.
- Merged parse_escaped_characters and get_string to get_processed_string, which makes more sense.
- Changed is_decimal implementation, to avoid unncessary string duplicating.
- Removed string copying in parse_number value and json_parse_string, since it was unncessary.
2012-11-04 17:45:52 +01:00
|
|
|
skip_char(string);
|
|
|
|
skip_whitespaces(string);
|
2012-10-16 19:56:54 +02:00
|
|
|
if (**string == ']') { /* empty array */
|
Code refractoring.
Details:
- Increased STARTING_CAPACITY from 10 to 15.
- Added 2 macros: skip_char and skip_whitespaces.
- Added json_object_nget_value function, which removes neccessity to allocate new string when using dotget functions.
- Removed parson_strdup function, it was called only once and could be easilly replaced with appropriate call to parson_strndup.
- Renamed skip_string to skip_quotes, which is a more appropriate name, and made it work on a passed pointer to a string, which is much like skip_char and skip_whitespaces.
- Removed copy_and_remove_whitespaces, it was unncessary, and could be easily replaced with skip_whitepsaces macro.
- Merged parse_escaped_characters and get_string to get_processed_string, which makes more sense.
- Changed is_decimal implementation, to avoid unncessary string duplicating.
- Removed string copying in parse_number value and json_parse_string, since it was unncessary.
2012-11-04 17:45:52 +01:00
|
|
|
skip_char(string);
|
2012-10-16 19:56:54 +02:00
|
|
|
return output_value;
|
|
|
|
}
|
|
|
|
while (**string != '\0') {
|
2012-10-24 10:41:05 +02:00
|
|
|
new_array_value = parse_value(string, nesting);
|
2012-10-22 19:20:02 +02:00
|
|
|
if (!new_array_value) {
|
2012-10-16 19:56:54 +02:00
|
|
|
json_value_free(output_value);
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-11-07 22:51:03 +01:00
|
|
|
if(json_array_add(output_array, new_array_value) == ERROR) {
|
2012-10-22 19:20:02 +02:00
|
|
|
parson_free(new_array_value);
|
2012-10-19 16:24:24 +02:00
|
|
|
json_value_free(output_value);
|
|
|
|
return NULL;
|
|
|
|
}
|
Code refractoring.
Details:
- Increased STARTING_CAPACITY from 10 to 15.
- Added 2 macros: skip_char and skip_whitespaces.
- Added json_object_nget_value function, which removes neccessity to allocate new string when using dotget functions.
- Removed parson_strdup function, it was called only once and could be easilly replaced with appropriate call to parson_strndup.
- Renamed skip_string to skip_quotes, which is a more appropriate name, and made it work on a passed pointer to a string, which is much like skip_char and skip_whitespaces.
- Removed copy_and_remove_whitespaces, it was unncessary, and could be easily replaced with skip_whitepsaces macro.
- Merged parse_escaped_characters and get_string to get_processed_string, which makes more sense.
- Changed is_decimal implementation, to avoid unncessary string duplicating.
- Removed string copying in parse_number value and json_parse_string, since it was unncessary.
2012-11-04 17:45:52 +01:00
|
|
|
skip_whitespaces(string);
|
2013-11-30 20:28:55 +01:00
|
|
|
if (**string != ',')
|
|
|
|
break;
|
Code refractoring.
Details:
- Increased STARTING_CAPACITY from 10 to 15.
- Added 2 macros: skip_char and skip_whitespaces.
- Added json_object_nget_value function, which removes neccessity to allocate new string when using dotget functions.
- Removed parson_strdup function, it was called only once and could be easilly replaced with appropriate call to parson_strndup.
- Renamed skip_string to skip_quotes, which is a more appropriate name, and made it work on a passed pointer to a string, which is much like skip_char and skip_whitespaces.
- Removed copy_and_remove_whitespaces, it was unncessary, and could be easily replaced with skip_whitepsaces macro.
- Merged parse_escaped_characters and get_string to get_processed_string, which makes more sense.
- Changed is_decimal implementation, to avoid unncessary string duplicating.
- Removed string copying in parse_number value and json_parse_string, since it was unncessary.
2012-11-04 17:45:52 +01:00
|
|
|
skip_char(string);
|
|
|
|
skip_whitespaces(string);
|
2012-10-16 19:56:54 +02:00
|
|
|
}
|
Code refractoring.
Details:
- Increased STARTING_CAPACITY from 10 to 15.
- Added 2 macros: skip_char and skip_whitespaces.
- Added json_object_nget_value function, which removes neccessity to allocate new string when using dotget functions.
- Removed parson_strdup function, it was called only once and could be easilly replaced with appropriate call to parson_strndup.
- Renamed skip_string to skip_quotes, which is a more appropriate name, and made it work on a passed pointer to a string, which is much like skip_char and skip_whitespaces.
- Removed copy_and_remove_whitespaces, it was unncessary, and could be easily replaced with skip_whitepsaces macro.
- Merged parse_escaped_characters and get_string to get_processed_string, which makes more sense.
- Changed is_decimal implementation, to avoid unncessary string duplicating.
- Removed string copying in parse_number value and json_parse_string, since it was unncessary.
2012-11-04 17:45:52 +01:00
|
|
|
skip_whitespaces(string);
|
2012-11-07 22:51:03 +01:00
|
|
|
if (**string != ']' || /* Trim array after parsing is over */
|
|
|
|
json_array_resize(output_array, json_array_get_count(output_array)) == ERROR) {
|
2012-10-16 19:56:54 +02:00
|
|
|
json_value_free(output_value);
|
|
|
|
return NULL;
|
|
|
|
}
|
Code refractoring.
Details:
- Increased STARTING_CAPACITY from 10 to 15.
- Added 2 macros: skip_char and skip_whitespaces.
- Added json_object_nget_value function, which removes neccessity to allocate new string when using dotget functions.
- Removed parson_strdup function, it was called only once and could be easilly replaced with appropriate call to parson_strndup.
- Renamed skip_string to skip_quotes, which is a more appropriate name, and made it work on a passed pointer to a string, which is much like skip_char and skip_whitespaces.
- Removed copy_and_remove_whitespaces, it was unncessary, and could be easily replaced with skip_whitepsaces macro.
- Merged parse_escaped_characters and get_string to get_processed_string, which makes more sense.
- Changed is_decimal implementation, to avoid unncessary string duplicating.
- Removed string copying in parse_number value and json_parse_string, since it was unncessary.
2012-11-04 17:45:52 +01:00
|
|
|
skip_char(string);
|
2012-10-16 19:56:54 +02:00
|
|
|
return output_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSON_Value * parse_string_value(const char **string) {
|
Code refractoring.
Details:
- Increased STARTING_CAPACITY from 10 to 15.
- Added 2 macros: skip_char and skip_whitespaces.
- Added json_object_nget_value function, which removes neccessity to allocate new string when using dotget functions.
- Removed parson_strdup function, it was called only once and could be easilly replaced with appropriate call to parson_strndup.
- Renamed skip_string to skip_quotes, which is a more appropriate name, and made it work on a passed pointer to a string, which is much like skip_char and skip_whitespaces.
- Removed copy_and_remove_whitespaces, it was unncessary, and could be easily replaced with skip_whitepsaces macro.
- Merged parse_escaped_characters and get_string to get_processed_string, which makes more sense.
- Changed is_decimal implementation, to avoid unncessary string duplicating.
- Removed string copying in parse_number value and json_parse_string, since it was unncessary.
2012-11-04 17:45:52 +01:00
|
|
|
const char *new_string = get_processed_string(string);
|
2013-11-30 20:28:55 +01:00
|
|
|
if (!new_string)
|
|
|
|
return NULL;
|
2012-10-16 19:56:54 +02:00
|
|
|
return json_value_init_string(new_string);
|
|
|
|
}
|
|
|
|
|
2012-10-18 15:25:11 +02:00
|
|
|
static JSON_Value * parse_boolean_value(const char **string) {
|
2012-10-16 19:56:54 +02:00
|
|
|
size_t true_token_size = sizeof_token("true");
|
2012-11-03 08:19:28 -04:00
|
|
|
size_t false_token_size = sizeof_token("false");
|
|
|
|
if (strncmp("true", *string, true_token_size) == 0) {
|
2012-10-16 19:56:54 +02:00
|
|
|
*string += true_token_size;
|
2012-10-18 15:25:11 +02:00
|
|
|
return json_value_init_boolean(1);
|
2012-10-16 19:56:54 +02:00
|
|
|
} else if (strncmp("false", *string, false_token_size) == 0) {
|
|
|
|
*string += false_token_size;
|
2012-10-18 15:25:11 +02:00
|
|
|
return json_value_init_boolean(0);
|
2012-10-16 19:56:54 +02:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSON_Value * parse_number_value(const char **string) {
|
2012-10-24 10:41:05 +02:00
|
|
|
char *end;
|
|
|
|
double number = strtod(*string, &end);
|
|
|
|
JSON_Value *output_value;
|
Code refractoring.
Details:
- Increased STARTING_CAPACITY from 10 to 15.
- Added 2 macros: skip_char and skip_whitespaces.
- Added json_object_nget_value function, which removes neccessity to allocate new string when using dotget functions.
- Removed parson_strdup function, it was called only once and could be easilly replaced with appropriate call to parson_strndup.
- Renamed skip_string to skip_quotes, which is a more appropriate name, and made it work on a passed pointer to a string, which is much like skip_char and skip_whitespaces.
- Removed copy_and_remove_whitespaces, it was unncessary, and could be easily replaced with skip_whitepsaces macro.
- Merged parse_escaped_characters and get_string to get_processed_string, which makes more sense.
- Changed is_decimal implementation, to avoid unncessary string duplicating.
- Removed string copying in parse_number value and json_parse_string, since it was unncessary.
2012-11-04 17:45:52 +01:00
|
|
|
if (is_decimal(*string, end - *string)) {
|
2012-10-24 10:41:05 +02:00
|
|
|
*string = end;
|
|
|
|
output_value = json_value_init_number(number);
|
|
|
|
} else {
|
|
|
|
output_value = NULL;
|
|
|
|
}
|
2012-11-03 08:19:28 -04:00
|
|
|
return output_value;
|
2012-10-16 19:56:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSON_Value * parse_null_value(const char **string) {
|
|
|
|
size_t token_size = sizeof_token("null");
|
|
|
|
if (strncmp("null", *string, token_size) == 0) {
|
|
|
|
*string += token_size;
|
|
|
|
return json_value_init_null();
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parser API */
|
|
|
|
JSON_Value * json_parse_file(const char *filename) {
|
2013-11-30 20:22:16 +01:00
|
|
|
char *file_contents = read_file(filename);
|
|
|
|
JSON_Value *output_value = NULL;
|
2013-11-30 20:28:55 +01:00
|
|
|
if (!file_contents)
|
2013-11-30 20:22:16 +01:00
|
|
|
return NULL;
|
2012-10-16 19:56:54 +02:00
|
|
|
output_value = json_parse_string(file_contents);
|
2012-10-22 19:20:02 +02:00
|
|
|
parson_free(file_contents);
|
2012-10-16 19:56:54 +02:00
|
|
|
return output_value;
|
|
|
|
}
|
|
|
|
|
2013-11-30 20:22:16 +01:00
|
|
|
JSON_Value * json_parse_file_with_comments(const char *filename) {
|
|
|
|
char *file_contents = read_file(filename);
|
|
|
|
JSON_Value *output_value = NULL;
|
2013-11-30 20:28:55 +01:00
|
|
|
if (!file_contents)
|
2013-11-30 20:22:16 +01:00
|
|
|
return NULL;
|
|
|
|
output_value = json_parse_string_with_comments(file_contents);
|
|
|
|
parson_free(file_contents);
|
|
|
|
return output_value;
|
|
|
|
}
|
|
|
|
|
2012-10-16 19:56:54 +02:00
|
|
|
JSON_Value * json_parse_string(const char *string) {
|
2014-04-03 14:59:03 +04:00
|
|
|
if (!string)
|
|
|
|
return NULL;
|
|
|
|
skip_whitespaces(&string);
|
|
|
|
if (*string != '{' && *string != '[')
|
2013-11-30 20:28:55 +01:00
|
|
|
return NULL;
|
2012-11-24 20:59:16 +01:00
|
|
|
return parse_value((const char**)&string, 0);
|
2012-10-16 19:56:54 +02:00
|
|
|
}
|
|
|
|
|
2013-11-30 20:22:16 +01:00
|
|
|
JSON_Value * json_parse_string_with_comments(const char *string) {
|
|
|
|
JSON_Value *result = NULL;
|
|
|
|
char *string_mutable_copy = NULL, *string_mutable_copy_ptr = NULL;
|
|
|
|
string_mutable_copy = parson_strndup(string, strlen(string));
|
2013-11-30 20:28:55 +01:00
|
|
|
if (!string_mutable_copy)
|
2013-11-30 20:22:16 +01:00
|
|
|
return NULL;
|
|
|
|
remove_comments(string_mutable_copy, "/*", "*/");
|
|
|
|
remove_comments(string_mutable_copy, "//", "\n");
|
|
|
|
string_mutable_copy_ptr = string_mutable_copy;
|
|
|
|
skip_whitespaces(&string_mutable_copy_ptr);
|
|
|
|
if (*string_mutable_copy_ptr != '{' && *string_mutable_copy_ptr != '[') {
|
|
|
|
parson_free(string_mutable_copy);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
result = parse_value((const char**)&string_mutable_copy_ptr, 0);
|
|
|
|
parson_free(string_mutable_copy);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-16 19:56:54 +02:00
|
|
|
/* JSON Object API */
|
2013-11-30 20:22:16 +01:00
|
|
|
|
2012-10-16 19:56:54 +02:00
|
|
|
JSON_Value * json_object_get_value(const JSON_Object *object, const char *name) {
|
2012-11-07 22:51:03 +01:00
|
|
|
return json_object_nget_value(object, name, strlen(name));
|
2012-10-16 19:56:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const char * json_object_get_string(const JSON_Object *object, const char *name) {
|
|
|
|
return json_value_get_string(json_object_get_value(object, name));
|
|
|
|
}
|
|
|
|
|
|
|
|
double json_object_get_number(const JSON_Object *object, const char *name) {
|
|
|
|
return json_value_get_number(json_object_get_value(object, name));
|
|
|
|
}
|
|
|
|
|
|
|
|
JSON_Object * json_object_get_object(const JSON_Object *object, const char *name) {
|
|
|
|
return json_value_get_object(json_object_get_value(object, name));
|
|
|
|
}
|
|
|
|
|
|
|
|
JSON_Array * json_object_get_array(const JSON_Object *object, const char *name) {
|
|
|
|
return json_value_get_array(json_object_get_value(object, name));
|
|
|
|
}
|
|
|
|
|
2012-10-18 15:25:11 +02:00
|
|
|
int json_object_get_boolean(const JSON_Object *object, const char *name) {
|
|
|
|
return json_value_get_boolean(json_object_get_value(object, name));
|
2012-10-16 19:56:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
JSON_Value * json_object_dotget_value(const JSON_Object *object, const char *name) {
|
Code refractoring.
Details:
- Increased STARTING_CAPACITY from 10 to 15.
- Added 2 macros: skip_char and skip_whitespaces.
- Added json_object_nget_value function, which removes neccessity to allocate new string when using dotget functions.
- Removed parson_strdup function, it was called only once and could be easilly replaced with appropriate call to parson_strndup.
- Renamed skip_string to skip_quotes, which is a more appropriate name, and made it work on a passed pointer to a string, which is much like skip_char and skip_whitespaces.
- Removed copy_and_remove_whitespaces, it was unncessary, and could be easily replaced with skip_whitepsaces macro.
- Merged parse_escaped_characters and get_string to get_processed_string, which makes more sense.
- Changed is_decimal implementation, to avoid unncessary string duplicating.
- Removed string copying in parse_number value and json_parse_string, since it was unncessary.
2012-11-04 17:45:52 +01:00
|
|
|
const char *dot_position = strchr(name, '.');
|
2013-11-30 20:28:55 +01:00
|
|
|
if (!dot_position)
|
|
|
|
return json_object_get_value(object, name);
|
Code refractoring.
Details:
- Increased STARTING_CAPACITY from 10 to 15.
- Added 2 macros: skip_char and skip_whitespaces.
- Added json_object_nget_value function, which removes neccessity to allocate new string when using dotget functions.
- Removed parson_strdup function, it was called only once and could be easilly replaced with appropriate call to parson_strndup.
- Renamed skip_string to skip_quotes, which is a more appropriate name, and made it work on a passed pointer to a string, which is much like skip_char and skip_whitespaces.
- Removed copy_and_remove_whitespaces, it was unncessary, and could be easily replaced with skip_whitepsaces macro.
- Merged parse_escaped_characters and get_string to get_processed_string, which makes more sense.
- Changed is_decimal implementation, to avoid unncessary string duplicating.
- Removed string copying in parse_number value and json_parse_string, since it was unncessary.
2012-11-04 17:45:52 +01:00
|
|
|
object = json_value_get_object(json_object_nget_value(object, name, dot_position - name));
|
|
|
|
return json_object_dotget_value(object, dot_position + 1);
|
2012-10-16 19:56:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const char * json_object_dotget_string(const JSON_Object *object, const char *name) {
|
|
|
|
return json_value_get_string(json_object_dotget_value(object, name));
|
|
|
|
}
|
|
|
|
|
|
|
|
double json_object_dotget_number(const JSON_Object *object, const char *name) {
|
|
|
|
return json_value_get_number(json_object_dotget_value(object, name));
|
|
|
|
}
|
|
|
|
|
|
|
|
JSON_Object * json_object_dotget_object(const JSON_Object *object, const char *name) {
|
|
|
|
return json_value_get_object(json_object_dotget_value(object, name));
|
|
|
|
}
|
|
|
|
|
|
|
|
JSON_Array * json_object_dotget_array(const JSON_Object *object, const char *name) {
|
|
|
|
return json_value_get_array(json_object_dotget_value(object, name));
|
|
|
|
}
|
|
|
|
|
2012-10-18 15:25:11 +02:00
|
|
|
int json_object_dotget_boolean(const JSON_Object *object, const char *name) {
|
|
|
|
return json_value_get_boolean(json_object_dotget_value(object, name));
|
2012-10-16 19:56:54 +02:00
|
|
|
}
|
|
|
|
|
2012-11-07 22:51:03 +01:00
|
|
|
size_t json_object_get_count(const JSON_Object *object) {
|
|
|
|
return object ? object->count : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char * json_object_get_name(const JSON_Object *object, size_t index) {
|
2013-11-30 20:28:55 +01:00
|
|
|
if (index >= json_object_get_count(object))
|
|
|
|
return NULL;
|
2012-11-07 22:51:03 +01:00
|
|
|
return object->names[index];
|
|
|
|
}
|
|
|
|
|
2012-10-16 19:56:54 +02:00
|
|
|
/* JSON Array API */
|
|
|
|
JSON_Value * json_array_get_value(const JSON_Array *array, size_t index) {
|
2013-11-30 20:28:55 +01:00
|
|
|
if (index >= json_array_get_count(array))
|
|
|
|
return NULL;
|
2012-10-16 19:56:54 +02:00
|
|
|
return array->items[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
const char * json_array_get_string(const JSON_Array *array, size_t index) {
|
|
|
|
return json_value_get_string(json_array_get_value(array, index));
|
|
|
|
}
|
|
|
|
|
|
|
|
double json_array_get_number(const JSON_Array *array, size_t index) {
|
|
|
|
return json_value_get_number(json_array_get_value(array, index));
|
|
|
|
}
|
|
|
|
|
|
|
|
JSON_Object * json_array_get_object(const JSON_Array *array, size_t index) {
|
|
|
|
return json_value_get_object(json_array_get_value(array, index));
|
|
|
|
}
|
|
|
|
|
|
|
|
JSON_Array * json_array_get_array(const JSON_Array *array, size_t index) {
|
|
|
|
return json_value_get_array(json_array_get_value(array, index));
|
|
|
|
}
|
|
|
|
|
2012-10-18 15:25:11 +02:00
|
|
|
int json_array_get_boolean(const JSON_Array *array, size_t index) {
|
|
|
|
return json_value_get_boolean(json_array_get_value(array, index));
|
2012-10-16 19:56:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t json_array_get_count(const JSON_Array *array) {
|
2012-10-24 10:41:05 +02:00
|
|
|
return array ? array->count : 0;
|
2012-10-16 19:56:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* JSON Value API */
|
2012-11-03 19:52:49 +01:00
|
|
|
JSON_Value_Type json_value_get_type(const JSON_Value *value) {
|
2012-10-22 19:20:02 +02:00
|
|
|
return value ? value->type : JSONError;
|
2012-10-16 19:56:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
JSON_Object * json_value_get_object(const JSON_Value *value) {
|
2012-10-22 19:20:02 +02:00
|
|
|
return json_value_get_type(value) == JSONObject ? value->value.object : NULL;
|
2012-10-16 19:56:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
JSON_Array * json_value_get_array(const JSON_Value *value) {
|
2012-10-22 19:20:02 +02:00
|
|
|
return json_value_get_type(value) == JSONArray ? value->value.array : NULL;
|
2012-10-16 19:56:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const char * json_value_get_string(const JSON_Value *value) {
|
2012-10-22 19:20:02 +02:00
|
|
|
return json_value_get_type(value) == JSONString ? value->value.string : NULL;
|
2012-10-16 19:56:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
double json_value_get_number(const JSON_Value *value) {
|
2012-10-22 19:20:02 +02:00
|
|
|
return json_value_get_type(value) == JSONNumber ? value->value.number : 0;
|
2012-10-16 19:56:54 +02:00
|
|
|
}
|
|
|
|
|
2012-10-18 15:25:11 +02:00
|
|
|
int json_value_get_boolean(const JSON_Value *value) {
|
2012-10-22 19:20:02 +02:00
|
|
|
return json_value_get_type(value) == JSONBoolean ? value->value.boolean : -1;
|
2012-10-16 19:56:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void json_value_free(JSON_Value *value) {
|
2012-10-22 19:20:02 +02:00
|
|
|
switch (json_value_get_type(value)) {
|
2012-10-16 19:56:54 +02:00
|
|
|
case JSONObject:
|
|
|
|
json_object_free(value->value.object);
|
|
|
|
break;
|
|
|
|
case JSONString:
|
2012-10-28 20:18:34 +01:00
|
|
|
if (value->value.string) { parson_free(value->value.string); }
|
2012-10-16 19:56:54 +02:00
|
|
|
break;
|
|
|
|
case JSONArray:
|
|
|
|
json_array_free(value->value.array);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2012-10-22 19:20:02 +02:00
|
|
|
parson_free(value);
|
2012-10-16 19:56:54 +02:00
|
|
|
}
|