mirror of
https://github.com/kgabis/parson.git
synced 2025-05-10 03:32:08 +00:00
commit
ede8ea497a
27
parson.c
27
parson.c
@ -19,12 +19,13 @@
|
|||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
THE SOFTWARE.
|
THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "parson.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <stddef.h>
|
|
||||||
#include "parson.h"
|
|
||||||
|
|
||||||
#define STARTING_CAPACITY 10
|
#define STARTING_CAPACITY 10
|
||||||
#define MAX_CAPACITY 10000
|
#define MAX_CAPACITY 10000
|
||||||
@ -36,32 +37,32 @@
|
|||||||
#define parson_realloc(a, b) realloc(a, b)
|
#define parson_realloc(a, b) realloc(a, b)
|
||||||
|
|
||||||
/* Type definitions */
|
/* Type definitions */
|
||||||
union json_value_value {
|
typedef union json_value_value {
|
||||||
const char *string;
|
const char *string;
|
||||||
double number;
|
double number;
|
||||||
JSON_Object *object;
|
JSON_Object *object;
|
||||||
JSON_Array *array;
|
JSON_Array *array;
|
||||||
int boolean;
|
int boolean;
|
||||||
int null;
|
int null;
|
||||||
};
|
} json_value_value;
|
||||||
|
|
||||||
struct json_value_t {
|
typedef struct json_value_t {
|
||||||
enum json_value_type type;
|
JSON_value_t type;
|
||||||
union json_value_value value;
|
json_value_value value;
|
||||||
};
|
} json_value_t;
|
||||||
|
|
||||||
struct json_object_t {
|
typedef struct json_object_t {
|
||||||
const char **names;
|
const char **names;
|
||||||
JSON_Value **values;
|
JSON_Value **values;
|
||||||
size_t count;
|
size_t count;
|
||||||
size_t capacity;
|
size_t capacity;
|
||||||
};
|
} json_object_t;
|
||||||
|
|
||||||
struct json_array_t {
|
typedef struct json_array_t {
|
||||||
JSON_Value **items;
|
JSON_Value **items;
|
||||||
size_t count;
|
size_t count;
|
||||||
size_t capacity;
|
size_t capacity;
|
||||||
};
|
} json_array_t;
|
||||||
|
|
||||||
/* JSON Object */
|
/* JSON Object */
|
||||||
static JSON_Object * json_object_init(void);
|
static JSON_Object * json_object_init(void);
|
||||||
@ -641,7 +642,7 @@ size_t json_array_get_count(const JSON_Array *array) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* JSON Value API */
|
/* JSON Value API */
|
||||||
enum json_value_type json_value_get_type(const JSON_Value *value) {
|
JSON_value_t json_value_get_type(const JSON_Value *value) {
|
||||||
return value ? value->type : JSONError;
|
return value ? value->type : JSONError;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
13
parson.h
13
parson.h
@ -27,15 +27,16 @@ extern "C"
|
|||||||
{
|
{
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#include <stddef.h> /* size_t */
|
||||||
|
|
||||||
|
|
||||||
/* Data structures, enums and typedefs */
|
/* Data structures, enums and typedefs */
|
||||||
struct json_object_t;
|
|
||||||
typedef struct json_object_t JSON_Object;
|
typedef struct json_object_t JSON_Object;
|
||||||
struct json_array_t;
|
|
||||||
typedef struct json_array_t JSON_Array;
|
typedef struct json_array_t JSON_Array;
|
||||||
struct json_value_t;
|
|
||||||
typedef struct json_value_t JSON_Value;
|
typedef struct json_value_t JSON_Value;
|
||||||
|
|
||||||
enum json_value_type {
|
typedef enum JSON_value_t {
|
||||||
JSONError = 0,
|
JSONError = 0,
|
||||||
JSONNull = 1,
|
JSONNull = 1,
|
||||||
JSONString = 2,
|
JSONString = 2,
|
||||||
@ -43,7 +44,7 @@ enum json_value_type {
|
|||||||
JSONObject = 4,
|
JSONObject = 4,
|
||||||
JSONArray = 5,
|
JSONArray = 5,
|
||||||
JSONBoolean = 6
|
JSONBoolean = 6
|
||||||
};
|
} JSON_value_t;
|
||||||
|
|
||||||
/* Parses first JSON value in a file, returns NULL in case of error */
|
/* Parses first JSON value in a file, returns NULL in case of error */
|
||||||
JSON_Value * json_parse_file(const char *filename);
|
JSON_Value * json_parse_file(const char *filename);
|
||||||
@ -80,7 +81,7 @@ int json_array_get_boolean(const JSON_Array *array, size_t index);
|
|||||||
size_t json_array_get_count (const JSON_Array *array);
|
size_t json_array_get_count (const JSON_Array *array);
|
||||||
|
|
||||||
/* JSON Value */
|
/* JSON Value */
|
||||||
enum json_value_type json_value_get_type(const JSON_Value *value);
|
JSON_value_t json_value_get_type (const JSON_Value *value);
|
||||||
JSON_Object* json_value_get_object (const JSON_Value *value);
|
JSON_Object* json_value_get_object (const JSON_Value *value);
|
||||||
JSON_Array * json_value_get_array (const JSON_Value *value);
|
JSON_Array * json_value_get_array (const JSON_Value *value);
|
||||||
const char * json_value_get_string (const JSON_Value *value);
|
const char * json_value_get_string (const JSON_Value *value);
|
||||||
|
4
tests.c
4
tests.c
@ -19,10 +19,12 @@
|
|||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
THE SOFTWARE.
|
THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "parson.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "parson.h"
|
|
||||||
|
|
||||||
#define TEST(A) printf("%-72s-",#A); \
|
#define TEST(A) printf("%-72s-",#A); \
|
||||||
if(A){puts(" OK");tests_passed++;} \
|
if(A){puts(" OK");tests_passed++;} \
|
||||||
|
Loading…
Reference in New Issue
Block a user