mirror of https://github.com/kgabis/parson.git
commit
de829803e3
@ -0,0 +1 @@ |
||||
.DS_Store |
@ -0,0 +1,75 @@ |
||||
#parson |
||||
|
||||
##About |
||||
Parson is a small json parser and reader written in C. |
||||
|
||||
##Features |
||||
* Small (only 2 files) |
||||
* Simple API |
||||
* Addressing json values with dot notation (similiar to C structs or objects in most OO languages, e.g. "objectA.objectB.value") |
||||
* C89 compatible |
||||
* Test suites |
||||
|
||||
##Installation |
||||
Run the following code: |
||||
``` |
||||
git clone http://github.com/kgabis/parson.git |
||||
``` |
||||
and copy parson.h and parson.c to you source code tree. |
||||
|
||||
##Example |
||||
Here is a function, which prints basic commit info (date, sha and author) from a github repository. It's also included in tests.c file, you can just uncomment and run it. |
||||
```c |
||||
void print_commit_info(const char *username, const char * repo) { |
||||
JSON_Value *root_value; |
||||
JSON_Array *commits; |
||||
JSON_Object *commit; |
||||
int i; |
||||
|
||||
char curl_command[512]; |
||||
char cleanup_command[256]; |
||||
char *output_filename = "commits.json"; |
||||
|
||||
/* it ain't pretty, but it's not a libcurl tutorial */ |
||||
sprintf(curl_command, "curl \"https://api.github.com/repos/%s/%s/commits\"\ |
||||
> %s 2> /dev/null", username, repo, output_filename); |
||||
sprintf(cleanup_command, "rm -f %s", output_filename); |
||||
system(curl_command); |
||||
|
||||
/* parsing json and validating output */ |
||||
root_value = json_parse_file(output_filename); |
||||
if (root_value == NULL || json_value_get_type(root_value) != JSONArray) { |
||||
system(cleanup_command); |
||||
return; |
||||
} |
||||
|
||||
/* getting array from root value and printing commit info */ |
||||
commits = json_value_get_array(root_value); |
||||
printf("%-10.10s %-10.10s %s\n", "Date", "SHA", "Author"); |
||||
for (i = 0; i < json_array_get_count(commits); i++) { |
||||
commit = json_array_get_object(commits, i); |
||||
printf("%.10s %.10s %s\n", |
||||
json_object_dotget_string(commit, "commit.author.date"), |
||||
json_object_get_string(commit, "sha"), |
||||
json_object_dotget_string(commit, "commit.author.name")); |
||||
} |
||||
|
||||
/* cleanup code */ |
||||
json_value_free(root_value); |
||||
system(cleanup_command); |
||||
} |
||||
``` |
||||
Calling ```print_commit_info("torvalds", "linux");``` prints: |
||||
``` |
||||
Date SHA Author |
||||
2012-10-15 dd8e8c4a2c David Rientjes |
||||
2012-10-15 3ce9e53e78 Michal Marek |
||||
2012-10-14 29bb4cc5e0 Randy Dunlap |
||||
2012-10-15 325adeb55e Ralf Baechle |
||||
2012-10-14 68687c842c Russell King |
||||
2012-10-14 ddffeb8c4d Linus Torvalds |
||||
... |
||||
``` |
||||
|
||||
##License |
||||
[The MIT License (MIT)](http://opensource.org/licenses/mit-license.php) |
@ -0,0 +1,606 @@ |
||||
/*
|
||||
Copyright (c) 2012 Krzysztof Gabis |
||||
|
||||
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: |
||||
|
||||
The above copyright notice and this permission notice shall be included in |
||||
all copies or substantial portions of the Software. |
||||
|
||||
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. |
||||
*/ |
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
#include <string.h> |
||||
#include <ctype.h> |
||||
#include "parson.h" |
||||
|
||||
#define STARTING_CAPACITY 10 |
||||
#define sizeof_token(a) (sizeof(a) - 1) |
||||
|
||||
/* Type definitions */ |
||||
union json_value_value { |
||||
const char * string; |
||||
double number; |
||||
JSON_Object *object; |
||||
JSON_Array *array; |
||||
int bool; |
||||
int null; |
||||
}; |
||||
|
||||
struct json_value_t { |
||||
enum json_value_type type; |
||||
union json_value_value value; |
||||
}; |
||||
|
||||
struct json_object_t { |
||||
const char **names; |
||||
JSON_Value **values; |
||||
size_t count; |
||||
size_t capacity; |
||||
}; |
||||
|
||||
struct json_array_t { |
||||
JSON_Value **items; |
||||
size_t count; |
||||
size_t capacity; |
||||
}; |
||||
|
||||
/* JSON Object */ |
||||
static JSON_Object * json_object_init(); |
||||
static int json_object_add(JSON_Object *object, const char *name, JSON_Value *value); |
||||
static void json_object_free(JSON_Object *object); |
||||
|
||||
/* JSON Array */ |
||||
static JSON_Array * json_array_init(); |
||||
static void json_array_add(JSON_Array *array, JSON_Value *value); |
||||
static void json_array_free(JSON_Array *array); |
||||
|
||||
/* JSON Value */ |
||||
static JSON_Value * json_value_init_object(); |
||||
static JSON_Value * json_value_init_array(); |
||||
static JSON_Value * json_value_init_string(const char *string); |
||||
static JSON_Value * json_value_init_number(double number); |
||||
static JSON_Value * json_value_init_bool(int bool); |
||||
static JSON_Value * json_value_init_null(); |
||||
|
||||
/* Parser */ |
||||
static const char * skip_string(const char *string); |
||||
static char * copy_and_remove_whitespaces(const char *string); |
||||
static int is_utf_string(const char *string); |
||||
static const char * parse_escaped_characters(const char *string); |
||||
static const char * get_string(const char **string); |
||||
static JSON_Value * parse_object_value(const char **string); |
||||
static JSON_Value * parse_array_value(const char **string); |
||||
static JSON_Value * parse_string_value(const char **string); |
||||
static JSON_Value * parse_bool_value(const char **string); |
||||
static JSON_Value * parse_number_value(const char **string); |
||||
static JSON_Value * parse_null_value(const char **string); |
||||
static JSON_Value * parse_value(const char **string); |
||||
|
||||
/* JSON Object */ |
||||
static JSON_Object * json_object_init() { |
||||
JSON_Object *new_object = (JSON_Object*)malloc(sizeof(JSON_Object)); |
||||
new_object->names = (const char**)malloc(sizeof(const char*) * STARTING_CAPACITY); |
||||
new_object->values = (JSON_Value**)malloc(sizeof(JSON_Value*) * STARTING_CAPACITY); |
||||
new_object->capacity = STARTING_CAPACITY; |
||||
new_object->count = 0; |
||||
return new_object; |
||||
} |
||||
|
||||
static int json_object_add(JSON_Object *object, const char *name, JSON_Value *value) { |
||||
size_t index; |
||||
if (object->count >= object->capacity) { |
||||
size_t new_capacity = object->capacity * 2; |
||||
object->names = realloc(object->names, new_capacity * sizeof(const char*)); |
||||
object->values = realloc(object->values, new_capacity * sizeof(JSON_Value*)); |
||||
object->capacity = new_capacity; |
||||
}
|
||||
if (json_object_get_value(object, name) != NULL) { return 0; }
|
||||
index = object->count; |
||||
object->names[index] = strdup(name); |
||||
object->values[index] = value; |
||||
object->count++; |
||||
return 1; |
||||
} |
||||
|
||||
static void json_object_free(JSON_Object *object) { |
||||
int i; |
||||
for (i = 0; i < object->count; i++) { |
||||
free((void*)object->names[i]); |
||||
json_value_free(object->values[i]); |
||||
} |
||||
free(object->names); |
||||
free(object->values); |
||||
free(object); |
||||
} |
||||
|
||||
/* JSON Array */ |
||||
static JSON_Array * json_array_init() { |
||||
JSON_Array *new_array = (JSON_Array*)malloc(sizeof(JSON_Array)); |
||||
new_array->items = (JSON_Value**)malloc(STARTING_CAPACITY * sizeof(JSON_Value*)); |
||||
new_array->capacity = STARTING_CAPACITY; |
||||
new_array->count = 0; |
||||
return new_array; |
||||
} |
||||
|
||||
static void json_array_add(JSON_Array *array, JSON_Value *value) { |
||||
if (array->count >= array->capacity) { |
||||
size_t new_capacity = array->capacity * 2; |
||||
array->items = realloc(array->items, new_capacity * sizeof(JSON_Value*)); |
||||
array->capacity = new_capacity; |
||||
} |
||||
array->items[array->count] = value; |
||||
array->count++; |
||||
} |
||||
|
||||
static void json_array_free(JSON_Array *array) { |
||||
int i; |
||||
for (i = 0; i < array->count; i++) { |
||||
json_value_free(array->items[i]); |
||||
} |
||||
free(array->items); |
||||
free(array); |
||||
} |
||||
|
||||
/* JSON Value */ |
||||
static JSON_Value * json_value_init_object() { |
||||
JSON_Value *new_value = (JSON_Value*)malloc(sizeof(JSON_Value)); |
||||
new_value->type = JSONObject; |
||||
new_value->value.object = json_object_init(); |
||||
return new_value; |
||||
} |
||||
|
||||
static JSON_Value * json_value_init_array() { |
||||
JSON_Value *new_value = (JSON_Value*)malloc(sizeof(JSON_Value)); |
||||
new_value->type = JSONArray; |
||||
new_value->value.array = json_array_init(); |
||||
return new_value; |
||||
} |
||||
|
||||
static JSON_Value * json_value_init_string(const char *string) { |
||||
JSON_Value *new_value = (JSON_Value*)malloc(sizeof(JSON_Value)); |
||||
new_value->type = JSONString; |
||||
new_value->value.string = string; |
||||
return new_value; |
||||
} |
||||
|
||||
static JSON_Value * json_value_init_number(double number) { |
||||
JSON_Value *new_value = (JSON_Value*)malloc(sizeof(JSON_Value)); |
||||
new_value->type = JSONNumber; |
||||
new_value->value.number = number; |
||||
return new_value; |
||||
} |
||||
|
||||
static JSON_Value * json_value_init_bool(int bool) { |
||||
JSON_Value *new_value = (JSON_Value*)malloc(sizeof(JSON_Value)); |
||||
new_value->type = JSONBool; |
||||
new_value->value.bool = bool; |
||||
return new_value; |
||||
} |
||||
|
||||
static JSON_Value * json_value_init_null() { |
||||
JSON_Value *new_value = (JSON_Value*)malloc(sizeof(JSON_Value)); |
||||
new_value->type = JSONNull; |
||||
return new_value; |
||||
} |
||||
|
||||
/* Parser */ |
||||
static const char * skip_string(const char *string) { |
||||
string++; |
||||
while (*string != '\0' && *string != '\"') { |
||||
if (*string == '\\') { string++; if (*string == '\0') { break; } } |
||||
string++; |
||||
} |
||||
if (*string == '\0') { return NULL; } |
||||
return string + 1; |
||||
} |
||||
|
||||
static char *copy_and_remove_whitespaces(const char *string) { |
||||
char *output_string = (char*)malloc(strlen(string) + 1); |
||||
char *output_string_ptr = output_string; |
||||
const char *string_ptr = string; |
||||
const char *skipped_string = NULL; |
||||
char current_char;
|
||||
while (*string_ptr) { |
||||
current_char = *string_ptr;
|
||||
switch (current_char) { |
||||
case ' ': case '\r': case '\n': case '\t': |
||||
string_ptr++; |
||||
break; |
||||
case '\"': |
||||
skipped_string = skip_string(string_ptr); |
||||
if (skipped_string == NULL) { free(output_string); return NULL; } |
||||
strncpy(output_string_ptr, string_ptr, skipped_string - string_ptr); |
||||
output_string_ptr = output_string_ptr + (skipped_string - string_ptr); |
||||
string_ptr = skipped_string; |
||||
break; |
||||
default: |
||||
*output_string_ptr = current_char; |
||||
string_ptr++; |
||||
output_string_ptr++; |
||||
break; |
||||
} |
||||
} |
||||
*output_string_ptr = '\0'; |
||||
output_string = realloc(output_string, strlen(output_string) + 1); |
||||
return output_string; |
||||
} |
||||
|
||||
static int is_utf_string(const char *string) { |
||||
int i; |
||||
if (strlen(string) < 4) { return 0; } |
||||
for (i = 0; i < 4; i++) { if (!isxdigit(string[i])) { return 0; } } |
||||
return 1; |
||||
} |
||||
|
||||
static const char * parse_escaped_characters(const char *string) { |
||||
char *output_string = (char*)malloc(strlen(string) + 1); |
||||
char *output_string_ptr = output_string; |
||||
const char *string_ptr = string; |
||||
char current_char; |
||||
unsigned int utf_val;
|
||||
while (*string_ptr) { |
||||
current_char = *string_ptr; |
||||
if (current_char == '\\') { |
||||
string_ptr++; |
||||
current_char = *string_ptr; |
||||
switch (current_char) { |
||||
case '\"': case '\\': case '/': break; |
||||
case 'b': current_char = '\b'; break; |
||||
case 'f': current_char = '\f'; break; |
||||
case 'n': current_char = '\n'; break; |
||||
case 'r': current_char = '\r'; break; |
||||
case 't': current_char = '\t'; break; |
||||
case 'u': |
||||
string_ptr++; |
||||
if (!is_utf_string(string_ptr) || |
||||
sscanf(string_ptr, "%4x", &utf_val) == EOF) { |
||||
free(output_string); return NULL; |
||||
} |
||||
if (utf_val < 0x80) { |
||||
current_char = utf_val; |
||||
} else if (utf_val < 0x800) { |
||||
*output_string_ptr++ = (utf_val >> 6) | 0xC0; |
||||
current_char = ((utf_val | 0x80) & 0xBF); |
||||
} else { |
||||
*output_string_ptr++ = (utf_val >> 12) | 0xE0; |
||||
*output_string_ptr++ = (((utf_val >> 6) | 0x80) & 0xBF); |
||||
current_char = ((utf_val | 0x80) & 0xBF); |
||||
} |
||||
string_ptr += 3; |
||||
break; |
||||
default: |
||||
free(output_string); |
||||
return NULL; |
||||
break; |
||||
} |
||||
} else if (iscntrl(current_char)) { /* no control characters allowed */ |
||||
free(output_string);
|
||||
return NULL; |
||||
}
|
||||
*output_string_ptr = current_char; |
||||
output_string_ptr++; |
||||
string_ptr++; |
||||
} |
||||
*output_string_ptr = '\0'; |
||||
output_string = realloc(output_string, strlen(output_string) + 1); |
||||
return output_string; |
||||
} |
||||
|
||||
/* Returns contents of a string inside double quotes and parses escaped
|
||||
characters inside. |
||||
Example: "\u006Corem ipsum" -> lorem ipsum */ |
||||
static const char * get_string(const char **string) { |
||||
char *quote_contents; |
||||
const char *parsed_string; |
||||
const char *after_closing_quote_ptr = skip_string(*string);
|
||||
if (after_closing_quote_ptr == NULL) { return NULL; } |
||||
quote_contents = strndup(*string + 1, after_closing_quote_ptr - *string - 2); |
||||
*string = after_closing_quote_ptr; |
||||
parsed_string = parse_escaped_characters(quote_contents); |
||||
free(quote_contents); |
||||
return (const char*)parsed_string; |
||||
} |
||||
|
||||
static JSON_Value * parse_value(const char **string) { |
||||
JSON_Value *output_value = NULL; |
||||
if (*string == NULL) { return NULL; } |
||||
switch ((*string)[0]) { |
||||
case '{': |
||||
output_value = parse_object_value(string); |
||||
break; |
||||
case '[': |
||||
output_value = parse_array_value(string); |
||||
break; |
||||
case '\"': |
||||
output_value = parse_string_value(string); |
||||
break; |
||||
case 'f': |
||||
case 't': |
||||
output_value = parse_bool_value(string); |
||||
break; |
||||
case '-': |
||||
case '0': case '1': case '2': case '3': case '4': |
||||
case '5': case '6': case '7': case '8': case '9': |
||||
output_value = parse_number_value(string); |
||||
break; |
||||
case 'n': |
||||
output_value = parse_null_value(string); |
||||
break; |
||||
default: |
||||
return NULL; |
||||
} |
||||
return output_value; |
||||
} |
||||
|
||||
static JSON_Value * parse_object_value(const char **string) { |
||||
JSON_Value *output_value = json_value_init_object(); |
||||
const char *new_key = NULL; |
||||
JSON_Value *new_value = NULL;
|
||||
(*string)++; |
||||
if (**string == '}') { (*string)++; return output_value; } /* empty object */ |
||||
while (**string != '\0') { |
||||
new_key = get_string(string); |
||||
if (new_key == NULL || **string != ':') { |
||||
json_value_free(output_value); |
||||
return NULL; |
||||
} |
||||
(*string)++; |
||||
new_value = parse_value(string); |
||||
if (new_value == NULL) { |
||||
free((void*)new_key); |
||||
json_value_free(output_value); |
||||
return NULL; |
||||
} |
||||
if(!json_object_add(json_value_get_object(output_value), new_key, new_value)) { |
||||
free((void*)new_key); |
||||
free(new_value); |
||||
json_value_free(output_value); |
||||
return NULL; |
||||
} |
||||
free((void*)new_key);
|
||||
if (**string != ',') { break; } |
||||
(*string)++; |
||||
} |
||||
if (**string != '}') { json_value_free(output_value); return NULL; } |
||||
(*string)++; |
||||
return output_value; |
||||
} |
||||
|
||||
static JSON_Value * parse_array_value(const char **string) { |
||||
JSON_Value *output_value = json_value_init_array(); |
||||
JSON_Value *new_array_value = NULL;
|
||||
(*string)++; |
||||
if (**string == ']') { /* empty array */ |
||||
(*string)++; |
||||
return output_value; |
||||
} |
||||
while (**string != '\0') { |
||||
new_array_value = parse_value(string); |
||||
if (new_array_value == NULL) { |
||||
json_value_free(output_value); |
||||
return NULL; |
||||
} |
||||
json_array_add(json_value_get_array(output_value), new_array_value); |
||||
if (**string != ',') { break; } |
||||
(*string)++;
|
||||
} |
||||
if (**string != ']') { |
||||
json_value_free(output_value); |
||||
return NULL; |
||||
} |
||||
(*string)++; |
||||
return output_value; |
||||
} |
||||
|
||||
static JSON_Value * parse_string_value(const char **string) { |
||||
const char *new_string = get_string(string); |
||||
if (new_string == NULL) { return NULL; } |
||||
return json_value_init_string(new_string); |
||||
} |
||||
|
||||
static JSON_Value * parse_bool_value(const char **string) { |
||||
size_t true_token_size = sizeof_token("true"); |
||||
size_t false_token_size = sizeof_token("false");
|
||||
if (strncmp("true", *string, true_token_size) == 0) {
|
||||
*string += true_token_size; |
||||
return json_value_init_bool(1); |
||||
} else if (strncmp("false", *string, false_token_size) == 0) { |
||||
*string += false_token_size; |
||||
return json_value_init_bool(0); |
||||
} |
||||
return NULL; |
||||
} |
||||
|
||||
static JSON_Value * parse_number_value(const char **string) { |
||||
return json_value_init_number(strtod(*string, (char**)string)); |
||||
} |
||||
|
||||
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) { |
||||
FILE *fp = fopen(filename, "r"); |
||||
size_t file_size; |
||||
char *file_contents; |
||||
JSON_Value *output_value;
|
||||
if (fp == NULL) { return NULL; } |
||||
fseek(fp, 0L, SEEK_END); |
||||
file_size = ftell(fp); |
||||
rewind(fp); |
||||
file_contents = (char*)malloc(sizeof(char) * (file_size + 1)); |
||||
fread(file_contents, file_size, 1, fp); |
||||
fclose(fp); |
||||
file_contents[file_size] = '\0'; |
||||
output_value = json_parse_string(file_contents); |
||||
free(file_contents); |
||||
return output_value; |
||||
} |
||||
|
||||
JSON_Value * json_parse_string(const char *string) { |
||||
JSON_Value *output_value = NULL; |
||||
const char *json_string = string ? copy_and_remove_whitespaces(string) : NULL; |
||||
const char *json_string_ptr = json_string; |
||||
if (json_string == NULL) { return NULL; } |
||||
if (*json_string == '{' || *json_string == '[') { |
||||
output_value = parse_value((const char**)&json_string_ptr); |
||||
} |
||||
free((void*)json_string); |
||||
return output_value; |
||||
} |
||||
|
||||
/* JSON Object API */ |
||||
JSON_Value * json_object_get_value(const JSON_Object *object, const char *name) { |
||||
int i; |
||||
if (object == NULL) { return NULL; } |
||||
for (i = 0; i < object->count; i++) { |
||||
if (strcmp(object->names[i], name) == 0) { return object->values[i]; } |
||||
} |
||||
return NULL; |
||||
} |
||||
|
||||
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)); |
||||
} |
||||
|
||||
int json_object_get_bool(const JSON_Object *object, const char *name) { |
||||
return json_value_get_bool(json_object_get_value(object, name)); |
||||
} |
||||
|
||||
JSON_Value * json_object_dotget_value(const JSON_Object *object, const char *name) { |
||||
const char *object_name, *dot_position = strchr(name, '.'); |
||||
JSON_Value *output_value; |
||||
if (dot_position == NULL) { return json_object_get_value(object, name); } |
||||
object_name = strndup(name, dot_position - name); |
||||
output_value = json_object_dotget_value(json_object_get_object(object, object_name), |
||||
dot_position + 1); |
||||
free((void*)object_name); |
||||
return output_value; |
||||
} |
||||
|
||||
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)); |
||||
} |
||||
|
||||
int json_object_dotget_bool(const JSON_Object *object, const char *name) { |
||||
return json_value_get_bool(json_object_dotget_value(object, name)); |
||||
} |
||||
|
||||
/* JSON Array API */ |
||||
JSON_Value * json_array_get_value(const JSON_Array *array, size_t index) { |
||||
if (index >= json_array_get_count(array)) { return NULL; } |
||||
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)); |
||||
} |
||||
|
||||
int json_array_get_bool(const JSON_Array *array, size_t index) { |
||||
return json_value_get_bool(json_array_get_value(array, index)); |
||||
} |
||||
|
||||
size_t json_array_get_count(const JSON_Array *array) { |
||||
return array != NULL ? array->count : 0; |
||||
} |
||||
|
||||
/* JSON Value API */ |
||||
enum json_value_type json_value_get_type(const JSON_Value *value) { |
||||
return value != NULL ? value->type : 0; |
||||
} |
||||
|
||||
JSON_Object * json_value_get_object(const JSON_Value *value) { |
||||
if (value == NULL || value->type != JSONObject) { return NULL; } |
||||
return value->value.object; |
||||
} |
||||
|
||||
JSON_Array * json_value_get_array(const JSON_Value *value) { |
||||
if (value == NULL || value->type != JSONArray) { return NULL; } |
||||
return value->value.array; |
||||
} |
||||
|
||||
const char * json_value_get_string(const JSON_Value *value) { |
||||
if (value == NULL || value->type != JSONString) { return NULL; } |
||||
return value->value.string; |
||||
} |
||||
|
||||
double json_value_get_number(const JSON_Value *value) { |
||||
if (value == NULL || value->type != JSONNumber) { return 0; } |
||||
return value->value.number; |
||||
} |
||||
|
||||
int json_value_get_bool(const JSON_Value *value) { |
||||
if (value == NULL || value->type != JSONBool) { return -1; } |
||||
return value->value.bool; |
||||
} |
||||
|
||||
void json_value_free(JSON_Value *value) { |
||||
switch (value->type) { |
||||
case JSONObject: |
||||
json_object_free(value->value.object); |
||||
break; |
||||
case JSONString: |
||||
if (value->value.string != NULL) { free((void*)value->value.string); } |
||||
break; |
||||
case JSONArray: |
||||
json_array_free(value->value.array); |
||||
break; |
||||
default: |
||||
break; |
||||
} |
||||
free(value); |
||||
} |
@ -0,0 +1,94 @@ |
||||
/*
|
||||
Copyright (c) 2012 Krzysztof Gabis |
||||
|
||||
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: |
||||
|
||||
The above copyright notice and this permission notice shall be included in |
||||
all copies or substantial portions of the Software. |
||||
|
||||
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. |
||||
*/ |
||||
#ifndef parson_parson_h |
||||
#define parson_parson_h |
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" |
||||
{ |
||||
#endif |
||||
|
||||
/* Data structures, enums and typedefs */ |
||||
struct json_object_t; |
||||
typedef struct json_object_t JSON_Object; |
||||
struct json_array_t; |
||||
typedef struct json_array_t JSON_Array; |
||||
struct json_value_t; |
||||
typedef struct json_value_t JSON_Value; |
||||
|
||||
enum json_value_type { |
||||
JSONNull = 1, |
||||
JSONString = 2, |
||||
JSONNumber = 3, |
||||
JSONObject = 4, |
||||
JSONArray = 5, |
||||
JSONBool = 6 |
||||
}; |
||||
|
||||
/* Parses first JSON value in a file, returns NULL in case of error */ |
||||
JSON_Value * json_parse_file(const char *filename); |
||||
|
||||
/* Parses first JSON value in a string, returns NULL in case of error */ |
||||
JSON_Value * json_parse_string(const char *string); |
||||
|
||||
/* JSON Object */ |
||||
JSON_Value * json_object_get_value(const JSON_Object *object, const char *name); |
||||
const char * json_object_get_string(const JSON_Object *object, const char *name); |
||||
double json_object_get_number(const JSON_Object *object, const char *name); |
||||
JSON_Object * json_object_get_object(const JSON_Object *object, const char *name); |
||||
JSON_Array * json_object_get_array(const JSON_Object *object, const char *name); |
||||
int json_object_get_bool(const JSON_Object *object, const char *name); |
||||
|
||||
/* dotget functions enable addressing values with dot notation in nested objects,
|
||||
just like in structs or c++/java/c# objects (e.g. objectA.objectB.value).
|
||||
Because valid names in JSON can contain dots, some values may be inaccessible |
||||
this way. */
|
||||
JSON_Value * json_object_dotget_value(const JSON_Object *object, const char *name); |
||||
const char * json_object_dotget_string(const JSON_Object *object, const char *name); |
||||
double json_object_dotget_number(const JSON_Object *object, const char *name); |
||||
JSON_Object * json_object_dotget_object(const JSON_Object *object, const char *name); |
||||
JSON_Array * json_object_dotget_array(const JSON_Object *object, const char *name); |
||||
int json_object_dotget_bool(const JSON_Object *object, const char *name); |
||||
|
||||
/* JSON Array */ |
||||
JSON_Value * json_array_get_value(const JSON_Array *array, size_t index); |
||||
const char * json_array_get_string(const JSON_Array *array, size_t index); |
||||
double json_array_get_number(const JSON_Array *array, size_t index); |
||||
JSON_Object * json_array_get_object(const JSON_Array *array, size_t index); |
||||
JSON_Array * json_array_get_array(const JSON_Array *array, size_t index); |
||||
int json_array_get_bool(const JSON_Array *array, size_t index); |
||||
size_t json_array_get_count(const JSON_Array *array); |
||||
|
||||
/* JSON Value */ |
||||
enum json_value_type json_value_get_type(const JSON_Value *value); |
||||
JSON_Object * json_value_get_object(const JSON_Value *value); |
||||
JSON_Array * json_value_get_array(const JSON_Value *value); |
||||
const char * json_value_get_string(const JSON_Value *value); |
||||
double json_value_get_number(const JSON_Value *value); |
||||
int json_value_get_bool(const JSON_Value *value); |
||||
void json_value_free(JSON_Value *value); |
||||
|
||||
#ifdef __cplusplus |
||||
} |
||||
#endif |
||||
|
||||
#endif |
@ -0,0 +1,209 @@ |
||||
/*
|
||||
Copyright (c) 2012 Krzysztof Gabis |
||||
|
||||
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: |
||||
|
||||
The above copyright notice and this permission notice shall be included in |
||||
all copies or substantial portions of the Software. |
||||
|
||||
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. |
||||
*/ |
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
#include <string.h> |
||||
#include "parson.h" |
||||
|
||||
#define TEST(A) printf("%-72s-",#A); \ |
||||
if(A){puts(" OK");tests_passed++;} \
|
||||
else{puts(" FAIL");tests_failed++;} |
||||
#define STREQ(A, B) (strcmp(A, B) == 0) |
||||
|
||||
void test_suite_1(); |
||||
void test_suite_2(); |
||||
void test_suite_3(); |
||||
|
||||
void print_commit_info(const char *username, const char * repo); |
||||
|
||||
static int tests_passed; |
||||
static int tests_failed; |
||||
|
||||
int main(int argc, const char * argv[]) { |
||||
/* Example function from readme file: */ |
||||
/* print_commit_info("torvalds", "linux"); */ |
||||
test_suite_1(); |
||||
test_suite_2(); |
||||
test_suite_3(); |
||||
printf("Tests failed: %d\n", tests_failed); |
||||
printf("Tests passed: %d\n", tests_passed); |
||||
return 0; |
||||
} |
||||
|
||||
/* 3 test files from json.org */ |
||||
void test_suite_1() { |
||||
int i; |
||||
JSON_Value *root_value; |
||||
char filename[128]; |
||||
for (i = 1; i <= 3; i++) { |
||||
filename[0] = '\0'; |
||||
sprintf(filename, "tests/test_1_%d.txt", i); |
||||
printf("Testing %s:\n", filename); |
||||
root_value = json_parse_file(filename); |
||||
TEST(root_value != NULL); |
||||
if (root_value != NULL) { json_value_free(root_value); } |
||||
} |
||||
} |
||||
|
||||
/* Testing correctness of parsed values */ |
||||
void test_suite_2() { |
||||
JSON_Value *root_value; |
||||
JSON_Object *object; |
||||
JSON_Array *array; |
||||
int i; |
||||
const char *filename = "tests/test_2.txt"; |
||||
|
||||
root_value = json_parse_file(filename); |
||||
if(root_value == NULL) { |
||||
tests_failed++; |
||||
return; |
||||
} |
||||
if (json_value_get_type(root_value) != JSONObject) { |
||||
printf("Root is not a JSON object.\n"); |
||||
tests_failed++; |
||||
json_value_free(root_value); |
||||
return; |
||||
} |
||||
|
||||
object = json_value_get_object(root_value); |
||||
printf("Testing %s:\n", filename); |
||||
|
||||
TEST(STREQ(json_object_get_string(object, "string"), "lorem ipsum")); |
||||
TEST(STREQ(json_object_get_string(object, "utf string"), "lorem ipsum")); |
||||
TEST(json_object_get_number(object, "positive one") == 1.0); |
||||
TEST(json_object_get_number(object, "negative one") == -1.0); |
||||
TEST(json_object_get_number(object, "hard to parse number") == -0.000314); |
||||
TEST(json_object_get_bool(object, "bool true")); |
||||
TEST(!json_object_get_bool(object, "bool false"));
|
||||
TEST(json_value_get_type(json_object_get_value(object, "null")) == JSONNull); |
||||
|
||||
array = json_object_get_array(object, "string array"); |
||||
if (array != NULL && json_array_get_count(array) > 1) { |
||||
TEST(STREQ(json_array_get_string(array, 0), "lorem")); |
||||
TEST(STREQ(json_array_get_string(array, 1), "ipsum")); |
||||
} else { |
||||
tests_failed++; |
||||
} |
||||
|
||||
array = json_object_get_array(object, "x^2 array"); |
||||
if (array != NULL) { |
||||
for (i = 0; i < json_array_get_count(array); i++) { |
||||
TEST(json_array_get_number(array, i) == (i * i)); |
||||
} |
||||
} else { |
||||
tests_failed++; |
||||
} |
||||
|
||||
TEST(json_object_get_array(object, "non existent array") == NULL); |
||||
TEST(STREQ(json_object_dotget_string(object, "object.nested string"), "str")); |
||||
TEST(json_object_dotget_bool(object, "object.nested true")); |
||||
TEST(!json_object_dotget_bool(object, "object.nested false")); |
||||
TEST(json_object_dotget_value(object, "object.nested null") != NULL); |
||||
TEST(json_object_dotget_number(object, "object.nested number") == 123); |
||||
|
||||
TEST(json_object_dotget_value(object, "should.be.null") == NULL); |
||||
TEST(json_object_dotget_value(object, "should.be.null.") == NULL); |
||||
TEST(json_object_dotget_value(object, ".") == NULL); |
||||
TEST(json_object_dotget_value(object, "") == NULL); |
||||
|
||||
array = json_object_dotget_array(object, "object.nested array"); |
||||
if (array != NULL && json_array_get_count(array) > 1) { |
||||
TEST(STREQ(json_array_get_string(array, 0), "lorem")); |
||||
TEST(STREQ(json_array_get_string(array, 1), "ipsum")); |
||||
} else { |
||||
tests_failed++; |
||||
} |
||||
TEST(json_object_dotget_bool(object, "nested true")); |
||||
|
||||
json_value_free(root_value); |
||||
} |
||||
|
||||
/* Testing values, on which parsing should fail */ |
||||
void test_suite_3() { |
||||
TEST(json_parse_string(NULL) == NULL); |
||||
TEST(json_parse_string("") == NULL); /* empty string */ |
||||
TEST(json_parse_string("[\"lorem\",]") == NULL); |
||||
TEST(json_parse_string("[,]") == NULL); |
||||
TEST(json_parse_string("[,") == NULL); |
||||
TEST(json_parse_string("[") == NULL); |
||||
TEST(json_parse_string("]") == NULL); |
||||
TEST(json_parse_string("{\"a\":0,\"a\":0}") == NULL); /* duplicate keys */ |
||||
TEST(json_parse_string("{:,}") == NULL); |
||||
TEST(json_parse_string("{,}") == NULL); |
||||
TEST(json_parse_string("{,") == NULL); |
||||
TEST(json_parse_string("{:") == NULL); |
||||
TEST(json_parse_string("{") == NULL); |
||||
TEST(json_parse_string("}") == NULL); |
||||
TEST(json_parse_string("x") == NULL); |
||||
TEST(json_parse_string("\"string\"") == NULL); |
||||
TEST(json_parse_string("{:\"no name\"}") == NULL); |
||||
TEST(json_parse_string("[,\"no first value\"]") == NULL); |
||||
TEST(json_parse_string("[\"\\u00zz\"]") == NULL); /* invalid utf value */ |
||||
TEST(json_parse_string("[\"\\\"]") == NULL); /* control character */ |
||||
TEST(json_parse_string("[\"\"\"]") == NULL); /* control character */ |
||||
TEST(json_parse_string("[\"\0\"]") == NULL); /* control character */ |
||||
TEST(json_parse_string("[\"\a\"]") == NULL); /* control character */ |
||||
TEST(json_parse_string("[\"\b\"]") == NULL); /* control character */ |
||||
TEST(json_parse_string("[\"\t\"]") == NULL); /* control character */ |
||||
TEST(json_parse_string("[\"\n\"]") == NULL); /* control character */ |
||||
TEST(json_parse_string("[\"\f\"]") == NULL); /* control character */ |
||||
TEST(json_parse_string("[\"\r\"]") == NULL); /* control character */ |
||||
} |
||||
|
||||
void print_commit_info(const char *username, const char * repo) { |
||||
JSON_Value *root_value; |
||||
JSON_Array *commits; |
||||
JSON_Object *commit; |
||||
int i; |
||||
|
||||
char curl_command[512]; |
||||
char cleanup_command[256]; |
||||
char *output_filename = "commits.json"; |
||||
|
||||
/* it ain't pretty, but it's not a libcurl tutorial */ |
||||
sprintf(curl_command, "curl \"https://api.github.com/repos/%s/%s/commits\"\
|
||||
> %s 2> /dev/null", username, repo, output_filename); |
||||
sprintf(cleanup_command, "rm -f %s", output_filename); |
||||
system(curl_command); |
||||
|
||||
/* parsing json and validating output */ |
||||
root_value = json_parse_file(output_filename); |
||||
if (root_value == NULL || json_value_get_type(root_value) != JSONArray) { |
||||
system(cleanup_command); |
||||
return; |
||||
} |
||||
|
||||
/* getting array from root value and printing commit info */ |
||||
commits = json_value_get_array(root_value); |
||||
printf("%-10.10s %-10.10s %s\n", "Date", "SHA", "Author"); |
||||
for (i = 0; i < json_array_get_count(commits); i++) { |
||||
commit = json_array_get_object(commits, i); |
||||
printf("%.10s %.10s %s\n", |
||||
json_object_dotget_string(commit, "commit.author.date"), |
||||
json_object_get_string(commit, "sha"), |
||||
json_object_dotget_string(commit, "commit.author.name")); |
||||
} |
||||
|
||||
/* cleanup code */ |
||||
json_value_free(root_value); |
||||
system(cleanup_command); |
||||
} |
@ -0,0 +1,5 @@ |
||||
#!/bin/bash |
||||
gcc tests.c parson.c -Wall -pedantic-errors -std=c89 -o test |
||||
./test |
||||
rm -f *.o |
||||
rm test |
@ -0,0 +1,58 @@ |
||||
[ |
||||
"JSON Test Pattern pass1", |
||||
{"object with 1 member":["array with 1 element"]}, |
||||
{}, |
||||
[], |
||||
-42, |
||||
true, |
||||
false, |
||||
null, |
||||
{ |
||||
"integer": 1234567890, |
||||
"real": -9876.543210, |
||||
"e": 0.123456789e-12, |
||||
"E": 1.234567890E+34, |
||||
"": 23456789012E66, |
||||
"zero": 0, |
||||
"one": 1, |
||||
"space": " ", |
||||
"quote": "\"", |
||||
"backslash": "\\", |
||||
"controls": "\b\f\n\r\t", |
||||
"slash": "/ & \/", |
||||
"alpha": "abcdefghijklmnopqrstuvwyz", |
||||
"ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWYZ", |
||||
"digit": "0123456789", |
||||
"0123456789": "digit", |
||||
"special": "`1~!@#$%^&*()_+-={':[,]}|;.</>?", |
||||
"hex": "\u0123\u4567\u89AB\uCDEF\uabcd\uef4A", |
||||
"true": true, |
||||
"false": false, |
||||
"null": null, |
||||
"array":[ ], |
||||
"object":{ }, |
||||
"address": "50 St. James Street", |
||||
"url": "http://www.JSON.org/", |
||||
"comment": "// /* <!-- --", |
||||
"# -- --> */": " ", |
||||
" s p a c e d " :[1,2 , 3 |
||||
|
||||
, |
||||
|
||||
4 , 5 , 6 ,7 ],"compact":[1,2,3,4,5,6,7], |
||||
"jsontext": "{\"object with 1 member\":[\"array with 1 element\"]}", |
||||
"quotes": "" \u0022 %22 0x22 034 "", |
||||
"\/\\\"\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?" |
||||
: "A key can be any string" |
||||
}, |
||||
0.5 ,98.6 |
||||
, |
||||
99.44 |
||||
, |
||||
|
||||
1066, |
||||
1e1, |
||||
0.1e1, |
||||
1e-1, |
||||
1e00,2e+00,2e-00 |
||||
,"rosebud"] |
@ -0,0 +1 @@ |
||||
[[[[[[[[[[[[[[[[[[["Not too deep"]]]]]]]]]]]]]]]]]]] |
@ -0,0 +1,6 @@ |
||||
{ |
||||
"JSON Test Pattern pass3": { |
||||
"The outermost value": "must be an object or array.", |
||||
"In this test": "It is an object." |
||||
} |
||||
} |
@ -0,0 +1,19 @@ |
||||
{ |
||||
"string" : "lorem ipsum", |
||||
"utf string" : "\u006corem\u0020ipsum", |
||||
"positive one" : 1, |
||||
"negative one" : -1, |
||||
"pi" : 3.14, |
||||
"hard to parse number" : -3.14e-4, |
||||
"bool true" : true, |
||||
"bool false" : false, |
||||
"null" : null, |
||||
"string array" : ["lorem", "ipsum"], |
||||
"x^2 array" : [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100], |
||||
"object" : { "nested string" : "str", |
||||
"nested true" : true, |
||||
"nested false" : false, |
||||
"nested null" : null, |
||||
"nested number" : 123, |
||||
"nested array" : ["lorem", "ipsum"] } |
||||
} |
Loading…
Reference in new issue