dark-hammer / include / core / json.h

/***********************************************************************************
 * Copyright (c) 2012, Sepehr Taghdisian
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice, 
 *   this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation 
 *   and/or other materials provided with the distribution.
 *
 ***********************************************************************************/


 /**
 * @defgroup json JSON
 * using cjson library, this file is basically a wrapper to cjson\n
 * note: library is thread-safe as long as multiple threads do not manipulate same json tree
 * for more JSON information visit: http://www.json.org/fatfree.html\n
 * libcjson : http://sourceforge.net/projects/cjson/\n
 */

#ifndef __JSON_H__
#define __JSON_H__

#include "types.h"
#include "core-api.h"
#include "file-mgr.h"
#include "allocator.h"

#ifdef __cplusplus
extern "C"  {
#endif

/**
 * basic json type, used in json functions, if =JSON_NULL then it is either uninitialized or error
 * @ingroup json
 */
typedef void*   json_t;

#define JSON_NULL   NULL

/**
 * initialize json faster and custom memory management
 * @param buf_size buffer size (in bytes) that is reserved for parsing json, can be =0 to set -
 * default buffer size
 * @ingroup json
 */
CORE_API result_t json_init(size_t buf_size);
CORE_API void json_release();
CORE_API void json_reassign_malloc();

/**
 * parse json file and create a root object, root object must be destroyed if not needed anymore 
 * @param f file handle of json file
 * @return json object, JSON_NULL if error occured
 * @ingroup json
 */
CORE_API json_t json_parsefile(file_t f, struct allocator* tmp_alloc);
/**
 * parse json string (buffer) 
 * @param str json formatted string
 * @return json object, JSON_NULL if error occured
 * @ingroup json
 */
CORE_API json_t json_parsestring(const char* str);
/**
 * save json data to file
 * @param filepath path to the file on the disk
 * @param trim trims output json data (no formatting) to be more optimized and compact
 * @ingroup json
 */
CORE_API result_t json_savetofile(json_t j, const char* filepath, bool_t trim);
/**
 * save json data to file handle 
 * @param f file handle that is ready and opened for writing
 * @param trim trims output json data (no formatting) to be more optimized and compact
 * @ingroup json
 */
CORE_API result_t json_savetofilef(json_t j, file_t f, bool_t trim);
/**
 * save json data to buffer, returns resulting string. user should call json_deletebuffer after
 * @param alloc allocator that is used to create the output buffer
 * @param outsize output buffer size, including /0 character
 * @param trim trims output json data (no formatting) to be more optimized and compact
 * @see json_deletebuffer
 * @ingroup json
 */
CORE_API char* json_savetobuffer(json_t j, size_t* outsize, bool_t trim);

/**
 * deletes previously created json buffer @see json_savetobuffer
 */
CORE_API void json_deletebuffer(char* buffer);

/**
 * destroys json object and free memory 
 * @param j json object that is allocated previously
 * @ingroup json
 */
CORE_API void json_destroy(json_t j);

/* set/get functions for json items 
 **
 * @ingroup json
 */
CORE_API void json_seti(json_t j, int32 n);
/**
 * @ingroup json
 */
CORE_API void json_setf(json_t j, fl32 f);
/**
 * @ingroup json
 */
CORE_API void json_sets(json_t j, const char* str);
/**
 * @ingroup json
 */
CORE_API void json_setb(json_t j, bool_t b);
/**
 * @ingroup json
 */
CORE_API int32 json_geti(json_t j);
/**
 * @ingroup json
 */
CORE_API fl32 json_getf(json_t j);
/**
 * @ingroup json
 */
CORE_API const char* json_gets(json_t j);
/**
 * @ingroup json
 */
CORE_API bool_t json_getb(json_t j);

/**
 * @ingroup json
 */
CORE_API int32 json_geti_child(json_t parent, const char* name, int32 def_value);
/**
 * @ingroup json
 */
CORE_API fl32 json_getf_child(json_t parent, const char* name, fl32 def_value);
/**
 * @ingroup json
 */
CORE_API const char* json_gets_child(json_t parent, const char* name, const char* def_value);
/**
 * @ingroup json
 */
CORE_API bool_t json_getb_child(json_t parent, const char* name, bool_t def_value);

/**
 * get array size (number of items) from a json array item 
 * @ingroup json
 */
CORE_API int32 json_getarr_count(json_t j);
/**
 * get array item from a json array item 
 * @param idx zero-based index to the array
 * @ingroup json
 */
CORE_API json_t json_getarr_item(json_t j, int32 idx);
/**
 * get child item from an json object referenced by a name 
 * @ingroup json
 */
CORE_API json_t json_getitem(json_t j, const char* name);

/* creating json items for different types 
 **
 * @ingroup json
 */
CORE_API json_t json_create_null();
/**
 * @ingroup json
 */
CORE_API json_t json_create_obj();
/**
 * @ingroup json
 */
CORE_API json_t json_create_bool(bool_t b);
/**
 * @ingroup json
 */
CORE_API json_t json_create_num(fl64 n);
/**
 * @ingroup json
 */
CORE_API json_t json_create_str(const char* str);
/**
 * @ingroup json
 */
CORE_API json_t json_create_arr();
/**
 * @ingroup json
 */
CORE_API json_t json_create_arri(int32* nums, int32 count);
/**
 * @ingroup json
 */
CORE_API json_t json_create_arrf(fl32* nums, int32 count);
/**
 * @ingroup json
 */
CORE_API json_t json_create_arrs(const char** strs, int32 count);

/**
 * add single item to array type 
 * @ingroup json
 */
CORE_API void json_additem_toarr(json_t arr, json_t item);
/**
 * add item to object type 
 * @ingroup json
 */
CORE_API void json_additem_toobj(json_t obj, const char* name, json_t item);

#ifdef __cplusplus
}
#endif

#endif /* __JSON_H__ */
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.